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
This commit is contained in:
l0ng-ai
2026-11-12 01:38:03 +08:00
committed by GitHub
parent 40aa7e4899
commit 2f9658c367
2 changed files with 25 additions and 6 deletions
+3 -5
View File
@@ -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();
@@ -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", {});