diff --git a/src/mode/modeHandler.ts b/src/mode/modeHandler.ts index e73d16cd505..d076f036bb8 100644 --- a/src/mode/modeHandler.ts +++ b/src/mode/modeHandler.ts @@ -63,7 +63,7 @@ export class ModeHandler implements vscode.Disposable, IModeHandler { public focusChanged = false; - private searchDecorationCacheKey: { searchString: string; documentVersion: number } | undefined; + private searchDecorationCacheKey: { searchString: string; matchIndex: number | undefined } | undefined; private readonly disposables: vscode.Disposable[] = []; private readonly handlerMap: IModeHandlerMap; @@ -1190,19 +1190,27 @@ export class ModeHandler implements vscode.Disposable, IModeHandler { if ( cacheKey && cacheKey.searchString === globalState.searchState.searchString && - cacheKey.documentVersion === this.vimState.document.version + cacheKey.matchIndex === globalState.searchState.nextMatchIndex ) { // The decorations are fine as-is, don't waste time re-calculating this.searchDecorationCacheKey = cacheKey; return; } - // If there are no decorations from the command line, get decorations for previous SearchState - decorations = getDecorationsForSearchMatchRanges( - globalState.searchState.getMatchRanges(this.vimState), - ); + // Update decorations highlighting the current match + if (globalState.searchState.nextMatchIndex !== undefined) { + decorations = getDecorationsForSearchMatchRanges( + globalState.searchState.getMatchRanges(this.vimState), + globalState.searchState.nextMatchIndex + ); + } else { + // No next match defined, update decorations without a currentMatchIndex + decorations = getDecorationsForSearchMatchRanges( + globalState.searchState.getMatchRanges(this.vimState), + ); + } this.searchDecorationCacheKey = { searchString: globalState.searchState.searchString, - documentVersion: this.vimState.document.version, + matchIndex: globalState.searchState.nextMatchIndex, }; } } diff --git a/src/state/searchState.ts b/src/state/searchState.ts index 280413ad104..1a6851b9fec 100644 --- a/src/state/searchState.ts +++ b/src/state/searchState.ts @@ -42,6 +42,7 @@ export class SearchState { this.cursorStartPosition = startPosition; this.ignoreSmartcase = ignoreSmartcase; + this._nextMatchIndex = undefined; } private _searchString: string; @@ -49,6 +50,11 @@ export class SearchState { private offset?: SearchOffset; public readonly cursorStartPosition: Position; + private _nextMatchIndex: number | undefined; + + public get nextMatchIndex(): number | undefined { + return this._nextMatchIndex; + } public get searchString(): string { return this._searchString; @@ -136,6 +142,7 @@ export class SearchState { return undefined; } const { range, index } = nextMatch; + this._nextMatchIndex = nextMatch.index; return { pos: this.offset ? this.offset.apply(range) : range.start, index }; }