Skip to content

Commit b8759fe

Browse files
committed
Converted cancelSearchSignal to a regular global variable boolean.
Moved constants to an Enum
1 parent b6e0be0 commit b8759fe

File tree

1 file changed

+48
-44
lines changed

1 file changed

+48
-44
lines changed

addons/addon-search/src/SearchAddon.ts

Lines changed: 48 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -48,51 +48,56 @@ interface IHighlight extends IDisposable {
4848
decoration: IDecoration;
4949
match: ISearchResult;
5050
}
51-
// just a wrapper around boolean so we can keep a reference to boolean value
52-
// to make it clear: the goal is to pass a boolean by reference not value
53-
interface ICancelSearchSignal{
54-
value: boolean;
55-
}
5651

5752
type ChunkSearchDirection = 'up'|'down';
5853

5954
const NON_WORD_CHARACTERS = ' ~!@#$%^&*()+`-=[]{}|\\;:"\',./<>?';
60-
const DEFAULT_HIGHLIGHT_LIMIT = 1000;
6155

62-
export class SearchAddon extends Disposable implements ITerminalAddon , ISearchApi {
63-
private _terminal: Terminal | undefined;
64-
private _cachedSearchTerm: string | undefined;
65-
private _highlightedLines: Set<number> = new Set();
66-
private _currentMatchIndex: number = 0;
67-
private _matches: ISearchResult[] = [];
68-
private _matchesWithHighlightApplied: IHighlight[] = [];
69-
private _selectedDecoration: MutableDisposable<IHighlight> = this._register(new MutableDisposable());
70-
private _highlightLimit: number;
71-
private _searchOptions: ISearchOptions | undefined;
72-
private _debounceTimeout: number | undefined;
73-
private _searchCompleted: boolean = true;
74-
private _cancelSearchSignal: ICancelSearchSignal = { value:false };
56+
const enum Performance {
57+
58+
DEFAULT_HIGHLIGHT_LIMIT = 1000,
59+
7560
/**
7661
* Number of matches in each chunk
7762
*/
78-
private _chunkSize: number = 200;
63+
CHUNK_SIZE = 200,
64+
7965
/**
8066
* Time in ms
8167
* 1 ms seems to work fine as we just need to let other parts of the code to take over
8268
* and return here when their work is done
8369
*/
84-
private _timeBetweenChunkOperations = 1;
70+
TIME_BETWEEN_CHUNK_OPERATIONS = 1,
8571

8672
/**
8773
* This should be high enough so not to trigger a lot of searches
8874
* and subsequently a lot of canceled searches which clean up their own
8975
* decorations and cause flickers
9076
*/
91-
private _debounceTimeWindow = 300;
77+
DEBOUNCE_TIME_WINDOW = 300,
78+
9279
/**
9380
* Using this mainly for resizing event
9481
*/
95-
private _longerDebounceTimeWindow = 1000;
82+
LONGER_DEBOUNCE_TIME_WINDOW = 1000,
83+
}
84+
85+
86+
export class SearchAddon extends Disposable implements ITerminalAddon , ISearchApi {
87+
private _terminal: Terminal | undefined;
88+
private _cachedSearchTerm: string | undefined;
89+
private _highlightedLines: Set<number> = new Set();
90+
private _currentMatchIndex: number = 0;
91+
private _matches: ISearchResult[] = [];
92+
private _matchesWithHighlightApplied: IHighlight[] = [];
93+
private _selectedDecoration: MutableDisposable<IHighlight> = this._register(new MutableDisposable());
94+
private _highlightLimit: number;
95+
private _searchOptions: ISearchOptions | undefined;
96+
private _debounceTimeout: number | undefined;
97+
private _searchCompleted: boolean = true;
98+
private _cancelSearchSignal: boolean = false;
99+
100+
96101
/**
97102
* translateBufferLineToStringWithWrap is a fairly expensive call.
98103
* We memoize the calls into an array that has a time based ttl.
@@ -106,7 +111,7 @@ export class SearchAddon extends Disposable implements ITerminalAddon , ISearchA
106111
constructor(options?: Partial<ISearchAddonOptions>) {
107112
super();
108113

109-
this._highlightLimit = options?.highlightLimit ?? DEFAULT_HIGHLIGHT_LIMIT;
114+
this._highlightLimit = options?.highlightLimit ?? Performance.DEFAULT_HIGHLIGHT_LIMIT;
110115
}
111116

112117
public activate(terminal: Terminal): void {
@@ -151,11 +156,11 @@ export class SearchAddon extends Disposable implements ITerminalAddon , ISearchA
151156
if (matchesWithHighlightApplied.length>0){
152157
this._iterateToDisposeDecoration(matchesWithHighlightApplied);
153158
}
154-
},this._timeBetweenChunkOperations);
159+
},Performance.TIME_BETWEEN_CHUNK_OPERATIONS);
155160
}
156161
private _chunkDisposeDecoration(matchesWithHighlightApplied: IHighlight[]): void{
157162

158-
const numberOfElementsToDispose = this._chunkSize > matchesWithHighlightApplied.length ? matchesWithHighlightApplied.length : this._chunkSize;
163+
const numberOfElementsToDispose = Performance.CHUNK_SIZE > matchesWithHighlightApplied.length ? matchesWithHighlightApplied.length : CHUNK_SIZE;
159164

160165
for (let i=0;i<numberOfElementsToDispose;i++){
161166
matchesWithHighlightApplied.pop()?.dispose();
@@ -184,7 +189,7 @@ export class SearchAddon extends Disposable implements ITerminalAddon , ISearchA
184189
}
185190

186191
if (!term || term.length === 0) {
187-
this._cancelSearchSignal.value = true;
192+
this._cancelSearchSignal = true;
188193
this._searchCompleted=true;
189194
window.clearTimeout(this._debounceTimeout);
190195
this.clearDecorations();
@@ -203,7 +208,7 @@ export class SearchAddon extends Disposable implements ITerminalAddon , ISearchA
203208

204209
if (freshSearch){
205210

206-
this._cancelSearchSignal.value = true;
211+
this._cancelSearchSignal = true;
207212
window.clearTimeout(this._debounceTimeout);
208213

209214
this._debounceTimeout = setTimeout(()=>{
@@ -212,15 +217,15 @@ export class SearchAddon extends Disposable implements ITerminalAddon , ISearchA
212217
if (wasLastSearchRegex===true){
213218
this._destroyLinesCache();
214219
}
215-
this._cancelSearchSignal = { value: false };
220+
this._cancelSearchSignal = false;
216221
this._searchCompleted = false;
217222
this.clearDecorations(true);
218223
this._matches = [];
219224
this._currentMatchIndex = -1;
220225

221-
this._findAllMatches(term,this._cancelSearchSignal);
226+
this._findAllMatches(term);
222227

223-
},writeBufferOrWindowResizeEvent === true ? this._longerDebounceTimeWindow : this._debounceTimeWindow);
228+
},writeBufferOrWindowResizeEvent === true ? Performance.LONGER_DEBOUNCE_TIME_WINDOW : Performance.DEBOUNCE_TIME_WINDOW);
224229

225230
}
226231

@@ -265,10 +270,9 @@ export class SearchAddon extends Disposable implements ITerminalAddon , ISearchA
265270
this._fireResults();
266271
}
267272

268-
private _findAllMatches(term: string,cancelSearchSignal: ICancelSearchSignal): void {
269-
273+
private _findAllMatches(term: string): void {
270274

271-
const chunkSearchIterator = this._chunkSearchGenerator(term,cancelSearchSignal);
275+
const chunkSearchIterator = this._chunkSearchGenerator(term);
272276
this._iterate(chunkSearchIterator,0);
273277
}
274278

@@ -315,14 +319,14 @@ export class SearchAddon extends Disposable implements ITerminalAddon , ISearchA
315319
this._fireResults();
316320
}
317321

318-
},this._timeBetweenChunkOperations);
322+
},Performance.TIME_BETWEEN_CHUNK_OPERATIONS);
319323
}
320324
private _fireResults(): void {
321325
if (this._searchOptions?.decorations){
322326
this._onDidChangeResults.fire({ resultIndex:this._currentMatchIndex, resultCount: this._matches.length,searchCompleted: this._searchCompleted });
323327
}
324328
}
325-
private *_chunkSearchGenerator(term: string,cancelSearchSignal: ICancelSearchSignal): Generator<{direction: string,chunkSize: number}>{
329+
private *_chunkSearchGenerator(term: string): Generator<{direction: string,chunkSize: number}>{
326330

327331
const rowIndex = this._terminal!.buffer.active.viewportY;
328332

@@ -340,7 +344,7 @@ export class SearchAddon extends Disposable implements ITerminalAddon , ISearchA
340344

341345
while (downDirectionLastResult !== undefined || upDirectionLastResult !== undefined) {
342346

343-
if (cancelSearchSignal.value === true){
347+
if (this._cancelSearchSignal === true){
344348
return false;
345349
}
346350

@@ -349,7 +353,7 @@ export class SearchAddon extends Disposable implements ITerminalAddon , ISearchA
349353
// we need two variable to check for yield on exceeding max row scans
350354
// didNotYieldForThisManyRows for the current exection
351355
// and usedForYield for the next time we are given execution
352-
if (downDirectionLastResult.didNotYieldForThisManyRows < this._chunkSize){
356+
if (downDirectionLastResult.didNotYieldForThisManyRows < Performance.CHUNK_SIZE){
353357
if (downDirectionLastResult.usedForYield === false){
354358
currentChunkMatches.push(downDirectionLastResult);
355359
}
@@ -369,7 +373,7 @@ export class SearchAddon extends Disposable implements ITerminalAddon , ISearchA
369373

370374
} else if (upDirectionLastResult !== undefined && searchDirection === 'up'){
371375

372-
if (upDirectionLastResult.didNotYieldForThisManyRows < this._chunkSize){
376+
if (upDirectionLastResult.didNotYieldForThisManyRows < Performance.CHUNK_SIZE){
373377
if (upDirectionLastResult.usedForYield === false){
374378
currentChunkMatches.push(upDirectionLastResult);
375379
}
@@ -407,7 +411,7 @@ export class SearchAddon extends Disposable implements ITerminalAddon , ISearchA
407411
}
408412

409413
if (
410-
(currentChunkMatches.length > 0 && currentChunkMatches.length % this._chunkSize === 0) ||
414+
(currentChunkMatches.length > 0 && currentChunkMatches.length % Performance.CHUNK_SIZE === 0) ||
411415
(downDirectionLastResult === undefined && searchDirection === 'down') ||
412416
(upDirectionLastResult === undefined && searchDirection ==='up') ||
413417
yieldForReachingMaxRowScans
@@ -481,8 +485,8 @@ export class SearchAddon extends Disposable implements ITerminalAddon , ISearchA
481485
}
482486

483487
numberOfRowsSearched++;
484-
if (numberOfRowsSearched + didNotYieldForThisManyRows >= this._chunkSize){
485-
return { term:'-1',row: y, col: 0 ,size:-1, didNotYieldForThisManyRows: this._chunkSize,usedForYield: true };
488+
if (numberOfRowsSearched + didNotYieldForThisManyRows >= Performance.CHUNK_SIZE){
489+
return { term:'-1',row: y, col: 0 ,size:-1, didNotYieldForThisManyRows: Performance.CHUNK_SIZE,usedForYield: true };
486490
}
487491
}
488492
}
@@ -508,8 +512,8 @@ export class SearchAddon extends Disposable implements ITerminalAddon , ISearchA
508512
}
509513
}
510514
numberOfRowsSearched++;
511-
if (numberOfRowsSearched + didNotYieldForThisManyRows >= this._chunkSize){
512-
return { term:'-1', row: y, col: this._terminal.cols, size: -1, didNotYieldForThisManyRows: this._chunkSize,usedForYield: true };
515+
if (numberOfRowsSearched + didNotYieldForThisManyRows >= Performance.CHUNK_SIZE){
516+
return { term:'-1', row: y, col: this._terminal.cols, size: -1, didNotYieldForThisManyRows: Performance.CHUNK_SIZE,usedForYield: true };
513517
}
514518
}
515519
}

0 commit comments

Comments
 (0)