Skip to content

Commit

Permalink
feat(findFiles): configuration based use ripgrep for findfiles
Browse files Browse the repository at this point in the history
  • Loading branch information
alaindresse committed Nov 30, 2024
1 parent cf68704 commit 6967784
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 25 deletions.
44 changes: 24 additions & 20 deletions .vscode/settings.json
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"
}
}
17 changes: 17 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,23 @@
"markdownDescription": "%asciidoc.antora.showEnableAntoraPrompt.desc%"
}
}
},
{
"order": 27,
"id": "security",
"title": "%asciidoc.findfiles.title%",
"properties": {
"asciidoc.findfiles.useRipgrep": {
"type": "boolean",
"default": false,
"markdownDescription": "%asciidoc.findfiles.useRipgrep.desc%"
},
"asciidoc.findfiles.ripgrepPath": {
"type": "string",
"default": "",
"markdownDescription": "%asciidoc.findfiles.ripgrepPath.desc%"
}
}
}
],
"configurationDefaults": {
Expand Down
5 changes: 4 additions & 1 deletion package.nls.de.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,8 @@
"asciidoc.antora.enableAntoraSupport.deprecationMessage": "This setting has been replaced by `#asciidoc.antora.showEnableAntoraPrompt#` and no longer has any effect.",
"asciidoc.antora.title": "Antora",
"asciidoc.antora.enableAntoraSupport.desc": "Aktiviere [Antora](https://antora.org/) Unterstützung.",
"asciidoc.antora.showEnableAntoraPrompt.desc": "Show a prompt to enable [Antora](https://antora.org/) support when an antora.yml file is detected."
"asciidoc.antora.showEnableAntoraPrompt.desc": "Show a prompt to enable [Antora](https://antora.org/) support when an antora.yml file is detected.",
"asciidoc.findfiles.title": "Find files",
"asciidoc.findfiles.useRipgrep.desc": "Use ripgrep to search for files in the workspace.",
"asciidoc.findfiles.ripgrepPath.desc": "External `rg` command to execute. It accepts a full path to the binary, for instance: `/path/to/rg`. If the value is empty, use ripgrep from VSCode's built-in version."
}
5 changes: 4 additions & 1 deletion package.nls.fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,8 @@
"asciidoc.antora.enableAntoraSupport.deprecationMessage": "Ce paramètre a été remplacé par `#asciidoc.antora.showEnableAntoraPrompt#` et n'a plus aucun effet.",
"asciidoc.antora.title": "Antora",
"asciidoc.antora.enableAntoraSupport.desc": "Active le support [Antora](https://antora.org/).",
"asciidoc.antora.showEnableAntoraPrompt.desc": "Affiche une fenêtre contextuelle permettant d'activer le support [Antora](https://antora.org/) quand un fichier antora.yml est trouvé."
"asciidoc.antora.showEnableAntoraPrompt.desc": "Affiche une fenêtre contextuelle permettant d'activer le support [Antora](https://antora.org/) quand un fichier antora.yml est trouvé.",
"asciidoc.findfiles.title": "Find files",
"asciidoc.findfiles.useRipgrep.desc": "Use ripgrep to search for files in the workspace.",
"asciidoc.findfiles.ripgrepPath.desc": "External `rg` command to execute. It accepts a full path to the binary, for instance: `/path/to/rg`. If the value is empty, use ripgrep from VSCode's built-in version."
}
5 changes: 4 additions & 1 deletion package.nls.ja.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,8 @@
"asciidoc.antora.enableAntoraSupport.deprecationMessage": "This setting has been replaced by `#asciidoc.antora.showEnableAntoraPrompt#` and no longer has any effect.",
"asciidoc.antora.title": "Antora",
"asciidoc.antora.enableAntoraSupport.desc": "[Antora](https://antora.org/)サポートを有効にします。",
"asciidoc.antora.showEnableAntoraPrompt.desc": "Show a prompt to enable [Antora](https://antora.org/) support when an antora.yml file is detected."
"asciidoc.antora.showEnableAntoraPrompt.desc": "Show a prompt to enable [Antora](https://antora.org/) support when an antora.yml file is detected.",
"asciidoc.findfiles.title": "Find files",
"asciidoc.findfiles.useRipgrep.desc": "Use ripgrep to search for files in the workspace.",
"asciidoc.findfiles.ripgrepPath.desc": "External `rg` command to execute. It accepts a full path to the binary, for instance: `/path/to/rg`. If the value is empty, use ripgrep from VSCode's built-in version."
}
6 changes: 5 additions & 1 deletion package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,9 @@

"asciidoc.antora.title": "Antora",
"asciidoc.antora.enableAntoraSupport.desc": "Enable [Antora](https://antora.org/) support.",
"asciidoc.antora.showEnableAntoraPrompt.desc": "Show a prompt to enable [Antora](https://antora.org/) support when an antora.yml file is detected."
"asciidoc.antora.showEnableAntoraPrompt.desc": "Show a prompt to enable [Antora](https://antora.org/) support when an antora.yml file is detected.",

"asciidoc.findfiles.title": "Find files",
"asciidoc.findfiles.useRipgrep.desc": "Use ripgrep to search for files in the workspace.",
"asciidoc.findfiles.ripgrepPath.desc": "External `rg` command to execute. It accepts a full path to the binary, for instance: `/path/to/rg`. If the value is empty, use ripgrep from VSCode's built-in version."
}
62 changes: 61 additions & 1 deletion src/util/findFiles.ts
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)
})
})
}

0 comments on commit 6967784

Please sign in to comment.