-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(ripgrep): replace ripgrep by workspace.findFiles (#920)
- Loading branch information
1 parent
ac3064b
commit c854814
Showing
1 changed file
with
1 addition
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,9 @@ | ||
import { spawn } from 'child_process' | ||
import ospath from 'path' | ||
import vscode, { Uri } from 'vscode' | ||
import { getWorkspaceFolders } from './workspace' | ||
|
||
/** | ||
* Find files across all workspace folders in the workspace using a glob expression. | ||
* Use `@vscode/ripgrep` to find files when there is a platform shell present. | ||
* @param glob A glob pattern that defines the files to search for. | ||
*/ | ||
export async function findFiles (glob: string): Promise<Uri[]> { | ||
if ('browser' in process && (process as any).browser === true) { | ||
return vscode.workspace.findFiles(glob) | ||
} | ||
const searchedUris : Uri[] = [] | ||
for (const workspaceFolder of getWorkspaceFolders()) { | ||
const rootUri = workspaceFolder.uri | ||
const paths = await ripgrep(glob, rootUri.fsPath) | ||
searchedUris.push(...paths.map((path) => Uri.joinPath(rootUri, path))) | ||
} | ||
return searchedUris | ||
} | ||
|
||
async function ripgrep (glob: string, rootFolder: string): Promise<string[]> { | ||
const rgPath = ospath.join(vscode.env.appRoot, `node_modules/@vscode/ripgrep/bin/rg${process.platform === 'win32' ? '.exe' : ''}`) | ||
return new Promise((resolve, reject) => { | ||
const rg = spawn(rgPath, ['--hidden', '--follow', '--files', '-g', glob], { cwd: rootFolder }) | ||
let stdout : string = '' | ||
let stderr = '' | ||
|
||
rg.stdout.on('data', (data) => { | ||
stdout += data.toString() | ||
}) | ||
|
||
rg.stderr.on('data', (data) => { | ||
stderr += data.toString() | ||
}) | ||
|
||
rg.on('close', (code) => { | ||
if (code === 0) { | ||
const result = stdout.split('\n') | ||
.map((path) => path.trim()) | ||
.filter((path) => !!path) // ensure empty strings are deleted from answer | ||
resolve(result) | ||
} else if (code === 1) { | ||
resolve([]) | ||
} else { | ||
reject(new Error(`code ${code}: ${stderr}`)) | ||
} | ||
}) | ||
|
||
rg.on('error', (err) => { | ||
reject(err) | ||
}) | ||
}) | ||
return vscode.workspace.findFiles(glob) | ||
} |