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