From fa92e6074de025084bda35a9bd1517e9e6078428 Mon Sep 17 00:00:00 2001 From: Megan Rogge Date: Tue, 26 Apr 2022 15:45:30 -0700 Subject: [PATCH] search fixes (#3748) --- addons/xterm-addon-search/src/SearchAddon.ts | 47 +++++++++++++------ .../typings/xterm-addon-search.d.ts | 6 +-- 2 files changed, 36 insertions(+), 17 deletions(-) diff --git a/addons/xterm-addon-search/src/SearchAddon.ts b/addons/xterm-addon-search/src/SearchAddon.ts index c1de5653..ef3ba2fc 100644 --- a/addons/xterm-addon-search/src/SearchAddon.ts +++ b/addons/xterm-addon-search/src/SearchAddon.ts @@ -85,6 +85,16 @@ export class SearchAddon implements ITerminalAddon { }, 200); } }); + this._terminal.onResize(() => { + if (this._highlightTimeout) { + window.clearTimeout(this._highlightTimeout); + } + if (this._cachedSearchTerm && this._lastSearchOptions?.decorations) { + this._highlightTimeout = setTimeout(() => { + this._highlightAllMatches(this._cachedSearchTerm!, { ...this._lastSearchOptions, incremental: true }); + }, 200); + } + }); } public dispose(): void { @@ -101,10 +111,8 @@ export class SearchAddon implements ITerminalAddon { } }); this._resultDecorations?.clear(); - this._cachedSearchTerm = undefined; this._searchResults = undefined; this._resultDecorations = undefined; - this._resultIndex = undefined; } /** @@ -120,9 +128,11 @@ export class SearchAddon implements ITerminalAddon { } this._lastSearchOptions = searchOptions; if (searchOptions?.decorations) { - this._highlightAllMatches(term, searchOptions); + if (this._resultIndex || this._cachedSearchTerm && term !== this._cachedSearchTerm) { + this._highlightAllMatches(term, searchOptions); + } } - return this._fireResults(this._findNextAndSelect(term, searchOptions), searchOptions); + return this._fireResults(term, this._findNextAndSelect(term, searchOptions), searchOptions); } private _highlightAllMatches(term: string, searchOptions: ISearchOptions): void { @@ -149,9 +159,9 @@ export class SearchAddon implements ITerminalAddon { result.col + result.term.length >= this._terminal.cols ? 0 : result.col + 1, searchOptions ); - if (this._searchResults.size > 2000) { + if (this._searchResults.size > 1000) { this.clearDecorations(); - this._resultIndex = -1; + this._resultIndex = undefined; return; } } @@ -163,9 +173,6 @@ export class SearchAddon implements ITerminalAddon { resultDecorations.set(resultDecoration.marker.line, decorationsForLine); } }); - if (this._searchResults.size > 0) { - this._cachedSearchTerm = term; - } } private _find(term: string, startRow: number, startCol: number, searchOptions?: ISearchOptions): ISearchResult | undefined { @@ -210,9 +217,15 @@ export class SearchAddon implements ITerminalAddon { if (!this._terminal || !term || term.length === 0) { this._terminal?.clearSelection(); this.clearDecorations(); + this._cachedSearchTerm = undefined; + this._resultIndex = -1; return false; } + if (this._cachedSearchTerm !== term) { + this._resultIndex = undefined; + this._terminal.clearSelection(); + } let startCol = 0; let startRow = 0; @@ -278,7 +291,6 @@ export class SearchAddon implements ITerminalAddon { } } } - // Set selection and scroll if a result was found return this._selectResult(result, searchOptions?.decorations); } @@ -294,13 +306,13 @@ export class SearchAddon implements ITerminalAddon { throw new Error('Cannot use addon until it has been loaded'); } this._lastSearchOptions = searchOptions; - if (searchOptions?.decorations) { + if (searchOptions?.decorations && (this._resultIndex || term !== this._cachedSearchTerm)) { this._highlightAllMatches(term, searchOptions); } - return this._fireResults(this._findPreviousAndSelect(term, searchOptions), searchOptions); + return this._fireResults(term, this._findPreviousAndSelect(term, searchOptions), searchOptions); } - private _fireResults(found: boolean, searchOptions?: ISearchOptions): boolean { + private _fireResults(term: string, found: boolean, searchOptions?: ISearchOptions): boolean { if (searchOptions?.decorations) { if (found && this._resultIndex !== undefined && this._searchResults?.size) { this._onDidChangeResults.fire({ resultIndex: this._resultIndex, resultCount: this._searchResults.size }); @@ -310,6 +322,7 @@ export class SearchAddon implements ITerminalAddon { this._onDidChangeResults.fire(undefined); } } + this._cachedSearchTerm = term; return found; } @@ -322,9 +335,15 @@ export class SearchAddon implements ITerminalAddon { result = undefined; this._terminal?.clearSelection(); this.clearDecorations(); + this._resultIndex = -1; return false; } + if (this._cachedSearchTerm !== term) { + this._resultIndex = undefined; + this._terminal.clearSelection(); + } + let startRow = this._terminal.buffer.active.baseY + this._terminal.rows; let startCol = this._terminal.cols; const isReverseSearch = true; @@ -383,7 +402,7 @@ export class SearchAddon implements ITerminalAddon { } if (this._searchResults) { - if (this._resultIndex === undefined) { + if (this._resultIndex === undefined || this._resultIndex < 0) { this._resultIndex = this._searchResults?.size - 1; } else { this._resultIndex--; diff --git a/addons/xterm-addon-search/typings/xterm-addon-search.d.ts b/addons/xterm-addon-search/typings/xterm-addon-search.d.ts index 5dafb449..4d683db0 100644 --- a/addons/xterm-addon-search/typings/xterm-addon-search.d.ts +++ b/addons/xterm-addon-search/typings/xterm-addon-search.d.ts @@ -113,9 +113,9 @@ declare module 'xterm-addon-search' { /** * When decorations are enabled, fires when - * the search results or the selected result changes, - * returning undefined if there are no matches. - * -1 is returned for resultCount/resultIndex when the threshold of 2k results + * the search results change. + * @returns -1 if there are no matches and + * @returns undefined when the threshold of 1k results * is exceeded and decorations are disposed of. */ readonly onDidChangeResults: IEvent<{ resultIndex: number, resultCount: number } | undefined>;