diff --git a/src/vs/workbench/services/search/node/ripgrepTextSearch.ts b/src/vs/workbench/services/search/node/ripgrepTextSearch.ts index aa04058965697e062337173b03117145760166d6..6ae30eb0b4d01456b68a83953365c7ad1bd62762 100644 --- a/src/vs/workbench/services/search/node/ripgrepTextSearch.ts +++ b/src/vs/workbench/services/search/node/ripgrepTextSearch.ts @@ -74,7 +74,7 @@ export class RipgrepEngine { this.rgProc = cp.spawn(rgPath, rgArgs.globArgs, { cwd }); process.once('exit', this.killRgProcFn); - this.ripgrepParser = new RipgrepParser(this.config.maxResults, cwd); + this.ripgrepParser = new RipgrepParser(this.config.maxResults, cwd, this.config.extraFiles); this.ripgrepParser.on('result', (match: ISerializedFileMatch) => { if (this.postProcessExclusions) { const handleResultP = (>this.postProcessExclusions(match.path, undefined, () => getSiblings(match.path))) @@ -170,12 +170,15 @@ export class RipgrepParser extends EventEmitter { private remainder: string; private isDone: boolean; private stringDecoder: NodeStringDecoder; + private extraSearchFiles: string[]; private numResults = 0; - constructor(private maxResults: number, private rootFolder: string) { + constructor(private maxResults: number, private rootFolder: string, extraFiles?: string[]) { super(); this.stringDecoder = new StringDecoder(); + + this.extraSearchFiles = extraFiles || []; } public cancel(): void { @@ -229,19 +232,37 @@ export class RipgrepParser extends EventEmitter { this.onResult(); } - this.fileMatch = new FileMatch(path.isAbsolute(r[1]) ? r[1] : path.join(this.rootFolder, r[1])); + this.fileMatch = this.getFileMatch(r[1]); } else { // Line is empty (or malformed) } } } + private getFileMatch(relativeOrAbsolutePath: string): FileMatch { + const absPath = path.isAbsolute(relativeOrAbsolutePath) ? + relativeOrAbsolutePath : + path.join(this.rootFolder, relativeOrAbsolutePath); + + return new FileMatch(absPath); + } + private handleMatchLine(outputLine: string, lineNum: number, text: string): void { if (lineNum === 0) { text = strings.stripUTF8BOM(text); } const lineMatch = new LineMatch(text, lineNum); + if (!this.fileMatch) { + // When searching a single file and no folderQueries, rg does not print the file line, so create it here + const singleFile = this.extraSearchFiles[0]; + if (!singleFile) { + throw new Error('Got match line for unknown file'); + } + + this.fileMatch = this.getFileMatch(singleFile); + } + this.fileMatch.addMatch(lineMatch); let lastMatchEndPos = 0;