From c4b7f2a6703032c6e7b02ea376d784f83da8f35f Mon Sep 17 00:00:00 2001 From: l0ng-ai <24760907+l0ng-ai@users.noreply.github.com> Date: Mon, 30 Nov 2026 00:01:46 +0800 Subject: [PATCH] fix(editor): keep a horizontally scrolled view in place while line-end hints refresh Inline blame, line-end lenses and plugin messages draw from a zero-width anchor: they widen the scroller but not the content, and most of them leave on an edit and return a moment later. While they were away the scroll range shrank and the browser pulled a horizontally scrolled view back to the left, so the text jumped sideways as the user typed. A 1px spacer at the visible right edge, moved with every scroll, keeps the range from shrinking under the view without adding scroll room. Fixes #6624 --- .../core/cm/horizontalScrollHold.test.ts | 43 ++++++++++++++ src/editor/core/cm/horizontalScrollHold.ts | 59 +++++++++++++++++++ src/editor/core/cm/setup.ts | 2 + 3 files changed, 104 insertions(+) create mode 100644 src/editor/core/cm/horizontalScrollHold.test.ts create mode 100644 src/editor/core/cm/horizontalScrollHold.ts diff --git a/src/editor/core/cm/horizontalScrollHold.test.ts b/src/editor/core/cm/horizontalScrollHold.test.ts new file mode 100644 index 000000000..1db9fc0f3 --- /dev/null +++ b/src/editor/core/cm/horizontalScrollHold.test.ts @@ -0,0 +1,43 @@ +// @vitest-environment jsdom +import { afterEach, expect, it } from "vitest"; +import { EditorState } from "@codemirror/state"; +import { EditorView } from "@codemirror/view"; +import { horizontalScrollHold } from "./horizontalScrollHold"; + +const views: EditorView[] = []; +afterEach(() => views.splice(0).forEach((v) => v.destroy())); + +function mount(): EditorView { + const parent = document.createElement("div"); + document.body.append(parent); + const view = new EditorView({ + state: EditorState.create({ doc: "x".repeat(200), extensions: horizontalScrollHold }), + parent, + }); + views.push(view); + return view; +} + +// A line-end hint that leaves on an edit shrinks the scroll range; the spacer at the visible right +// edge keeps a horizontally scrolled view where it is instead of letting the browser pull it left. +it("keeps a spacer at the visible right edge as the view scrolls", () => { + const view = mount(); + const scroller = view.scrollDOM; + const spacer = scroller.querySelector(":scope > .cm-openidea-scrollHold")!; + expect(spacer.getAttribute("aria-hidden")).toBe("true"); + Object.defineProperty(scroller, "clientWidth", { configurable: true, value: 500 }); + scroller.scrollLeft = 40; + scroller.dispatchEvent(new Event("scroll")); + expect(spacer.style.left).toBe("539px"); + scroller.scrollLeft = 0; + scroller.dispatchEvent(new Event("scroll")); + expect(spacer.style.left).toBe("499px"); +}); + +it("removes the spacer with the editor", () => { + const view = mount(); + const scroller = view.scrollDOM; + view.destroy(); + views.splice(views.indexOf(view), 1); + expect(scroller.querySelector(".cm-openidea-scrollHold")).toBeNull(); +}); diff --git a/src/editor/core/cm/horizontalScrollHold.ts b/src/editor/core/cm/horizontalScrollHold.ts new file mode 100644 index 000000000..cb4f78e4d --- /dev/null +++ b/src/editor/core/cm/horizontalScrollHold.ts @@ -0,0 +1,59 @@ +import { EditorView, ViewPlugin, type ViewUpdate } from "@codemirror/view"; + +/** + * Line-end hints (inline blame, line-end lenses, plugin messages) draw from a zero-width anchor, + * so they widen the scroller without widening the content, and most of them leave on an edit and + * come back a moment later. While they are away the scroll range shrinks and the browser pulls a + * horizontally scrolled view back to the left: the text jumps sideways as the user types. + * A 1px spacer at the visible right edge keeps the range from shrinking under the view; it moves + * with the view and never adds scroll room of its own. + */ +export const horizontalScrollHold = [ + ViewPlugin.fromClass( + class { + readonly spacer: HTMLElement; + private readonly onScroll = () => this.place(); + + constructor(readonly view: EditorView) { + this.spacer = document.createElement("div"); + this.spacer.className = "cm-openidea-scrollHold"; + this.spacer.setAttribute("aria-hidden", "true"); + view.scrollDOM.appendChild(this.spacer); + view.scrollDOM.addEventListener("scroll", this.onScroll, { passive: true }); + this.place(); + } + + update(u: ViewUpdate) { + if (u.geometryChanged) this.view.requestMeasure({ read: () => this.edge(), write: (x) => this.put(x) }); + } + + private edge(): number { + const { scrollLeft, clientWidth } = this.view.scrollDOM; + return Math.max(0, scrollLeft + clientWidth - 1); + } + + private put(x: number) { + this.spacer.style.left = `${x}px`; + } + + private place() { + this.put(this.edge()); + } + + destroy() { + this.view.scrollDOM.removeEventListener("scroll", this.onScroll); + this.spacer.remove(); + } + }, + ), + EditorView.baseTheme({ + ".cm-openidea-scrollHold": { + position: "absolute", + top: "0", + width: "1px", + height: "1px", + pointerEvents: "none", + visibility: "hidden", + }, + }), +]; diff --git a/src/editor/core/cm/setup.ts b/src/editor/core/cm/setup.ts index a3a58e441..e1db6a1c9 100644 --- a/src/editor/core/cm/setup.ts +++ b/src/editor/core/cm/setup.ts @@ -58,6 +58,7 @@ import { gutterLineDrag } from "./gutterDrag"; import { overwriteField } from "./overwrite"; import { imeSyllableSpaces } from "./imeCommit"; import { stickySelectionField } from "./stickySelection"; +import { horizontalScrollHold } from "./horizontalScrollHold"; import { relativeLineNumbers } from "./relativeLineNumbers"; import { clipboardDocumentPath, clipboardFilesExtension } from "./clipboard"; import { primarySelectionYield } from "./primarySelection"; @@ -404,6 +405,7 @@ export function baseExtensions(o: SetupOptions): Extension { overwriteField, imeSyllableSpaces, stickySelectionField, + horizontalScrollHold, gutterLineDrag, clipboardDocumentPath.of(o.ctx.path), clipboardFilesExtension,