diff --git a/src/editor/core/cm/stickyScroll.test.ts b/src/editor/core/cm/stickyScroll.test.ts index 03492529a..e29c5df54 100644 --- a/src/editor/core/cm/stickyScroll.test.ts +++ b/src/editor/core/cm/stickyScroll.test.ts @@ -1,6 +1,7 @@ import { describe, expect, it } from "vitest"; import { EditorSelection, EditorState } from "@codemirror/state"; import { EditorView } from "@codemirror/view"; +import { codeFolding, foldEffect, unfoldEffect } from "@codemirror/language"; import { javascript } from "@codemirror/lang-javascript"; import { languages } from "@codemirror/language-data"; import { completedSyntaxState } from "@/test/syntax"; @@ -95,7 +96,7 @@ describe("sticky rows and scrolling the caret into view", () => { EditorState.create({ doc, selection: EditorSelection.cursor(head), - extensions: [javascript(), stickyScroll, stickyMaxLines.of(max)], + extensions: [javascript(), codeFolding(), stickyScroll, stickyMaxLines.of(max)], }), ), }); @@ -109,6 +110,16 @@ describe("sticky rows and scrolling the caret into view", () => { v.destroy(); }); + it("reserves space only for visible enclosing scopes when the caret is inside a folded block", () => { + const v = view(doc.indexOf("target")); + const range = { from: v.state.doc.line(3).to, to: v.state.doc.line(5).to }; + v.dispatch({ effects: foldEffect.of(range) }); + expect(stickyTopMargin(v)).toBe(2 * v.defaultLineHeight); + v.dispatch({ effects: unfoldEffect.of(range) }); + expect(stickyTopMargin(v)).toBe(3 * v.defaultLineHeight); + v.destroy(); + }); + it("is limited by the sticky line count and is zero outside any scope", () => { const limited = view(doc.indexOf("target"), 2); expect(stickyTopMargin(limited)).toBe(2 * limited.defaultLineHeight); diff --git a/src/editor/core/cm/stickyScroll.ts b/src/editor/core/cm/stickyScroll.ts index be7e17ea6..dbda72e8c 100644 --- a/src/editor/core/cm/stickyScroll.ts +++ b/src/editor/core/cm/stickyScroll.ts @@ -362,7 +362,8 @@ class StickyScroll { * from under them, as in IDEA. */ export function stickyTopMargin(view: EditorView): number { - const line = view.state.doc.lineAt(view.state.selection.main.head).number; + const caretBlock = view.lineBlockAt(view.state.selection.main.head); + const line = view.state.doc.lineAt(caretBlock.from).number; const rows = Math.min(enclosingHeaders(view.state, line).length, view.state.facet(stickyMaxLines)); return rows * view.defaultLineHeight; }