@@ -322,15 +322,16 @@ export class TextSearchProvider implements vscode.TextSearchProvider {
322
322
323
323
const uri = DocumentContentProvider . getUri ( file . doc , "" , "" , true , options . folder ) ;
324
324
const content = decoder . decode ( await vscode . workspace . fs . readFile ( uri ) ) . split ( "\n" ) ;
325
+ const contentLength = content . length ;
325
326
// Find all lines that we have matches on
326
327
const lines = file . matches
327
328
. map ( ( match : SearchMatch ) => searchMatchToLine ( content , match , file . doc , api . configName ) )
328
329
. filter ( notNull ) ;
329
- // Filter out duplicates and compute all matches for each one
330
- [ ...new Set ( lines ) ] . forEach ( ( line , _index , matchedLines ) => {
330
+ // Remove duplicates and make them quickly searchable
331
+ const matchedLines = new Set ( lines ) ;
332
+ // Compute all matches for each one
333
+ matchedLines . forEach ( ( line ) => {
331
334
const text = content [ line ] ;
332
- const previewFrom = Math . max ( line - ( options . beforeContext || 0 ) , 0 ) ;
333
- const previewTo = Math . min ( line + ( options . afterContext || 0 ) , content . length - 1 ) ;
334
335
const regex = new RegExp (
335
336
query . isRegExp ? query . pattern : query . pattern . replace ( / [ - [ \] { } ( ) * + ? . , \\ ^ $ | # \s ] / g, "\\$&" ) ,
336
337
query . isCaseSensitive ? "g" : "gi"
@@ -346,14 +347,17 @@ export class TextSearchProvider implements vscode.TextSearchProvider {
346
347
counter ++ ;
347
348
}
348
349
if ( matchRanges . length && previewRanges . length ) {
349
- // Add preceding context lines that aren't result lines
350
- for ( let i = previewFrom ; i < line ; i ++ ) {
351
- if ( ! matchedLines . includes ( i ) ) {
352
- progress . report ( {
353
- uri,
354
- text : content [ i ] ,
355
- lineNumber : i + 1 ,
356
- } ) ;
350
+ if ( options . beforeContext ) {
351
+ // Add preceding context lines that aren't themselves result lines
352
+ const previewFrom = Math . max ( line - options . beforeContext , 0 ) ;
353
+ for ( let i = previewFrom ; i < line ; i ++ ) {
354
+ if ( ! matchedLines . has ( i ) ) {
355
+ progress . report ( {
356
+ uri,
357
+ text : content [ i ] ,
358
+ lineNumber : i + 1 ,
359
+ } ) ;
360
+ }
357
361
}
358
362
}
359
363
progress . report ( {
@@ -364,14 +368,17 @@ export class TextSearchProvider implements vscode.TextSearchProvider {
364
368
matches : previewRanges ,
365
369
} ,
366
370
} ) ;
367
- // Add following context lines that aren't result lines
368
- for ( let i = line + 1 ; i <= previewTo ; i ++ ) {
369
- if ( ! matchedLines . includes ( i ) ) {
370
- progress . report ( {
371
- uri,
372
- text : content [ i ] ,
373
- lineNumber : i + 1 ,
374
- } ) ;
371
+ if ( options . afterContext ) {
372
+ // Add following context lines that aren't themselves result lines
373
+ const previewTo = Math . min ( line + options . afterContext , contentLength - 1 ) ;
374
+ for ( let i = line + 1 ; i <= previewTo ; i ++ ) {
375
+ if ( ! matchedLines . has ( i ) ) {
376
+ progress . report ( {
377
+ uri,
378
+ text : content [ i ] ,
379
+ lineNumber : i + 1 ,
380
+ } ) ;
381
+ }
375
382
}
376
383
}
377
384
}
0 commit comments