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
No known key found for this signature in database
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 { 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 searchMatchDecoration = Decoration.mark({ class: "cm-searchMatch" });
const activeMatchDecoration = Decoration.mark({ class: "cm-activeMatch" }); const activeMatchDecoration = Decoration.mark({ class: "cm-activeMatch" });
@ -79,8 +80,23 @@ export class SearchHighlighter {
const match = this.parsedMatches[matchIndex]; const match = this.parsedMatches[matchIndex];
this.currentFound = matchIndex + 1; this.currentFound = matchIndex + 1;
this.activeMatch = activeMatchDecoration.range(match.from, match.to); 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({ this.view.dispatch({
effects: EditorView.scrollIntoView(match.from, { y: "center" }), effects: [
...unfoldEffects,
EditorView.scrollIntoView(match.from, { y: "center" })
],
scrollIntoView: true scrollIntoView: true
}); });
} }

View File

@ -1,6 +1,6 @@
import { defaultKeymap, history, historyKeymap } from "@codemirror/commands"; import { defaultKeymap, history, historyKeymap } from "@codemirror/commands";
import { EditorView, highlightActiveLine, keymap, lineNumbers, placeholder, ViewPlugin, ViewUpdate, type EditorViewConfig } from "@codemirror/view"; import { EditorView, highlightActiveLine, keymap, lineNumbers, placeholder, ViewPlugin, ViewUpdate, type EditorViewConfig } from "@codemirror/view";
import { defaultHighlightStyle, StreamLanguage, syntaxHighlighting, indentUnit, bracketMatching, foldGutter } from "@codemirror/language"; import { defaultHighlightStyle, StreamLanguage, syntaxHighlighting, indentUnit, bracketMatching, foldGutter, codeFolding } from "@codemirror/language";
import { Compartment, EditorSelection, EditorState, type Extension } from "@codemirror/state"; import { Compartment, EditorSelection, EditorState, type Extension } from "@codemirror/state";
import { highlightSelectionMatches } from "@codemirror/search"; import { highlightSelectionMatches } from "@codemirror/search";
import { vim } from "@replit/codemirror-vim"; import { vim } from "@replit/codemirror-vim";
@ -73,6 +73,7 @@ export default class CodeMirror extends EditorView {
]), ]),
highlightSelectionMatches(), highlightSelectionMatches(),
bracketMatching(), bracketMatching(),
codeFolding(),
foldGutter(), foldGutter(),
indentationMarkers(), indentationMarkers(),
]; ];