From eb55fd4ba35fef7544b99846ecc37b6f09dd2a75 Mon Sep 17 00:00:00 2001 From: Daniel Imms <2193314+Tyriar@users.noreply.github.com> Date: Mon, 25 May 2026 21:58:29 -0700 Subject: [PATCH] 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 --- addons/addon-search/src/SearchAddon.ts | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/addons/addon-search/src/SearchAddon.ts b/addons/addon-search/src/SearchAddon.ts index e6ed6170c..d35851535 100644 --- a/addons/addon-search/src/SearchAddon.ts +++ b/addons/addon-search/src/SearchAddon.ts @@ -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 { const previousNavigation = this._lastResolvedNavigation; + const selection = this._terminal?.getSelectionPosition(); if ( previousNavigation && previousNavigation.searchKey === currentSearchKey && 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 ( - selection && selection.start.x === previousNavigation.selectionStartX && selection.start.y === previousNavigation.selectionStartY && previousNavigation.index >= 0 && @@ -493,7 +498,7 @@ export class SearchAddon extends Disposable implements ITerminalAddon, ISearchAp return (previousNavigation.index + matches.length - 1) % matches.length; } } - const currentSelectionIndex = this._findIndexFromSelection(matches); + const currentSelectionIndex = this._findIndexFromSelection(matches, selection); let isIncrementalUpdate = false; if (searchOptions?.incremental && this._lastSearchKey !== undefined) { isIncrementalUpdate = this._lastSearchKey !== currentSearchKey; @@ -516,9 +521,7 @@ export class SearchAddon extends Disposable implements ITerminalAddon, ISearchAp return matches.length - 1; } - private _findIndexFromSelection(matches: IMatch[]): number { - const terminal = this._terminal; - const selection = terminal?.getSelectionPosition(); + private _findIndexFromSelection(matches: IMatch[], selection: ReturnType | undefined = this._terminal?.getSelectionPosition()): number { if (!selection) { return -1; }