-
Notifications
You must be signed in to change notification settings - Fork 30.4k
/
Copy pathripgrepTextSearchEngine.ts
631 lines (523 loc) · 18.5 KB
/
ripgrepTextSearchEngine.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as cp from 'child_process';
import { EventEmitter } from 'events';
import { StringDecoder } from 'string_decoder';
import { coalesce } from 'vs/base/common/arrays';
import { CancellationToken } from 'vs/base/common/cancellation';
import { groupBy } from 'vs/base/common/collections';
import { splitGlobAware } from 'vs/base/common/glob';
import * as path from 'vs/base/common/path';
import { createRegExp, escapeRegExpCharacters } from 'vs/base/common/strings';
import { URI } from 'vs/base/common/uri';
import { Progress } from 'vs/platform/progress/common/progress';
import { IExtendedExtensionSearchOptions, SearchError, SearchErrorCode, serializeSearchError } from 'vs/workbench/services/search/common/search';
import { Range, TextSearchComplete, TextSearchContext, TextSearchMatch, TextSearchOptions, TextSearchPreviewOptions, TextSearchQuery, TextSearchResult } from 'vs/workbench/services/search/common/searchExtTypes';
import { AST as ReAST, RegExpParser, RegExpVisitor } from 'vscode-regexpp';
import { rgPath } from '@vscode/ripgrep';
import { anchorGlob, createTextSearchResult, IOutputChannel, Maybe } from './ripgrepSearchUtils';
// If @vscode/ripgrep is in an .asar file, then the binary is unpacked.
const rgDiskPath = rgPath.replace(/\bnode_modules\.asar\b/, 'node_modules.asar.unpacked');
export class RipgrepTextSearchEngine {
constructor(private outputChannel: IOutputChannel) { }
provideTextSearchResults(query: TextSearchQuery, options: TextSearchOptions, progress: Progress<TextSearchResult>, token: CancellationToken): Promise<TextSearchComplete> {
this.outputChannel.appendLine(`provideTextSearchResults ${query.pattern}, ${JSON.stringify({
...options,
...{
folder: options.folder.toString()
}
})}`);
return new Promise((resolve, reject) => {
token.onCancellationRequested(() => cancel());
const rgArgs = getRgArgs(query, options);
const cwd = options.folder.fsPath;
const escapedArgs = rgArgs
.map(arg => arg.match(/^-/) ? arg : `'${arg}'`)
.join(' ');
this.outputChannel.appendLine(`${rgDiskPath} ${escapedArgs}\n - cwd: ${cwd}`);
let rgProc: Maybe<cp.ChildProcess> = cp.spawn(rgDiskPath, rgArgs, { cwd });
rgProc.on('error', e => {
console.error(e);
this.outputChannel.appendLine('Error: ' + (e && e.message));
reject(serializeSearchError(new SearchError(e && e.message, SearchErrorCode.rgProcessError)));
});
let gotResult = false;
const ripgrepParser = new RipgrepParser(options.maxResults, cwd, options.previewOptions);
ripgrepParser.on('result', (match: TextSearchResult) => {
gotResult = true;
dataWithoutResult = '';
progress.report(match);
});
let isDone = false;
const cancel = () => {
isDone = true;
rgProc?.kill();
ripgrepParser?.cancel();
};
let limitHit = false;
ripgrepParser.on('hitLimit', () => {
limitHit = true;
cancel();
});
let dataWithoutResult = '';
rgProc.stdout!.on('data', data => {
ripgrepParser.handleData(data);
if (!gotResult) {
dataWithoutResult += data;
}
});
let gotData = false;
rgProc.stdout!.once('data', () => gotData = true);
let stderr = '';
rgProc.stderr!.on('data', data => {
const message = data.toString();
this.outputChannel.appendLine(message);
if (stderr.length + message.length < 1e6) {
stderr += message;
}
});
rgProc.on('close', () => {
this.outputChannel.appendLine(gotData ? 'Got data from stdout' : 'No data from stdout');
this.outputChannel.appendLine(gotResult ? 'Got result from parser' : 'No result from parser');
if (dataWithoutResult) {
this.outputChannel.appendLine(`Got data without result: ${dataWithoutResult}`);
}
this.outputChannel.appendLine('');
if (isDone) {
resolve({ limitHit });
} else {
// Trigger last result
ripgrepParser.flush();
rgProc = null;
let searchError: Maybe<SearchError>;
if (stderr && !gotData && (searchError = rgErrorMsgForDisplay(stderr))) {
reject(serializeSearchError(new SearchError(searchError.message, searchError.code)));
} else {
resolve({ limitHit });
}
}
});
});
}
}
/**
* Read the first line of stderr and return an error for display or undefined, based on a list of
* allowed properties.
* Ripgrep produces stderr output which is not from a fatal error, and we only want the search to be
* "failed" when a fatal error was produced.
*/
export function rgErrorMsgForDisplay(msg: string): Maybe<SearchError> {
const lines = msg.split('\n');
const firstLine = lines[0].trim();
if (lines.some(l => l.startsWith('regex parse error'))) {
return new SearchError(buildRegexParseError(lines), SearchErrorCode.regexParseError);
}
const match = firstLine.match(/grep config error: unknown encoding: (.*)/);
if (match) {
return new SearchError(`Unknown encoding: ${match[1]}`, SearchErrorCode.unknownEncoding);
}
if (firstLine.startsWith('error parsing glob')) {
// Uppercase first letter
return new SearchError(firstLine.charAt(0).toUpperCase() + firstLine.substr(1), SearchErrorCode.globParseError);
}
if (firstLine.startsWith('the literal')) {
// Uppercase first letter
return new SearchError(firstLine.charAt(0).toUpperCase() + firstLine.substr(1), SearchErrorCode.invalidLiteral);
}
if (firstLine.startsWith('PCRE2: error compiling pattern')) {
return new SearchError(firstLine, SearchErrorCode.regexParseError);
}
return undefined;
}
export function buildRegexParseError(lines: string[]): string {
const errorMessage: string[] = ['Regex parse error'];
const pcre2ErrorLine = lines.filter(l => (l.startsWith('PCRE2:')));
if (pcre2ErrorLine.length >= 1) {
const pcre2ErrorMessage = pcre2ErrorLine[0].replace('PCRE2:', '');
if (pcre2ErrorMessage.indexOf(':') !== -1 && pcre2ErrorMessage.split(':').length >= 2) {
const pcre2ActualErrorMessage = pcre2ErrorMessage.split(':')[1];
errorMessage.push(':' + pcre2ActualErrorMessage);
}
}
return errorMessage.join('');
}
export class RipgrepParser extends EventEmitter {
private remainder = '';
private isDone = false;
private hitLimit = false;
private stringDecoder: StringDecoder;
private numResults = 0;
constructor(private maxResults: number, private rootFolder: string, private previewOptions?: TextSearchPreviewOptions) {
super();
this.stringDecoder = new StringDecoder();
}
cancel(): void {
this.isDone = true;
}
flush(): void {
this.handleDecodedData(this.stringDecoder.end());
}
override on(event: 'result', listener: (result: TextSearchResult) => void): this;
override on(event: 'hitLimit', listener: () => void): this;
override on(event: string, listener: (...args: any[]) => void): this {
super.on(event, listener);
return this;
}
handleData(data: Buffer | string): void {
if (this.isDone) {
return;
}
const dataStr = typeof data === 'string' ? data : this.stringDecoder.write(data);
this.handleDecodedData(dataStr);
}
private handleDecodedData(decodedData: string): void {
// check for newline before appending to remainder
let newlineIdx = decodedData.indexOf('\n');
// If the previous data chunk didn't end in a newline, prepend it to this chunk
const dataStr = this.remainder + decodedData;
if (newlineIdx >= 0) {
newlineIdx += this.remainder.length;
} else {
// Shortcut
this.remainder = dataStr;
return;
}
let prevIdx = 0;
while (newlineIdx >= 0) {
this.handleLine(dataStr.substring(prevIdx, newlineIdx).trim());
prevIdx = newlineIdx + 1;
newlineIdx = dataStr.indexOf('\n', prevIdx);
}
this.remainder = dataStr.substring(prevIdx);
}
private handleLine(outputLine: string): void {
if (this.isDone || !outputLine) {
return;
}
let parsedLine: IRgMessage;
try {
parsedLine = JSON.parse(outputLine);
} catch (e) {
throw new Error(`malformed line from rg: ${outputLine}`);
}
if (parsedLine.type === 'match') {
const matchPath = bytesOrTextToString(parsedLine.data.path);
const uri = URI.file(path.join(this.rootFolder, matchPath));
const result = this.createTextSearchMatch(parsedLine.data, uri);
this.onResult(result);
if (this.hitLimit) {
this.cancel();
this.emit('hitLimit');
}
} else if (parsedLine.type === 'context') {
const contextPath = bytesOrTextToString(parsedLine.data.path);
const uri = URI.file(path.join(this.rootFolder, contextPath));
const result = this.createTextSearchContext(parsedLine.data, uri);
result.forEach(r => this.onResult(r));
}
}
private createTextSearchMatch(data: IRgMatch, uri: URI): TextSearchMatch {
const lineNumber = data.line_number - 1;
const fullText = bytesOrTextToString(data.lines);
const fullTextBytes = Buffer.from(fullText);
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;
}
this.numResults++;
if (this.numResults >= this.maxResults) {
// Finish the line, then report the result below
this.hitLimit = true;
}
const matchText = bytesOrTextToString(match.match);
const inBetweenText = fullTextBytes.slice(prevMatchEnd, match.start).toString();
const inBetweenStats = getNumLinesAndLastNewlineLength(inBetweenText);
const startCol = inBetweenStats.numLines > 0 ?
inBetweenStats.lastLineLength :
inBetweenStats.lastLineLength + prevMatchEndCol;
const stats = getNumLinesAndLastNewlineLength(matchText);
const startLineNumber = inBetweenStats.numLines + prevMatchEndLine;
const endLineNumber = stats.numLines + startLineNumber;
const endCol = stats.numLines > 0 ?
stats.lastLineLength :
stats.lastLineLength + startCol;
prevMatchEnd = match.end;
prevMatchEndCol = endCol;
prevMatchEndLine = endLineNumber;
return new Range(startLineNumber, startCol, endLineNumber, endCol);
}));
return createTextSearchResult(uri, fullText, <Range[]>ranges, this.previewOptions);
}
private createTextSearchContext(data: IRgMatch, uri: URI): TextSearchContext[] {
const text = bytesOrTextToString(data.lines);
const startLine = data.line_number;
return text
.replace(/\r?\n$/, '')
.split('\n')
.map((line, i) => {
return {
text: line,
uri,
lineNumber: startLine + i
};
});
}
private onResult(match: TextSearchResult): void {
this.emit('result', match);
}
}
function bytesOrTextToString(obj: any): string {
return obj.bytes ?
Buffer.from(obj.bytes, 'base64').toString() :
obj.text;
}
function getNumLinesAndLastNewlineLength(text: string): { numLines: number; lastLineLength: number } {
const re = /\n/g;
let numLines = 0;
let lastNewlineIdx = -1;
let match: ReturnType<typeof re.exec>;
while (match = re.exec(text)) {
numLines++;
lastNewlineIdx = match.index;
}
const lastLineLength = lastNewlineIdx >= 0 ?
text.length - lastNewlineIdx - 1 :
text.length;
return { numLines, lastLineLength };
}
function getRgArgs(query: TextSearchQuery, options: TextSearchOptions): string[] {
const args = ['--hidden'];
args.push(query.isCaseSensitive ? '--case-sensitive' : '--ignore-case');
const { doubleStarIncludes, otherIncludes } = groupBy(
options.includes,
(include: string) => include.startsWith('**') ? 'doubleStarIncludes' : 'otherIncludes');
if (otherIncludes && otherIncludes.length) {
const uniqueOthers = new Set<string>();
otherIncludes.forEach(other => { uniqueOthers.add(other); });
args.push('-g', '!*');
uniqueOthers
.forEach(otherIncude => {
spreadGlobComponents(otherIncude)
.map(anchorGlob)
.forEach(globArg => {
args.push('-g', globArg);
});
});
}
if (doubleStarIncludes && doubleStarIncludes.length) {
doubleStarIncludes.forEach(globArg => {
args.push('-g', globArg);
});
}
options.excludes
.map(anchorGlob)
.forEach(rgGlob => args.push('-g', `!${rgGlob}`));
if (options.maxFileSize) {
args.push('--max-filesize', options.maxFileSize + '');
}
if (options.useIgnoreFiles) {
if (!options.useParentIgnoreFiles) {
args.push('--no-ignore-parent');
}
} else {
// Don't use .gitignore or .ignore
args.push('--no-ignore');
}
if (options.followSymlinks) {
args.push('--follow');
}
if (options.encoding && options.encoding !== 'utf8') {
args.push('--encoding', options.encoding);
}
// Ripgrep handles -- as a -- arg separator. Only --.
// - is ok, --- is ok, --some-flag is also ok. Need to special case.
if (query.pattern === '--') {
query.isRegExp = true;
query.pattern = '\\-\\-';
}
if (query.isMultiline && !query.isRegExp) {
query.pattern = escapeRegExpCharacters(query.pattern);
query.isRegExp = true;
}
if ((<IExtendedExtensionSearchOptions>options).usePCRE2) {
args.push('--pcre2');
}
// Allow $ to match /r/n
args.push('--crlf');
if (query.isRegExp) {
query.pattern = unicodeEscapesToPCRE2(query.pattern);
args.push('--engine', 'auto');
}
let searchPatternAfterDoubleDashes: Maybe<string>;
if (query.isWordMatch) {
const regexp = createRegExp(query.pattern, !!query.isRegExp, { wholeWord: query.isWordMatch });
const regexpStr = regexp.source.replace(/\\\//g, '/'); // RegExp.source arbitrarily returns escaped slashes. Search and destroy.
args.push('--regexp', regexpStr);
} else if (query.isRegExp) {
let fixedRegexpQuery = fixRegexNewline(query.pattern);
fixedRegexpQuery = fixNewline(fixedRegexpQuery);
args.push('--regexp', fixedRegexpQuery);
} else {
searchPatternAfterDoubleDashes = query.pattern;
args.push('--fixed-strings');
}
args.push('--no-config');
if (!options.useGlobalIgnoreFiles) {
args.push('--no-ignore-global');
}
args.push('--json');
if (query.isMultiline) {
args.push('--multiline');
}
if (options.beforeContext) {
args.push('--before-context', options.beforeContext + '');
}
if (options.afterContext) {
args.push('--after-context', options.afterContext + '');
}
// Folder to search
args.push('--');
if (searchPatternAfterDoubleDashes) {
// Put the query after --, in case the query starts with a dash
args.push(searchPatternAfterDoubleDashes);
}
args.push('.');
return args;
}
/**
* `"foo/*bar/something"` -> `["foo", "foo/*bar", "foo/*bar/something", "foo/*bar/something/**"]`
*/
export function spreadGlobComponents(globArg: string): string[] {
const components = splitGlobAware(globArg, '/');
return components.map((_, i) => components.slice(0, i + 1).join('/'));
}
export function unicodeEscapesToPCRE2(pattern: string): string {
// Match \u1234
const unicodePattern = /((?:[^\\]|^)(?:\\\\)*)\\u([a-z0-9]{4})/gi;
while (pattern.match(unicodePattern)) {
pattern = pattern.replace(unicodePattern, `$1\\x{$2}`);
}
// Match \u{1234}
// \u with 5-6 characters will be left alone because \x only takes 4 characters.
const unicodePatternWithBraces = /((?:[^\\]|^)(?:\\\\)*)\\u\{([a-z0-9]{4})\}/gi;
while (pattern.match(unicodePatternWithBraces)) {
pattern = pattern.replace(unicodePatternWithBraces, `$1\\x{$2}`);
}
return pattern;
}
export interface IRgMessage {
type: 'match' | 'context' | string;
data: IRgMatch;
}
export interface IRgMatch {
path: IRgBytesOrText;
lines: IRgBytesOrText;
line_number: number;
absolute_offset: number;
submatches: IRgSubmatch[];
}
export interface IRgSubmatch {
match: IRgBytesOrText;
start: number;
end: number;
}
export type IRgBytesOrText = { bytes: string } | { text: string };
const isLookBehind = (node: ReAST.Node) => node.type === 'Assertion' && node.kind === 'lookbehind';
export function fixRegexNewline(pattern: string): string {
// we parse the pattern anew each tiem
let re: ReAST.Pattern;
try {
re = new RegExpParser().parsePattern(pattern);
} catch {
return pattern;
}
let output = '';
let lastEmittedIndex = 0;
const replace = (start: number, end: number, text: string) => {
output += pattern.slice(lastEmittedIndex, start) + text;
lastEmittedIndex = end;
};
const context: ReAST.Node[] = [];
const visitor = new RegExpVisitor({
onCharacterEnter(char) {
if (char.raw !== '\\n') {
return;
}
const parent = context[0];
if (!parent) {
// simple char, \n -> \r?\n
replace(char.start, char.end, '\\r?\\n');
} else if (context.some(isLookBehind)) {
// no-op in a lookbehind, see #100569
} else if (parent.type === 'CharacterClass') {
if (parent.negate) {
// negative bracket expr, [^a-z\n] -> (?![a-z]|\r?\n)
const otherContent = pattern.slice(parent.start + 2, char.start) + pattern.slice(char.end, parent.end - 1);
if (parent.parent?.type === 'Quantifier') {
// If quantified, we can't use a negative lookahead in a quantifier.
// But `.` already doesn't match new lines, so we can just use that
// (with any other negations) instead.
replace(parent.start, parent.end, otherContent ? `[^${otherContent}]` : '.');
} else {
replace(parent.start, parent.end, '(?!\\r?\\n' + (otherContent ? `|[${otherContent}]` : '') + ')');
}
} else {
// positive bracket expr, [a-z\n] -> (?:[a-z]|\r?\n)
const otherContent = pattern.slice(parent.start + 1, char.start) + pattern.slice(char.end, parent.end - 1);
replace(parent.start, parent.end, otherContent === '' ? '\\r?\\n' : `(?:[${otherContent}]|\\r?\\n)`);
}
} else if (parent.type === 'Quantifier') {
replace(char.start, char.end, '(?:\\r?\\n)');
}
},
onQuantifierEnter(node) {
context.unshift(node);
},
onQuantifierLeave() {
context.shift();
},
onCharacterClassRangeEnter(node) {
context.unshift(node);
},
onCharacterClassRangeLeave() {
context.shift();
},
onCharacterClassEnter(node) {
context.unshift(node);
},
onCharacterClassLeave() {
context.shift();
},
onAssertionEnter(node) {
if (isLookBehind(node)) {
context.push(node);
}
},
onAssertionLeave(node) {
if (context[0] === node) {
context.shift();
}
},
});
visitor.visit(re);
output += pattern.slice(lastEmittedIndex);
return output;
}
export function fixNewline(pattern: string): string {
return pattern.replace(/\n/g, '\\r?\\n');
}