on data/ buffer change, update the match count (#3752)

This commit is contained in:
Megan Rogge
2022-04-27 19:10:00 +00:00
committed by GitHub
parent 7fa32af051
commit 656912c934
+29 -30
View File
@@ -12,6 +12,7 @@ export interface ISearchOptions {
caseSensitive?: boolean; caseSensitive?: boolean;
incremental?: boolean; incremental?: boolean;
decorations?: ISearchDecorationOptions; decorations?: ISearchDecorationOptions;
noScroll?: boolean;
} }
interface ISearchDecorationOptions { interface ISearchDecorationOptions {
@@ -56,6 +57,7 @@ export class SearchAddon implements ITerminalAddon {
private _resultDecorations: Map<number, IDecoration[]> | undefined; private _resultDecorations: Map<number, IDecoration[]> | undefined;
private _searchResults: Map<string, ISearchResult> | undefined; private _searchResults: Map<string, ISearchResult> | undefined;
private _onDataDisposable: IDisposable | undefined; private _onDataDisposable: IDisposable | undefined;
private _onResizeDisposable: IDisposable | undefined;
private _lastSearchOptions: ISearchOptions | undefined; private _lastSearchOptions: ISearchOptions | undefined;
private _highlightTimeout: number | undefined; private _highlightTimeout: number | undefined;
/** /**
@@ -75,31 +77,26 @@ export class SearchAddon implements ITerminalAddon {
public activate(terminal: Terminal): void { public activate(terminal: Terminal): void {
this._terminal = terminal; this._terminal = terminal;
this._onDataDisposable = this._terminal.onData(() => { this._onDataDisposable = this._terminal.onData(() => this._updateMatches());
if (this._highlightTimeout) { this._onResizeDisposable = this._terminal.onResize(() => this._updateMatches());
window.clearTimeout(this._highlightTimeout); }
}
if (this._cachedSearchTerm && this._lastSearchOptions?.decorations) { private _updateMatches(): void {
this._highlightTimeout = setTimeout(() => { if (this._highlightTimeout) {
this._highlightAllMatches(this._cachedSearchTerm!, { ...this._lastSearchOptions, incremental: true }); window.clearTimeout(this._highlightTimeout);
}, 200); }
} if (this._cachedSearchTerm && this._lastSearchOptions?.decorations) {
}); this._highlightTimeout = setTimeout(() => {
this._terminal.onResize(() => { this.findPrevious(this._cachedSearchTerm!, { ...this._lastSearchOptions, incremental: true, noScroll: true });
if (this._highlightTimeout) { this._onDidChangeResults.fire({ resultIndex: this._searchResults ? this._searchResults.size - 1 : -1, resultCount: this._searchResults ? this._searchResults.size : -1 });
window.clearTimeout(this._highlightTimeout); }, 200);
} }
if (this._cachedSearchTerm && this._lastSearchOptions?.decorations) {
this._highlightTimeout = setTimeout(() => {
this._highlightAllMatches(this._cachedSearchTerm!, { ...this._lastSearchOptions, incremental: true });
}, 200);
}
});
} }
public dispose(): void { public dispose(): void {
this.clearDecorations(); this.clearDecorations();
this._onDataDisposable?.dispose(); this._onDataDisposable?.dispose();
this._onResizeDisposable?.dispose();
} }
public clearDecorations(internal?: boolean): void { public clearDecorations(internal?: boolean): void {
@@ -299,7 +296,7 @@ export class SearchAddon implements ITerminalAddon {
} }
} }
// Set selection and scroll if a result was found // Set selection and scroll if a result was found
return this._selectResult(result, searchOptions?.decorations); return this._selectResult(result, searchOptions?.decorations, searchOptions?.noScroll);
} }
/** /**
* Find the previous instance of the term, then scroll to and select it. If it * Find the previous instance of the term, then scroll to and select it. If it
@@ -423,7 +420,7 @@ export class SearchAddon implements ITerminalAddon {
if (!result && currentSelection) return true; if (!result && currentSelection) return true;
// Set selection and scroll if a result was found // Set selection and scroll if a result was found
return this._selectResult(result, searchOptions?.decorations); return this._selectResult(result, searchOptions?.decorations, searchOptions?.noScroll);
} }
/** /**
@@ -660,7 +657,7 @@ export class SearchAddon implements ITerminalAddon {
* @param result The result to select. * @param result The result to select.
* @return Whether a result was selected. * @return Whether a result was selected.
*/ */
private _selectResult(result: ISearchResult | undefined, decorations?: ISearchDecorationOptions): boolean { private _selectResult(result: ISearchResult | undefined, decorations?: ISearchDecorationOptions, noScroll?: boolean): boolean {
const terminal = this._terminal!; const terminal = this._terminal!;
this._selectedDecoration?.dispose(); this._selectedDecoration?.dispose();
if (!result) { if (!result) {
@@ -679,16 +676,18 @@ export class SearchAddon implements ITerminalAddon {
color: decorations.activeMatchColorOverviewRuler color: decorations.activeMatchColorOverviewRuler
} }
}); });
this._selectedDecoration?.onRender((e) => this._applyStyles(e, decorations.activeMatchBackground, decorations.activeMatchBorder, result)); this._selectedDecoration?.onRender((e) => this._applyStyles(e, decorations.activeMatchBackground, decorations.activeMatchBorder));
this._selectedDecoration?.onDispose(() => marker.dispose()); this._selectedDecoration?.onDispose(() => marker.dispose());
} }
} }
if (!noScroll) {
// If it is not in the viewport then we scroll else it just gets selected // If it is not in the viewport then we scroll else it just gets selected
if (result.row >= (terminal.buffer.active.viewportY + terminal.rows) || result.row < terminal.buffer.active.viewportY) { if (result.row >= (terminal.buffer.active.viewportY + terminal.rows) || result.row < terminal.buffer.active.viewportY) {
let scroll = result.row - terminal.buffer.active.viewportY; let scroll = result.row - terminal.buffer.active.viewportY;
scroll -= Math.floor(terminal.rows / 2); scroll -= Math.floor(terminal.rows / 2);
terminal.scrollLines(scroll); terminal.scrollLines(scroll);
}
} }
return true; return true;
} }
@@ -701,7 +700,7 @@ export class SearchAddon implements ITerminalAddon {
* @param result the search result associated with the decoration * @param result the search result associated with the decoration
* @returns * @returns
*/ */
private _applyStyles(element: HTMLElement, backgroundColor: string | undefined, borderColor: string | undefined, result: ISearchResult): void { private _applyStyles(element: HTMLElement, backgroundColor: string | undefined, borderColor: string | undefined): void {
if (element.clientWidth <= 0) { if (element.clientWidth <= 0) {
return; return;
} }
@@ -736,7 +735,7 @@ export class SearchAddon implements ITerminalAddon {
color: decorations.matchOverviewRuler, position: 'center' color: decorations.matchOverviewRuler, position: 'center'
} }
}); });
findResultDecoration?.onRender((e) => this._applyStyles(e, decorations.matchBackground, decorations.matchBorder, result)); findResultDecoration?.onRender((e) => this._applyStyles(e, decorations.matchBackground, decorations.matchBorder));
findResultDecoration?.onDispose(() => marker.dispose()); findResultDecoration?.onDispose(() => marker.dispose());
return findResultDecoration; return findResultDecoration;
} }