fix(codemirror): total number of results not correct on large or folded documents

This commit is contained in:
Elian Doran 2025-05-31 11:22:04 +03:00
parent 5af4d65d1f
commit fac8f531b8
No known key found for this signature in database

View File

@ -16,6 +16,7 @@ export class SearchHighlighter {
currentFound: number; currentFound: number;
totalFound: number; totalFound: number;
matcher?: MatchDecorator; matcher?: MatchDecorator;
searchRegexp?: RegExp;
private parsedMatches: Match[]; private parsedMatches: Match[];
constructor(public view: EditorView) { constructor(public view: EditorView) {
@ -42,6 +43,7 @@ export class SearchHighlighter {
regexp: regex, regexp: regex,
decoration: searchMatchDecoration, decoration: searchMatchDecoration,
}); });
this.searchRegexp = regex;
this.#updateSearchData(this.view); this.#updateSearchData(this.view);
this.#scrollToMatchNearestSelection(); this.#scrollToMatchNearestSelection();
} }
@ -98,17 +100,21 @@ export class SearchHighlighter {
return; return;
} }
const matches = this.matcher.createDeco(view); // Create the match decorator which will automatically highlight matches in the document.
const cursor = matches.iter(); this.matches = this.matcher.createDeco(view);
while (cursor.value) {
this.parsedMatches.push({ // Manually search for matches in the current document in order to get the total number of matches.
from: cursor.from, const parsedMatches: Match[] = [];
to: cursor.to const text = view.state.doc.toString();
}); let match: RegExpExecArray | null | undefined;
cursor.next(); while ((match = this.searchRegexp?.exec(text))) {
const from = match.index ?? 0;
const to = from + match[0].length;
parsedMatches.push({ from, to });
} }
this.matches = matches; this.parsedMatches = parsedMatches;
this.totalFound = this.parsedMatches.length; this.totalFound = this.parsedMatches.length;
} }