Skip to content
This repository was archived by the owner on Jun 24, 2025. It is now read-only.

Commit 03de472

Browse files
committed
fix(codemirror): search not unfolding results
1 parent fac8f53 commit 03de472

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

packages/codemirror/src/find_replace.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { EditorView, Decoration, MatchDecorator, ViewPlugin, ViewUpdate } from "@codemirror/view";
2-
import { Range, RangeSet } from "@codemirror/state";
2+
import { foldState, unfoldEffect } from "@codemirror/language";
3+
import { Range, RangeSet, StateEffect } from "@codemirror/state";
34

45
const searchMatchDecoration = Decoration.mark({ class: "cm-searchMatch" });
56
const activeMatchDecoration = Decoration.mark({ class: "cm-activeMatch" });
@@ -79,8 +80,23 @@ export class SearchHighlighter {
7980
const match = this.parsedMatches[matchIndex];
8081
this.currentFound = matchIndex + 1;
8182
this.activeMatch = activeMatchDecoration.range(match.from, match.to);
83+
84+
// Check if the match is inside a folded region.
85+
const unfoldEffects: StateEffect<unknown>[] = [];
86+
const folded = this.view.state.field(foldState);
87+
const iter = folded.iter();
88+
while (iter.value) {
89+
if (match.from >= iter.from && match.to <= iter.to) {
90+
unfoldEffects.push(unfoldEffect.of({ from: iter.from, to: iter.to }));
91+
}
92+
iter.next();
93+
}
94+
8295
this.view.dispatch({
83-
effects: EditorView.scrollIntoView(match.from, { y: "center" }),
96+
effects: [
97+
...unfoldEffects,
98+
EditorView.scrollIntoView(match.from, { y: "center" })
99+
],
84100
scrollIntoView: true
85101
});
86102
}

packages/codemirror/src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { defaultKeymap, history, historyKeymap } from "@codemirror/commands";
22
import { EditorView, highlightActiveLine, keymap, lineNumbers, placeholder, ViewPlugin, ViewUpdate, type EditorViewConfig } from "@codemirror/view";
3-
import { defaultHighlightStyle, StreamLanguage, syntaxHighlighting, indentUnit, bracketMatching, foldGutter } from "@codemirror/language";
3+
import { defaultHighlightStyle, StreamLanguage, syntaxHighlighting, indentUnit, bracketMatching, foldGutter, codeFolding } from "@codemirror/language";
44
import { Compartment, EditorSelection, EditorState, type Extension } from "@codemirror/state";
55
import { highlightSelectionMatches } from "@codemirror/search";
66
import { vim } from "@replit/codemirror-vim";
@@ -73,6 +73,7 @@ export default class CodeMirror extends EditorView {
7373
]),
7474
highlightSelectionMatches(),
7575
bracketMatching(),
76+
codeFolding(),
7677
foldGutter(),
7778
indentationMarkers(),
7879
];

0 commit comments

Comments
 (0)