From 6927b787b2521f3a10c79fd239e126e7cd111ac8 Mon Sep 17 00:00:00 2001 From: l0ng-ai Date: Wed, 2 Dec 2026 00:15:32 +0800 Subject: [PATCH] fix(vcs): move on to the next file from the changes diff preview (#7271) The diff preview embedded in the Commit tool window and the Git window's Local Changes tab built a diff for the selected file without its sibling files, so F7 / the down arrow past the last difference did nothing there. The changes tree now publishes its files in tree order (unversioned included, amend entries left out) and the preview uses them as the diff's chain. Past the last (first) difference the diff hints, then shows the next (previous) file in the preview itself and asks the tree to select it; the tree reveals it the same way it follows the active diff tab. Fixes #7149 --- src/features/vcs/CommitView.tsx | 57 ++++++++++++++-- .../vcs/__tests__/commitPreviewDiff.test.tsx | 67 +++++++++++++++++-- src/features/vcs/changesPreview.ts | 47 +++++++++++++ 3 files changed, 161 insertions(+), 10 deletions(-) create mode 100644 src/features/vcs/changesPreview.ts diff --git a/src/features/vcs/CommitView.tsx b/src/features/vcs/CommitView.tsx index 8beb424afc..174e9bfd38 100644 --- a/src/features/vcs/CommitView.tsx +++ b/src/features/vcs/CommitView.tsx @@ -136,9 +136,16 @@ import { openDiff, openVcsDiff, opensDiffOnActivate, - workingDiffData, + type DiffData, } from "./diffInput"; import { DiffEditor } from "./DiffEditor"; +import { + type PreviewTree, + previewDiffData, + publishPreviewFiles, + selectPreviewFile, + useChangesPreview, +} from "./changesPreview"; import { KIND_COLOR, KIND_LABEL, fileCount, stripSlash } from "./model"; import { call, commands } from "@/platform/ipc"; import type { FileStatus } from "@/bindings"; @@ -713,6 +720,25 @@ export function useIncludedCount(): number { ); } +/** + * The embedded preview of `tree`'s selected working file. Past its last (first) difference + * F7 (Shift+F7) goes on to the tree's next (previous) file and selects it there. + */ +function usePreviewDiff(tree: PreviewTree, path: string | null | undefined) { + const files = useChangesPreview((s) => s.files[tree]); + const status = useVcs((s) => s.status); + const [moved, setMoved] = useState(null); + const data = useMemo(() => (path ? previewDiffData(path, files, moved, status) : null), [path, files, moved, status]); + const onNeighbour = useCallback( + (next: DiffData) => { + setMoved(next); + selectPreviewFile(tree, next.path); + }, + [tree], + ); + return { data, onNeighbour }; +} + /** * The Commit tool window's Commit tab: changes with inclusion checkboxes above the * message, with an optional embedded preview of the selected file's diff beside them @@ -732,7 +758,8 @@ function ChangesPanel() { amended && selectionKey?.startsWith("amend:") ? changeFileNodes([amended]).find((node) => node.key === selectionKey)?.historicalDiff : undefined; - const data = historical ?? (path ? workingDiffData(path) : null); + const working = usePreviewDiff("commit", historical ? null : path); + const data = historical ?? working.data; const hasChanges = useVcs((s) => s.status.length > 0) || !!amended?.historicalFileCount; return ( {data ? ( - + ) : ( {hasChanges ? "Select a file to preview its changes" : "No changes to preview"} @@ -776,6 +803,7 @@ function ChangesPanel() { export function LocalChangesView() { const preview = useCommit((s) => s.diffPreview); const path = useCommit((s) => (s.selection.length === 1 ? s.selection[0] : null)); + const working = usePreviewDiff("local", path); const anchor = useToolWindowLayout((s) => s.placements.git?.anchor ?? "bottom"); return ( - {path ? ( - + {working.data ? ( + ) : ( Select a file to preview its changes @@ -918,6 +946,25 @@ export function ChangesTree({ mode, amended }: { mode: "commit" | "local" | "dia [parents], ); + // The embedded diff preview walks this tree's files and asks it to select the one it moved to. + const previewTree = dialogMode ? null : mode; + const previewFiles = useMemo( + () => [...new Set(changeFileNodes(roots).flatMap((n) => (n.historicalDiff || !n.path ? [] : [n.path])))], + [roots], + ); + useEffect(() => { + if (previewTree) publishPreviewFiles(previewTree, previewFiles); + }, [previewTree, previewFiles]); + useEffect(() => () => void (previewTree && publishPreviewFiles(previewTree, undefined)), [previewTree]); + useEffect(() => { + if (!previewTree) return; + return useChangesPreview.subscribe(({ moved }, prev) => { + if (!moved || moved === prev.moved || moved.tree !== previewTree) return; + const node = [...byKey.values()].find((n) => n.type === "file" && n.path === moved.path && !n.historicalDiff); + if (node) revealNode(node); + }); + }, [previewTree, byKey, revealNode]); + const followedDiff = useRef(null); useEffect(() => { const editor = maybeEditorService(); diff --git a/src/features/vcs/__tests__/commitPreviewDiff.test.tsx b/src/features/vcs/__tests__/commitPreviewDiff.test.tsx index fa19af6cc4..569ba7d540 100644 --- a/src/features/vcs/__tests__/commitPreviewDiff.test.tsx +++ b/src/features/vcs/__tests__/commitPreviewDiff.test.tsx @@ -29,10 +29,15 @@ vi.mock("@/platform/ipc", async (importOriginal) => { return { ...real, call: vi.fn() }; }); +const preview = vi.hoisted(() => ({ + props: null as null | { input: { data: DiffDataT }; onNeighbour?: (next: DiffDataT) => void }, +})); +type DiffDataT = import("../diffInput").DiffData; vi.mock("../DiffEditor", () => ({ - DiffEditor: ({ input }: { input: { data: { path: string } } }) => ( -
- ), + DiffEditor: (props: { input: { data: DiffDataT }; onNeighbour?: (next: DiffDataT) => void }) => { + preview.props = props; + return
; + }, })); const openDiff = vi.hoisted(() => vi.fn()); @@ -41,9 +46,9 @@ vi.mock("../diffInput", async (original) => ({ openDiff, })); -import { CommitToolWindow } from "../CommitView"; +import { CommitToolWindow, LocalChangesView } from "../CommitView"; import { setCommitDiffPreview, useCommit } from "../commitStore"; -import { diffInput, workingDiffData } from "../diffInput"; +import { chainNeighbour, diffInput, workingDiffData } from "../diffInput"; import { useVcs } from "../store"; /** The splitter between the changes and the preview: the only vertical one in the layout. */ @@ -301,3 +306,55 @@ describe("the active diff follows the selected file (#2492)", () => { expect(openDiff).not.toHaveBeenCalled(); }); }); + +describe("moving past the preview's first or last difference (#7149)", () => { + beforeEach(() => { + localStorage.clear(); + openDiff.mockClear(); + preview.props = null; + useCommit.setState({ commitDiffPreview: true, diffPreview: true, selection: [], groupByDir: false }); + useVcs.setState({ + available: true, + root: "/p", + repo: "/p", + roots: ["/p"], + status: ["a.ts", "b.ts", "c.ts"].map((name) => modified(`/p/${name}`)), + }); + }); + + /** F7 past the last difference: what DiffEditor does with the chain it was given. */ + const move = (dir: 1 | -1) => { + const { input, onNeighbour } = preview.props!; + const next = chainNeighbour(input.data, dir); + expect(next).not.toBeNull(); + expect(onNeighbour).toBeDefined(); + act(() => onNeighbour!(next!)); + }; + + it.each([ + ["Commit tool window", () => ], + ["Local Changes", () => ], + ])("%s preview goes on to the tree's next and previous files and selects them", (_name, view) => { + render(view()); + selectFirstFile(); + expect(preview.props!.input.data.chain?.map((d) => d.path)).toEqual(["/p/a.ts", "/p/b.ts", "/p/c.ts"]); + move(1); + expect(screen.getByTestId("diff-preview")).toHaveAttribute("data-path", "/p/b.ts"); + expect(preview.props!.input.data.reveal).toBe("first"); + expect(useCommit.getState().selection).toEqual(["/p/b.ts"]); + expect(screen.getByRole("treeitem", { name: /b.ts/ })).toHaveAttribute("aria-selected", "true"); + move(-1); + expect(screen.getByTestId("diff-preview")).toHaveAttribute("data-path", "/p/a.ts"); + expect(preview.props!.input.data.reveal).toBe("last"); + expect(useCommit.getState().selection).toEqual(["/p/a.ts"]); + // The preview swaps its own content instead of opening an editor tab. + expect(openDiff).not.toHaveBeenCalled(); + }); + + it("has no next file when the tree has only one", () => { + useVcs.setState({ status: [modified("/p/a.ts")] }); + render(); + selectFirstFile(); + expect(preview.props!.input.data.chain).toBeUndefined(); + }); +}); diff --git a/src/features/vcs/changesPreview.ts b/src/features/vcs/changesPreview.ts new file mode 100644 index 0000000000..2efae9c754 --- /dev/null +++ b/src/features/vcs/changesPreview.ts @@ -0,0 +1,47 @@ +import { create } from "zustand"; +import type { FileStatus } from "@/bindings"; +import { type DiffData, withChain, workingDiffData } from "./diffInput"; + +/** The changes trees whose selection an embedded diff preview shows. */ +export type PreviewTree = "commit" | "local"; + +interface ChangesPreviewState { + /** Each tree's files in tree order: the preview's F7 chain. */ + files: Partial>; + /** A file the preview moved to past its first or last difference, for the tree to select. */ + moved: { tree: PreviewTree; path: string; seq: number } | null; +} + +export const useChangesPreview = create(() => ({ files: {}, moved: null })); + +/** Called by a changes tree when its files or their order change. */ +export function publishPreviewFiles(tree: PreviewTree, paths: readonly string[] | undefined) { + const current = useChangesPreview.getState().files[tree]; + const same = current && paths && current.length === paths.length && current.every((p, i) => p === paths[i]); + if (same || (!current && !paths)) return; + useChangesPreview.setState((s) => ({ files: { ...s.files, [tree]: paths } })); +} + +/** Asks `tree` to select `path`, the file the preview moved to. */ +export function selectPreviewFile(tree: PreviewTree, path: string) { + useChangesPreview.setState((s) => ({ moved: { tree, path, seq: (s.moved?.seq ?? 0) + 1 } })); +} + +/** + * The preview of the working file `path`, with the tree's other files as its chain: as in + * IDEA's changes view preview, F7 (Shift+F7) past the last (first) difference goes on to the + * next (previous) file and selects it in the tree. `moved` is the neighbour the diff moved + * to, which carries the difference to reveal. + */ +export function previewDiffData( + path: string, + files: readonly string[] | undefined, + moved: DiffData | null, + status: readonly FileStatus[], +): DiffData { + const byPath = new Map(status.map((st) => [st.path, st])); + const diff = (p: string) => workingDiffData(p, undefined, null, byPath.get(p)); + const index = files ? files.indexOf(path) : -1; + const data = !files || index < 0 || files.length < 2 ? diff(path) : withChain(files.map(diff), index); + return moved?.path === path && moved.reveal ? { ...data, reveal: moved.reveal } : data; +}