From 13e27f0133ec6310579ef85d4ea39aa91bddcd5c Mon Sep 17 00:00:00 2001 From: faysou Date: Tue, 24 Nov 2026 18:09:48 +0000 Subject: [PATCH] fix(editor): calculate sticky scroll margins from the visible folded line (#5662) The sticky-lines top margin used when scrolling the caret into view counted the headers enclosing the caret's source line, including headers a fold had hidden. Inside a folded block in a narrow split the extra rows made CodeMirror treat the visible fold placeholder as clipped and push the folded-code tooltip out of the viewport. The margin now starts from the caret's displayed line block, so outer scopes still count and unfolding restores the full margin. Co-authored-by: faysou <893862+faysou@users.noreply.github.com> --- src/editor/core/cm/stickyScroll.test.ts | 13 ++++++++++++- src/editor/core/cm/stickyScroll.ts | 3 ++- 2 files changed, 14 insertions(+), 2 deletions(-) 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; }