-
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.
feat(findFiles): configuration based use ripgrep for findfiles
- Loading branch information
1 parent
cf68704
commit 6967784
Showing
7 changed files
with
119 additions
and
25 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,22 +1,26 @@ | ||
{ | ||
"javascript.format.placeOpenBraceOnNewLineForFunctions": true, | ||
"javascript.format.placeOpenBraceOnNewLineForControlBlocks": true, | ||
"typescript.format.placeOpenBraceOnNewLineForFunctions": true, | ||
"typescript.format.placeOpenBraceOnNewLineForControlBlocks": true, | ||
"cSpell.words": [ | ||
"asciidoctor", | ||
"deserialize", | ||
"helvetica", | ||
"joaompinto", | ||
"junit", | ||
"linkify", | ||
"neue", | ||
"plantuml", | ||
"segoe", | ||
"webview" | ||
], | ||
"files.watcherExclude": { | ||
"out/**": true, | ||
"dist/**": true | ||
}, | ||
"javascript.format.placeOpenBraceOnNewLineForFunctions": true, | ||
"javascript.format.placeOpenBraceOnNewLineForControlBlocks": true, | ||
"typescript.format.placeOpenBraceOnNewLineForFunctions": true, | ||
"typescript.format.placeOpenBraceOnNewLineForControlBlocks": true, | ||
"cSpell.words": [ | ||
"asciidoctor", | ||
"deserialize", | ||
"helvetica", | ||
"joaompinto", | ||
"junit", | ||
"linkify", | ||
"neue", | ||
"plantuml", | ||
"segoe", | ||
"webview" | ||
], | ||
"files.watcherExclude": { | ||
"out/**": true, | ||
"dist/**": true | ||
}, | ||
"editor.formatOnSave": false, | ||
"editor.codeActionsOnSave": { | ||
"source.fixAll.eslint": "explicit" | ||
} | ||
} |
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
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
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
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
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
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,9 +1,69 @@ | ||
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[]> { | ||
return vscode.workspace.findFiles(glob) | ||
const isBrowser = ((process as any)?.browser === true) | ||
const useRipgrep = (vscode.workspace.getConfiguration('asciidoc', null).get('registerAsciidoctorExtensions') === true) | ||
|
||
if (isBrowser || !useRipgrep) { | ||
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 config = vscode.workspace.getConfiguration('asciidoc.findfiles') | ||
const customPath = config.get<string>('ripgrepPath') | ||
const rgPath = customPath || 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) | ||
}) | ||
}) | ||
} |