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", {});