fix(diff): cap the Next Difference scroll at 100 ms and move both panes together (#8002)

Next / Previous Difference scrolled with the browser's smooth scroll, whose duration grows with the distance, and the other pane only followed on scroll events, a frame behind. The jump now animates with an ease-out for at most 100 ms (none for a line or less, full length from eleven lines on), the other pane moves in the same frame, a wheel or pointer still interrupts it, and reduced motion still jumps instantly. The unified diff uses the same animation and stops an unfinished jump before the next one.

Refs #7433

Co-authored-by: wildcatDownstairs <[email protected]>
Цей коміт міститься в:
LU XIANGRUIтаwildcatDownstairs authored and GitHub committed 2026-12-04 18:39:49 +08:00
1 джерело bd3f0cb651
коміт 56c6e8090a
3 files changed
+104 -4

No files matched your search

+7 -1
Переглянути файл
@@ -120,7 +120,7 @@ import {
type DiffViewerHost,
type DiffViewerPane,
} from "./diffViewerProviders";
import { PaddedPanes, UnalignedPanes, type DiffPanes } from "./diffScroll";
import { animateScrollTop, PaddedPanes, UnalignedPanes, type DiffPanes } from "./diffScroll";
import { groupOf } from "./changes";
import { commitLineTarget, EXCLUDE_COMMIT_LINES, INCLUDE_COMMIT_LINES, inclusionGutter } from "./hunkInclusion";
import { unifiedCommitLines, unifiedCommitSelection } from "./unifiedCommitLines";
@@ -1273,6 +1273,8 @@ function TwoPaneDiff({
// IDEA: past the last (first) difference F7 (Shift+F7) first hints, then moves along the chain.
const [navHint, setNavHint] = useState<string | null>(null);
const pendingFile = useRef<0 | 1 | -1>(0);
/** Stops a Next / Previous Difference scroll of a single scroller that is still under way. */
const stopJump = useRef(() => {});
const navigation = useRef<object | null>(null);
useEffect(
() => () => {
@@ -1363,7 +1365,11 @@ function TwoPaneDiff({
top: Math.max(0, top),
behavior: reducedMotion() ? "instant" : "smooth",
};
stopJump.current();
stopJump.current = () => {};
if (views.panes) views.panes.scrollTo(onA ? "a" : "b", options);
else if (options.behavior === "smooth")
stopJump.current = animateScrollTop(scroller, options.top!, v.defaultLineHeight);
else scroller.scrollTo(options);
},
});
+40 -1
Переглянути файл
@@ -2,7 +2,15 @@ import { describe, expect, it, vi } from "vitest";
import { Chunk, MergeView } from "@codemirror/merge";
import { EditorState, Text } from "@codemirror/state";
import { EditorView } from "@codemirror/view";
import { bandAt, bandPath, transferY, PaddedPanes, UnalignedPanes, type Band } from "../diffScroll";
import {
bandAt,
bandPath,
navigationScrollDuration,
transferY,
PaddedPanes,
UnalignedPanes,
type Band,
} from "../diffScroll";
import { collapsedDiffLines } from "../collapsedDiffLines";
import { readFileSync } from "node:fs";
import { resolve } from "node:path";
@@ -114,6 +122,7 @@ describe("synchronized navigation scrolling", () => {
dom,
scrollDOM,
state: { doc: text },
defaultLineHeight: LINE,
get documentTop() {
return -scrollDOM.scrollTop;
},
@@ -161,6 +170,36 @@ describe("synchronized navigation scrolling", () => {
}
});
it("animates Next Difference for at most 100 ms however far it goes, as IntelliJ does", () => {
expect([0, 1, 6, 11, 5000].map((lines) => navigationScrollDuration(lines * LINE, LINE))).toEqual([
0, 0, 50, 100, 100,
]);
});
it.each(["a", "b"] as const)("brings the other pane along in the same frame of a long jump from %s", (side) => {
// Issue #7433: 150 lines down, the jump ends within 100 ms and both panes arrive together.
const frames: FrameRequestCallback[] = [];
const frame = vi.spyOn(window, "requestAnimationFrame").mockImplementation((f) => frames.push(f));
const now = vi.spyOn(performance, "now").mockReturnValue(0);
const p = panes();
try {
const leader = p[side];
const follower = p[side === "a" ? "b" : "a"];
p.controller.scrollTo(side, { top: 3000, behavior: "smooth" });
for (const time of [16, 50, 100]) {
frames.shift()!(time);
expect(follower.scrollDOM.scrollTop, `${time} ms`).toBe(leader.scrollDOM.scrollTop);
}
expect(leader.scrollDOM.scrollTop).toBe(3000);
expect(frames).toEqual([]);
expect(leader.scrollDOM.scrollTo).not.toHaveBeenCalled();
} finally {
p.controller.destroy();
frame.mockRestore();
now.mockRestore();
}
});
it.each(["wheel", "pointerdown"])("lets %s interrupt an animated jump", (event) => {
vi.useFakeTimers();
const frame = vi.spyOn(window, "requestAnimationFrame").mockReturnValue(1);
+57 -2
Переглянути файл
@@ -211,6 +211,47 @@ export function bindDividerWheel(divider: HTMLElement, view: EditorView, beforeS
};
}
/**
* How long Next / Previous Difference animates a scroll of `distance` px, as IntelliJ's editor
* navigation does (`ScrollingModelImpl`): no animation for a line or less, growing to 100 ms at
* eleven lines and never longer, however far the next change is.
*/
export function navigationScrollDuration(distance: number, lineHeight: number): number {
if (!(lineHeight > 0)) return 0;
return Math.round(Math.min(Math.max((distance / lineHeight - 1) / 10, 0), 1) * 100);
}
/**
* Scrolls `el` to `top` over {@link navigationScrollDuration}, easing out, calling `step` after
* each move (so a synchronized pane moves in the same frame). Returns a function that stops it.
*/
export function animateScrollTop(el: HTMLElement, top: number, lineHeight: number, step = () => {}): () => void {
const from = el.scrollTop;
const duration = navigationScrollDuration(Math.abs(top - from), lineHeight);
if (!duration) {
el.scrollTop = top;
step();
return () => {};
}
const start = performance.now();
let frame = requestAnimationFrame(function tick(now) {
const t = Math.min(1, Math.max(0, now - start) / duration);
el.scrollTop = from + (top - from) * (1 - (1 - t) ** 3);
step();
if (t < 1) frame = requestAnimationFrame(tick);
});
return () => cancelAnimationFrame(frame);
}
/** Moves `view`'s pane: a "smooth" scroll is the short navigation scroll above, not the browser's. */
function scrollPane(view: EditorView, options: ScrollToOptions, step: () => void): () => void {
if (options.behavior !== "smooth" || options.top === undefined) {
view.scrollDOM.scrollTo(options);
return () => {};
}
return animateScrollTop(view.scrollDOM, options.top, view.defaultLineHeight, step);
}
/**
* The scroll machinery of a side-by-side diff's panes: added to both editors,
* attached to their MergeView, and torn down with the diff.
@@ -238,6 +279,8 @@ export class PaddedPanes implements DiffPanes {
private mv: MergeView | null = null;
private sync: boolean;
private scrolling: Side | null = null;
/** Stops a Next / Previous Difference scroll still under way. */
private stopJump = () => {};
private scrollTimer: ReturnType<typeof setTimeout> | undefined;
private wheelLeader: Side | null = null;
private wheelTimer: ReturnType<typeof setTimeout> | undefined;
@@ -298,7 +341,9 @@ export class PaddedPanes implements DiffPanes {
this.clearWheel();
this.scrolling = this.sync ? side : null;
this.releaseScroll();
this.view(side).scrollDOM.scrollTo(options);
this.stopJump = scrollPane(this.view(side), options, () => {
if (this.sync) this.follow(side);
});
}
/**
@@ -312,6 +357,7 @@ export class PaddedPanes implements DiffPanes {
}
destroy() {
this.stopJump();
clearTimeout(this.scrollTimer);
clearTimeout(this.wheelTimer);
for (const f of this.cleanup) f();
@@ -373,6 +419,8 @@ export class PaddedPanes implements DiffPanes {
}
private interruptScroll() {
this.stopJump();
this.stopJump = () => {};
const side = this.scrolling;
this.scrolling = null;
clearTimeout(this.scrollTimer);
@@ -403,6 +451,8 @@ export class UnalignedPanes implements DiffPanes {
private readonly passed: Record<Side, { top: number; dir: number } | null> = { a: null, b: null };
private sync: boolean;
private scrolling: Side | null = null;
/** Stops a Next / Previous Difference scroll still under way. */
private stopJump = () => {};
private scrollTimer: ReturnType<typeof setTimeout> | undefined;
private wheelLeader: Side | null = null;
private wheelTimer: ReturnType<typeof setTimeout> | undefined;
@@ -582,7 +632,9 @@ export class UnalignedPanes implements DiffPanes {
this.clearWheel();
this.scrolling = this.sync ? side : null;
this.releaseScroll();
this.view(side).scrollDOM.scrollTo(options);
this.stopJump = scrollPane(this.view(side), options, () => {
if (this.sync) this.follow(side);
});
}
private leadWheel(side: Side) {
@@ -602,6 +654,8 @@ export class UnalignedPanes implements DiffPanes {
}
private interruptScroll() {
this.stopJump();
this.stopJump = () => {};
const side = this.scrolling;
this.scrolling = null;
clearTimeout(this.scrollTimer);
@@ -612,6 +666,7 @@ export class UnalignedPanes implements DiffPanes {
}
destroy() {
this.stopJump();
clearTimeout(this.scrollTimer);
clearTimeout(this.wheelTimer);
if (this.frame >= 0) cancelAnimationFrame(this.frame);