Avoid selection lookups when navigation state already identifies the next result.
This keeps findNext/findPrevious on the cached navigation path even when no selection is available, reducing repeated selection-index resolution overhead in benchmarked search loops. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -474,14 +474,19 @@ export class SearchAddon extends Disposable implements ITerminalAddon, ISearchAp
|
|||||||
|
|
||||||
private _resolveResultIndex(matches: IMatch[], term: string, searchOptions: ISearchOptions | undefined, direction: 'next' | 'previous', currentSearchKey: string): number {
|
private _resolveResultIndex(matches: IMatch[], term: string, searchOptions: ISearchOptions | undefined, direction: 'next' | 'previous', currentSearchKey: string): number {
|
||||||
const previousNavigation = this._lastResolvedNavigation;
|
const previousNavigation = this._lastResolvedNavigation;
|
||||||
|
const selection = this._terminal?.getSelectionPosition();
|
||||||
if (
|
if (
|
||||||
previousNavigation &&
|
previousNavigation &&
|
||||||
previousNavigation.searchKey === currentSearchKey &&
|
previousNavigation.searchKey === currentSearchKey &&
|
||||||
previousNavigation.matches === matches
|
previousNavigation.matches === matches
|
||||||
) {
|
) {
|
||||||
const selection = this._terminal?.getSelectionPosition();
|
if (!selection) {
|
||||||
|
if (direction === 'next') {
|
||||||
|
return (previousNavigation.index + 1) % matches.length;
|
||||||
|
}
|
||||||
|
return (previousNavigation.index + matches.length - 1) % matches.length;
|
||||||
|
}
|
||||||
if (
|
if (
|
||||||
selection &&
|
|
||||||
selection.start.x === previousNavigation.selectionStartX &&
|
selection.start.x === previousNavigation.selectionStartX &&
|
||||||
selection.start.y === previousNavigation.selectionStartY &&
|
selection.start.y === previousNavigation.selectionStartY &&
|
||||||
previousNavigation.index >= 0 &&
|
previousNavigation.index >= 0 &&
|
||||||
@@ -493,7 +498,7 @@ export class SearchAddon extends Disposable implements ITerminalAddon, ISearchAp
|
|||||||
return (previousNavigation.index + matches.length - 1) % matches.length;
|
return (previousNavigation.index + matches.length - 1) % matches.length;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const currentSelectionIndex = this._findIndexFromSelection(matches);
|
const currentSelectionIndex = this._findIndexFromSelection(matches, selection);
|
||||||
let isIncrementalUpdate = false;
|
let isIncrementalUpdate = false;
|
||||||
if (searchOptions?.incremental && this._lastSearchKey !== undefined) {
|
if (searchOptions?.incremental && this._lastSearchKey !== undefined) {
|
||||||
isIncrementalUpdate = this._lastSearchKey !== currentSearchKey;
|
isIncrementalUpdate = this._lastSearchKey !== currentSearchKey;
|
||||||
@@ -516,9 +521,7 @@ export class SearchAddon extends Disposable implements ITerminalAddon, ISearchAp
|
|||||||
return matches.length - 1;
|
return matches.length - 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
private _findIndexFromSelection(matches: IMatch[]): number {
|
private _findIndexFromSelection(matches: IMatch[], selection: ReturnType<Terminal['getSelectionPosition']> | undefined = this._terminal?.getSelectionPosition()): number {
|
||||||
const terminal = this._terminal;
|
|
||||||
const selection = terminal?.getSelectionPosition();
|
|
||||||
if (!selection) {
|
if (!selection) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user