Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use ripgrep instead of vscode findFiles #909

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 38 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,7 @@
"util": "^0.12.5",
"uuid": "8.3.2",
"vscode-nls": "5.2.0",
"vscode-ripgrep": "^1.13.2",
"vscode-uri": "^3.0.3",
"worker-thread": "^1.1.0"
},
Expand Down
7 changes: 4 additions & 3 deletions src/features/antora/antoraSupport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import ContentCatalog from '@antora/content-classifier/content-catalog'
import { getWorkspaceFolder } from '../../util/workspace'
import { dir, exists } from '../../util/file'
import * as contentClassifier from '@antora/content-classifier'
import { wrappedFindFiles } from '../../util/wrappedFindFiles'
const classifyContent = contentClassifier.default || contentClassifier

const MAX_DEPTH_SEARCH_ANTORA_CONFIG = 100
Expand Down Expand Up @@ -192,7 +193,7 @@ export async function findAntoraConfigFile (textDocumentUri: Uri): Promise<Uri |
cancellationToken.token.onCancellationRequested((e) => {
console.log('Cancellation requested, cause: ' + e)
})
const antoraConfigUris = await vscode.workspace.findFiles('**/antora.yml', undefined, 100, cancellationToken.token)
const antoraConfigUris = await wrappedFindFiles('**/antora.yml')
// check for Antora configuration
for (const antoraConfigUri of antoraConfigUris) {
const antoraConfigParentDirPath = antoraConfigUri.path.slice(0, antoraConfigUri.path.lastIndexOf('/'))
Expand Down Expand Up @@ -238,7 +239,7 @@ export async function getAntoraConfigs (): Promise<AntoraConfig[]> {
cancellationToken.token.onCancellationRequested((e) => {
console.log('Cancellation requested, cause: ' + e)
})
const antoraConfigUris = await vscode.workspace.findFiles('**/antora.yml', undefined, 100, cancellationToken.token)
const antoraConfigUris = await wrappedFindFiles('**/antora.yml')
// check for Antora configuration
const antoraConfigs = await Promise.all(antoraConfigUris.map(async (antoraConfigUri) => {
let config = {}
Expand Down Expand Up @@ -293,7 +294,7 @@ export async function getAntoraDocumentContext (textDocumentUri: Uri, workspaceS
const workspaceFolder = getWorkspaceFolder(antoraConfig.uri)
const workspaceRelative = posixpath.relative(workspaceFolder.uri.path, antoraConfig.contentSourceRootPath)
const globPattern = 'modules/*/{attachments,examples,images,pages,partials,assets}/**'
const files = await Promise.all((await vscode.workspace.findFiles(`${workspaceRelative ? `${workspaceRelative}/` : ''}${globPattern}`)).map(async (file) => {
const files = await Promise.all((await wrappedFindFiles(`${workspaceRelative ? `${workspaceRelative}/` : ''}${globPattern}`)).map(async (file) => {
const contentSourceRootPath = antoraConfig.contentSourceRootPath
return {
base: contentSourceRootPath,
Expand Down
3 changes: 2 additions & 1 deletion src/features/asciidoctorExtensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import vscode from 'vscode'
import { AsciidoctorExtensionsSecurityPolicyArbiter } from '../security'
import { Asciidoctor } from '@asciidoctor/core'
import { mermaidJSProcessor } from './mermaid'
import { wrappedFindFiles } from '../util/wrappedFindFiles'

export interface AsciidoctorExtensionsProvider {
activate(registry: Asciidoctor.Extensions.Registry): Promise<void>;
Expand Down Expand Up @@ -38,7 +39,7 @@ export class AsciidoctorExtensions {
}

private async getExtensionFilesInWorkspace (): Promise<vscode.Uri[]> {
return vscode.workspace.findFiles('.asciidoctor/lib/**/*.js')
return wrappedFindFiles('.asciidoctor/lib/**/*.js')
}

private isAsciidoctorExtensionsRegistrationEnabled (): boolean {
Expand Down
3 changes: 2 additions & 1 deletion src/features/workspaceSymbolProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { isAsciidocFile } from '../util/file'
import { Lazy, lazy } from '../util/lazy'
import AdocDocumentSymbolProvider from './documentSymbolProvider'
import { SkinnyTextDocument } from '../util/document'
import { wrappedFindFiles } from '../util/wrappedFindFiles'

export interface WorkspaceAsciidocDocumentProvider {
getAllAsciidocDocuments(): Promise<Iterable<SkinnyTextDocument>>;
Expand Down Expand Up @@ -37,7 +38,7 @@ class VSCodeWorkspaceAsciidocDocumentProvider implements WorkspaceAsciidocDocume
}

async getAllAsciidocDocuments () {
const resources = await vscode.workspace.findFiles('**/*.adoc', '**/node_modules/**')
const resources = await wrappedFindFiles('**/*.adoc')
const docs = await Promise.all(resources.map((doc) => this.getAsciidocDocument(doc)))
return docs.filter((doc) => !!doc) as SkinnyTextDocument[]
}
Expand Down
3 changes: 2 additions & 1 deletion src/providers/bibtex.provider.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as vscode from 'vscode'
import { createContext, Context } from './createContext'
import { readFileSync } from 'fs'
import { wrappedFindFiles } from '../util/wrappedFindFiles'
const bibtexParse = require('@orcid/bibtex-parse-js')

export const BibtexProvider = {
Expand Down Expand Up @@ -31,7 +32,7 @@ function shouldProvide (context: Context): boolean {
}

async function getCitationKeys (): Promise<string[]> {
const files = await vscode.workspace.findFiles('*.bib')
const files = await wrappedFindFiles('*.bib')
const filesContent = files.map((file) =>
readFileSync(file.path).toString('utf-8')
)
Expand Down
3 changes: 2 additions & 1 deletion src/providers/xref.provider.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as path from 'path'
import * as vscode from 'vscode'
import { createContext, Context } from './createContext'
import { wrappedFindFiles } from '../util/wrappedFindFiles'

export const xrefProvider = {
provideCompletionItems,
Expand Down Expand Up @@ -83,7 +84,7 @@ async function provideCrossRef (context: Context): Promise<vscode.CompletionItem
)

const completionItems: vscode.CompletionItem[] = []
const workspacesAdocFiles = await vscode.workspace.findFiles('**/*.adoc')
const workspacesAdocFiles = await wrappedFindFiles('**/*.adoc')
for (const adocFile of workspacesAdocFiles) {
const labels = await getIdsFromFile(adocFile)
for (const label of labels) {
Expand Down
66 changes: 66 additions & 0 deletions src/util/wrappedFindFiles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { spawn } from 'child_process'
import { rgPath } from 'vscode-ripgrep'
import { Uri } from 'vscode'
import { getWorkspaceFolders } from './workspace'

async function ripgrep (glob: string, rootFolder: string): Promise<string[]> {
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)
})
})
}

async function internalWrappedFindFiles (glob: string): Promise<Uri[]> {
// const uris = await vscode.workspace.findFiles(glob, undefined, 100, token)
const searchedUris : Uri[] = []

for (const workspaceFolder of getWorkspaceFolders()) {
const rootUri = workspaceFolder.uri
const paths = await ripgrep(glob, rootUri.path)
searchedUris.push(...paths.map((path) => Uri.joinPath(rootUri, path)))
}
return searchedUris
}

const cache: Map<string, { timestamp: number, uris: Uri[] }> = new Map()

function isCacheValid (timestamp: number): boolean {
return (Date.now() - timestamp) < 5000
}

export async function wrappedFindFiles (glob: string): Promise<Uri[]> {
const cacheEntry = cache.get(glob)
if (cacheEntry && isCacheValid(cacheEntry.timestamp)) {
return cacheEntry.uris
}

const uris = await internalWrappedFindFiles(glob)
cache.set(glob, { timestamp: Date.now(), uris })
return uris
}
Loading