From c2f68baa78d90f135cc6800cfa7e7140d6d09933 Mon Sep 17 00:00:00 2001 From: Connor Peet Date: Thu, 3 Dec 2020 22:44:41 -0800 Subject: [PATCH] search: fix handle empty results from ripgrep https://github.com/microsoft/vscode/issues/100569 --- .../search/node/ripgrepTextSearchEngine.ts | 12 +++++++++ .../test/node/ripgrepTextSearchEngine.test.ts | 27 +++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/src/vs/workbench/services/search/node/ripgrepTextSearchEngine.ts b/src/vs/workbench/services/search/node/ripgrepTextSearchEngine.ts index 53c6ae8fd67e4..074d3528f2947 100644 --- a/src/vs/workbench/services/search/node/ripgrepTextSearchEngine.ts +++ b/src/vs/workbench/services/search/node/ripgrepTextSearchEngine.ts @@ -283,6 +283,18 @@ export class RipgrepParser extends EventEmitter { let prevMatchEnd = 0; let prevMatchEndCol = 0; let prevMatchEndLine = lineNumber; + + // it looks like certain regexes can match a line, but cause rg to not + // emit any specific submatches for that line. + // https://github.com/microsoft/vscode/issues/100569#issuecomment-738496991 + if (data.submatches.length === 0) { + data.submatches.push( + fullText.length + ? { start: 0, end: 1, match: { text: fullText[0] } } + : { start: 0, end: 0, match: { text: '' } } + ); + } + const ranges = coalesce(data.submatches.map((match, i) => { if (this.hitLimit) { return null; diff --git a/src/vs/workbench/services/search/test/node/ripgrepTextSearchEngine.test.ts b/src/vs/workbench/services/search/test/node/ripgrepTextSearchEngine.test.ts index 9e15c6dc9bb76..3815dee816ba3 100644 --- a/src/vs/workbench/services/search/test/node/ripgrepTextSearchEngine.test.ts +++ b/src/vs/workbench/services/search/test/node/ripgrepTextSearchEngine.test.ts @@ -226,5 +226,32 @@ suite('RipgrepTextSearchEngine', () => { } ]); }); + + + test('empty result (#100569)', () => { + testParser( + [ + makeRgMatch('file1.js', 'foobar', 4, []), + makeRgMatch('file1.js', '', 5, []), + ], + [ + { + preview: { + text: 'foobar', + matches: [new Range(0, 0, 0, 1)] + }, + uri: joinPath(TEST_FOLDER, 'file1.js'), + ranges: [new Range(3, 0, 3, 1)] + }, + { + preview: { + text: '', + matches: [new Range(0, 0, 0, 0)] + }, + uri: joinPath(TEST_FOLDER, 'file1.js'), + ranges: [new Range(4, 0, 4, 0)] + } + ]); + }); }); });