From 2f9658c3670074ebc3e51520bf46732325909bb2 Mon Sep 17 00:00:00 2001 From: l0ng-ai <24760907+l0ng-ai@users.noreply.github.com> Date: Thu, 12 Nov 2026 01:38:03 +0800 Subject: [PATCH] fix(vcs): smooth-scroll again when moving between differences (#2795) Next/Previous Difference also dispatched a scrollIntoView effect, which set scrollTop in the same measure as the smooth scroll and cut the animation off. The smooth scroll already centres the caret, so the effect is removed. Fixes #2764 --- src/features/vcs/DiffEditor.tsx | 8 +++---- .../vcs/__tests__/diffToolbar.test.tsx | 23 ++++++++++++++++++- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/src/features/vcs/DiffEditor.tsx b/src/features/vcs/DiffEditor.tsx index 06a76b535..ae30fb6d5 100644 --- a/src/features/vcs/DiffEditor.tsx +++ b/src/features/vcs/DiffEditor.tsx @@ -768,11 +768,9 @@ export function DiffEditor({ state: v.state, dispatch: (tr) => { const selection = tr.selection ?? v.state.selection; - v.dispatch({ - selection, - effects: EditorView.scrollIntoView(selection.main.head, { y: "center" }), - userEvent: "select.byChunk", - }); + // No scrollIntoView effect: it would set scrollTop in the same measure and cut off the + // smooth scroll below, which centres the caret. + v.dispatch({ selection, userEvent: "select.byChunk" }); // CodeMirror draws the caret only in a focused editor, so a difference reached from // the toolbar, a menu or F7 pressed after those would move an invisible caret. v.focus(); diff --git a/src/features/vcs/__tests__/diffToolbar.test.tsx b/src/features/vcs/__tests__/diffToolbar.test.tsx index 85d7962c1..0acb721c1 100644 --- a/src/features/vcs/__tests__/diffToolbar.test.tsx +++ b/src/features/vcs/__tests__/diffToolbar.test.tsx @@ -3,7 +3,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; import { fireEvent, render, screen, waitFor, within } from "@testing-library/react"; import type { EditorInput } from "@/workbench/contributions"; import { Chunk, getChunks } from "@codemirror/merge"; -import { Text } from "@codemirror/state"; +import { StateEffect, Text } from "@codemirror/state"; import { EditorView } from "@codemirror/view"; import { ContextMenuHost, iconMarkup } from "@/ui"; @@ -691,6 +691,27 @@ describe("Diff navigation caret", () => { } }); + it.each(["split", "unified"] as const)("leaves the scrolling to the smooth scroll in the %s viewer", async (mode) => { + const { mounted, view } = await reveal(mode); + try { + // A scrollIntoView in the same update sets scrollTop at once and cuts the animation off. + let instant = false; + view().dispatch({ + effects: StateEffect.appendConfig.of( + EditorView.updateListener.of((u) => { + if (u.transactions.some((tr) => tr.scrollIntoView || tr.effects.length)) instant = true; + }), + ), + }); + instant = false; + fireEvent.click(await screen.findByRole("button", { name: "Next Difference" })); + expect(view().state.doc.lineAt(view().state.selection.main.head).number).toBe(2); + expect(instant).toBe(false); + } finally { + mounted.unmount(); + } + }); + it("keeps the caret visible when F7 runs while focus is outside the panes", async () => { const keymap = useKeymap.getState(); applyKeymapSettings("intellij-macos", {});