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 <[email protected]>
Цей коміт міститься в:
faysouтаfaysou authored and GitHub committed 2026-11-25 02:09:48 +08:00
1 джерело f028109518
коміт 13e27f0133
2 files changed
+14 -2

No files matched your search

+12 -1
Переглянути файл
@@ -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);
+2 -1
Переглянути файл
@@ -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;
}