From 4a14efc168504dd536c7bc8daec4ebe35ed41a70 Mon Sep 17 00:00:00 2001 From: l0ng-ai Date: Tue, 24 Nov 2026 21:10:16 +0800 Subject: [PATCH] fix(diff): prevent layout jitter on load (#5600) The side-by-side diff showed estimated gutter positions while CodeMirror measured the panes and the divider caught up. Collapsed unchanged rows get their final height immediately, and the panes and divider appear together after the first layout measurement. --- e2e/diffUnchangedLayout.spec.ts | 37 ++++++++++++++++++++++++++ src/features/vcs/DiffEditor.module.css | 4 +++ src/features/vcs/cm.ts | 3 ++- src/features/vcs/diffScroll.ts | 13 ++++++++- 4 files changed, 55 insertions(+), 2 deletions(-) diff --git a/e2e/diffUnchangedLayout.spec.ts b/e2e/diffUnchangedLayout.spec.ts index b2daaff16..578f4a5c4 100644 --- a/e2e/diffUnchangedLayout.spec.ts +++ b/e2e/diffUnchangedLayout.spec.ts @@ -45,6 +45,43 @@ test("side-by-side unchanged separators expand both panes from the keyboard", as await expect(left.locator(".cm-content")).toBeFocused(); }); +test("the first visible split diff frame has aligned line numbers and divider", async ({ workbench: page }) => { + const frames = await page.evaluate(async () => { + const left = Array.from({ length: 2500 }, (_, index) => `const item${index + 1} = ${index + 1};`).join("\n"); + const right = left.replace("const item2174 = 2174;", "const item2174 = 0;\nconst added = 1;"); + const actions = "/src/platform/actions.ts"; + const { executeAction } = await import(/* @vite-ignore */ actions); + const samples = []; + void executeAction("diff.compareTexts", { + path: "items.ts", + left: { text: left, label: "Before" }, + right: { text: right, label: "After" }, + }); + for (let frame = 0; frame < 120 && samples.length < 5; frame++) { + await new Promise(requestAnimationFrame); + const divider = document.querySelector(".cm-diffDivider"); + const panes = divider?.parentElement; + if (!divider || !panes || getComputedStyle(panes).opacity === "0") continue; + const origin = divider.getBoundingClientRect().top; + const top = (selector: string) => { + const element = panes.querySelector(selector); + return element ? element.getBoundingClientRect().top - origin : undefined; + }; + const line = top(".cm-merge-a .cm-diffLine-modified"); + const gutter = top(".cm-merge-a .cm-lineNumbers .cm-diffGutter-modified"); + const band = divider.querySelector(".cm-diffBand"); + samples.push({ line, gutter, bandStart: band?.getPointAtLength(0).y, path: band?.getAttribute("d") }); + } + return samples; + }); + expect(frames).toHaveLength(5); + const first = frames[0]!; + expect(first.line).toBeDefined(); + expect(first.gutter).toBeCloseTo(first.line!, 0); + expect(first.bandStart).toBeCloseTo(first.line!, 0); + for (const frame of frames) expect(frame).toEqual(first); +}); + test("the unified separator stays legible in a narrow viewer and expands on click", async ({ workbench: page }) => { await page.setViewportSize({ width: 640, height: 650 }); await openDiff(page, "unified"); diff --git a/src/features/vcs/DiffEditor.module.css b/src/features/vcs/DiffEditor.module.css index 188bc8624..0a08efbb5 100644 --- a/src/features/vcs/DiffEditor.module.css +++ b/src/features/vcs/DiffEditor.module.css @@ -355,6 +355,10 @@ .paneScroll :global(.cm-mergeViewEditor > .cm-editor > .cm-scroller) { overflow: auto !important; } +.measuringPanes { + opacity: 0; +} + .host :global(.cm-diffDivider) { position: relative; flex: none; diff --git a/src/features/vcs/cm.ts b/src/features/vcs/cm.ts index 82b19aa22..4bc485d05 100644 --- a/src/features/vcs/cm.ts +++ b/src/features/vcs/cm.ts @@ -506,6 +506,7 @@ export const viewerTheme = EditorView.theme( ".cm-collapsedLines": { WebkitUserSelect: "none", userSelect: "none", + minHeight: "max(27px, var(--openidea-editor-line-height, var(--lh-code)))", padding: "0", background: "var(--bg-editor)", }, @@ -514,7 +515,7 @@ export const viewerTheme = EditorView.theme( alignItems: "center", gap: "var(--space-1)", width: "100%", - minHeight: "max(27px, var(--openidea-editor-line-height, var(--lh-code)))", + minHeight: "inherit", padding: "0 var(--space-3)", color: "var(--fg-secondary)", fontFamily: "var(--font-ui)", diff --git a/src/features/vcs/diffScroll.ts b/src/features/vcs/diffScroll.ts index 8bdbcb14d..e349410a7 100644 --- a/src/features/vcs/diffScroll.ts +++ b/src/features/vcs/diffScroll.ts @@ -10,6 +10,7 @@ import { Decoration, EditorView, ViewPlugin, type DecorationSet, type ViewUpdate import { getChunks, type Chunk, type MergeView } from "@codemirror/merge"; import { changedLineGutters, chunkKind, type DiffKind } from "./cm"; import { arrowButton } from "./diffControls"; +import styles from "./DiffEditor.module.css"; type Side = "a" | "b"; @@ -435,7 +436,17 @@ export class UnalignedPanes implements DiffPanes { this.resize = new ResizeObserver(() => this.schedule()); this.resize.observe(this.divider); } - requestAnimationFrame(() => this.resync()); + const editors = this.divider.parentElement!; + const measuring = styles.measuringPanes!; + editors.classList.add(measuring); + // Both editors have queued their initial measurements. Reveal the panes with the first divider + // drawing after those measurements, so estimated gutter positions never reach the screen. + this.frame = requestAnimationFrame(() => { + if (this.sync) this.follow("b"); + this.draw(); + editors.classList.remove(measuring); + }); + this.cleanup.push(() => editors.classList.remove(measuring)); } /** Brings the left pane in line with the right one. */