diff --git a/packages/codemirror/src/find_replace.ts b/packages/codemirror/src/find_replace.ts index ba9347cfe..40d3c15a7 100644 --- a/packages/codemirror/src/find_replace.ts +++ b/packages/codemirror/src/find_replace.ts @@ -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[] = []; + 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 }); } diff --git a/packages/codemirror/src/index.ts b/packages/codemirror/src/index.ts index 8d6687be8..c86d37b12 100644 --- a/packages/codemirror/src/index.ts +++ b/packages/codemirror/src/index.ts @@ -1,6 +1,6 @@ import { defaultKeymap, history, historyKeymap } from "@codemirror/commands"; 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 { highlightSelectionMatches } from "@codemirror/search"; import { vim } from "@replit/codemirror-vim"; @@ -73,6 +73,7 @@ export default class CodeMirror extends EditorView { ]), highlightSelectionMatches(), bracketMatching(), + codeFolding(), foldGutter(), indentationMarkers(), ];