fix(codemirror): search not unfolding results

This commit is contained in:
Elian Doran
2025-05-31 11:44:10 +03:00
parent fac8f531b8
commit 03de472a57
2 changed files with 20 additions and 3 deletions

View File

@@ -1,5 +1,6 @@
import { EditorView, Decoration, MatchDecorator, ViewPlugin, ViewUpdate } from "@codemirror/view";
import { Range, RangeSet } from "@codemirror/state";
import { foldState, unfoldEffect } from "@codemirror/language";
import { Range, RangeSet, StateEffect } from "@codemirror/state";
const searchMatchDecoration = Decoration.mark({ class: "cm-searchMatch" });
const activeMatchDecoration = Decoration.mark({ class: "cm-activeMatch" });
@@ -79,8 +80,23 @@ export class SearchHighlighter {
const match = this.parsedMatches[matchIndex];
this.currentFound = matchIndex + 1;
this.activeMatch = activeMatchDecoration.range(match.from, match.to);
// Check if the match is inside a folded region.
const unfoldEffects: StateEffect<unknown>[] = [];
const folded = this.view.state.field(foldState);
const iter = folded.iter();
while (iter.value) {
if (match.from >= iter.from && match.to <= iter.to) {
unfoldEffects.push(unfoldEffect.of({ from: iter.from, to: iter.to }));
}
iter.next();
}
this.view.dispatch({
effects: EditorView.scrollIntoView(match.from, { y: "center" }),
effects: [
...unfoldEffects,
EditorView.scrollIntoView(match.from, { y: "center" })
],
scrollIntoView: true
});
}