Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Highlight current search match #8627

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions src/mode/modeHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
};
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/state/searchState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,19 @@ export class SearchState {

this.cursorStartPosition = startPosition;
this.ignoreSmartcase = ignoreSmartcase;
this._nextMatchIndex = undefined;
}

private _searchString: string;
public pattern?: Pattern;
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;
Expand Down Expand Up @@ -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 };
}
Expand Down