Skip to content

Commit 656912c

Browse files
authored
on data/ buffer change, update the match count (#3752)
1 parent 7fa32af commit 656912c

File tree

1 file changed

+29
-30
lines changed

1 file changed

+29
-30
lines changed

addons/xterm-addon-search/src/SearchAddon.ts

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export interface ISearchOptions {
1212
caseSensitive?: boolean;
1313
incremental?: boolean;
1414
decorations?: ISearchDecorationOptions;
15+
noScroll?: boolean;
1516
}
1617

1718
interface ISearchDecorationOptions {
@@ -56,6 +57,7 @@ export class SearchAddon implements ITerminalAddon {
5657
private _resultDecorations: Map<number, IDecoration[]> | undefined;
5758
private _searchResults: Map<string, ISearchResult> | undefined;
5859
private _onDataDisposable: IDisposable | undefined;
60+
private _onResizeDisposable: IDisposable | undefined;
5961
private _lastSearchOptions: ISearchOptions | undefined;
6062
private _highlightTimeout: number | undefined;
6163
/**
@@ -75,31 +77,26 @@ export class SearchAddon implements ITerminalAddon {
7577

7678
public activate(terminal: Terminal): void {
7779
this._terminal = terminal;
78-
this._onDataDisposable = this._terminal.onData(() => {
79-
if (this._highlightTimeout) {
80-
window.clearTimeout(this._highlightTimeout);
81-
}
82-
if (this._cachedSearchTerm && this._lastSearchOptions?.decorations) {
83-
this._highlightTimeout = setTimeout(() => {
84-
this._highlightAllMatches(this._cachedSearchTerm!, { ...this._lastSearchOptions, incremental: true });
85-
}, 200);
86-
}
87-
});
88-
this._terminal.onResize(() => {
89-
if (this._highlightTimeout) {
90-
window.clearTimeout(this._highlightTimeout);
91-
}
92-
if (this._cachedSearchTerm && this._lastSearchOptions?.decorations) {
93-
this._highlightTimeout = setTimeout(() => {
94-
this._highlightAllMatches(this._cachedSearchTerm!, { ...this._lastSearchOptions, incremental: true });
95-
}, 200);
96-
}
97-
});
80+
this._onDataDisposable = this._terminal.onData(() => this._updateMatches());
81+
this._onResizeDisposable = this._terminal.onResize(() => this._updateMatches());
82+
}
83+
84+
private _updateMatches(): void {
85+
if (this._highlightTimeout) {
86+
window.clearTimeout(this._highlightTimeout);
87+
}
88+
if (this._cachedSearchTerm && this._lastSearchOptions?.decorations) {
89+
this._highlightTimeout = setTimeout(() => {
90+
this.findPrevious(this._cachedSearchTerm!, { ...this._lastSearchOptions, incremental: true, noScroll: true });
91+
this._onDidChangeResults.fire({ resultIndex: this._searchResults ? this._searchResults.size - 1 : -1, resultCount: this._searchResults ? this._searchResults.size : -1 });
92+
}, 200);
93+
}
9894
}
9995

10096
public dispose(): void {
10197
this.clearDecorations();
10298
this._onDataDisposable?.dispose();
99+
this._onResizeDisposable?.dispose();
103100
}
104101

105102
public clearDecorations(internal?: boolean): void {
@@ -299,7 +296,7 @@ export class SearchAddon implements ITerminalAddon {
299296
}
300297
}
301298
// Set selection and scroll if a result was found
302-
return this._selectResult(result, searchOptions?.decorations);
299+
return this._selectResult(result, searchOptions?.decorations, searchOptions?.noScroll);
303300
}
304301
/**
305302
* Find the previous instance of the term, then scroll to and select it. If it
@@ -423,7 +420,7 @@ export class SearchAddon implements ITerminalAddon {
423420
if (!result && currentSelection) return true;
424421

425422
// Set selection and scroll if a result was found
426-
return this._selectResult(result, searchOptions?.decorations);
423+
return this._selectResult(result, searchOptions?.decorations, searchOptions?.noScroll);
427424
}
428425

429426
/**
@@ -660,7 +657,7 @@ export class SearchAddon implements ITerminalAddon {
660657
* @param result The result to select.
661658
* @return Whether a result was selected.
662659
*/
663-
private _selectResult(result: ISearchResult | undefined, decorations?: ISearchDecorationOptions): boolean {
660+
private _selectResult(result: ISearchResult | undefined, decorations?: ISearchDecorationOptions, noScroll?: boolean): boolean {
664661
const terminal = this._terminal!;
665662
this._selectedDecoration?.dispose();
666663
if (!result) {
@@ -679,16 +676,18 @@ export class SearchAddon implements ITerminalAddon {
679676
color: decorations.activeMatchColorOverviewRuler
680677
}
681678
});
682-
this._selectedDecoration?.onRender((e) => this._applyStyles(e, decorations.activeMatchBackground, decorations.activeMatchBorder, result));
679+
this._selectedDecoration?.onRender((e) => this._applyStyles(e, decorations.activeMatchBackground, decorations.activeMatchBorder));
683680
this._selectedDecoration?.onDispose(() => marker.dispose());
684681
}
685682
}
686683

684+
if (!noScroll) {
687685
// If it is not in the viewport then we scroll else it just gets selected
688-
if (result.row >= (terminal.buffer.active.viewportY + terminal.rows) || result.row < terminal.buffer.active.viewportY) {
689-
let scroll = result.row - terminal.buffer.active.viewportY;
690-
scroll -= Math.floor(terminal.rows / 2);
691-
terminal.scrollLines(scroll);
686+
if (result.row >= (terminal.buffer.active.viewportY + terminal.rows) || result.row < terminal.buffer.active.viewportY) {
687+
let scroll = result.row - terminal.buffer.active.viewportY;
688+
scroll -= Math.floor(terminal.rows / 2);
689+
terminal.scrollLines(scroll);
690+
}
692691
}
693692
return true;
694693
}
@@ -701,7 +700,7 @@ export class SearchAddon implements ITerminalAddon {
701700
* @param result the search result associated with the decoration
702701
* @returns
703702
*/
704-
private _applyStyles(element: HTMLElement, backgroundColor: string | undefined, borderColor: string | undefined, result: ISearchResult): void {
703+
private _applyStyles(element: HTMLElement, backgroundColor: string | undefined, borderColor: string | undefined): void {
705704
if (element.clientWidth <= 0) {
706705
return;
707706
}
@@ -736,7 +735,7 @@ export class SearchAddon implements ITerminalAddon {
736735
color: decorations.matchOverviewRuler, position: 'center'
737736
}
738737
});
739-
findResultDecoration?.onRender((e) => this._applyStyles(e, decorations.matchBackground, decorations.matchBorder, result));
738+
findResultDecoration?.onRender((e) => this._applyStyles(e, decorations.matchBackground, decorations.matchBorder));
740739
findResultDecoration?.onDispose(() => marker.dispose());
741740
return findResultDecoration;
742741
}

0 commit comments

Comments
 (0)