From 98a6de6ff07c12daa987e9f6a839ff4fbf709feb Mon Sep 17 00:00:00 2001 From: Orca <301901286+og-orca@users.noreply.github.com> Date: Thu, 3 Dec 2026 03:32:59 +0100 Subject: [PATCH] fix(github): open the selected file with Show Diff in the pull request trees (#7623) Show Diff (Ctrl+D) on a file in the pull request Changes tree said "Select a changed file to show its diff": the diff actions ask the focused view for its selection through useDiffSource, and neither the pull request details tree nor the New Pull Request changes tree registered one. Both now do: the first selected file, else the tree's first file (a folder selection opens its first file), with the New Pull Request tree keeping its file chain for F7. Closes #7614 Co-authored-by: og-orca <301901286+og-orca@users.noreply.github.com> --- src/features/github/create/ChangesPanel.tsx | 76 ++++---- src/features/github/create/Create.module.css | 3 + src/features/github/details/ChangesTree.tsx | 67 ++++--- .../github/details/Details.module.css | 3 + src/features/github/showDiff.test.tsx | 169 ++++++++++++++++++ 5 files changed, 256 insertions(+), 62 deletions(-) create mode 100644 src/features/github/showDiff.test.tsx diff --git a/src/features/github/create/ChangesPanel.tsx b/src/features/github/create/ChangesPanel.tsx index e657be580..133e9e2ae 100644 --- a/src/features/github/create/ChangesPanel.tsx +++ b/src/features/github/create/ChangesPanel.tsx @@ -7,6 +7,8 @@ import { useMemo, useRef, useState, type MouseEvent, type ReactNode } from "react"; import { buildFileTree, dirKeys, type FileTreeNode } from "@/features/vcs/fileTree"; import { openDiff, withChain } from "@/features/vcs/diffInput"; +import { useDiffSource } from "@/features/vcs/diffSource"; +import { filesUnder } from "@/features/vcs/logChanges"; import { KIND_COLOR, fileCount, renameFrom, type EffectiveKind } from "@/features/vcs/model"; import type { Commit } from "@/platform/ipc"; import { tx } from "@/platform/i18n"; @@ -160,16 +162,18 @@ function CommitDetails({ commit }: { commit: Commit }) { const kindOf = (c: CreateChange): EffectiveKind => (c.kind === "untracked" ? "added" : c.kind); -/** Show Diff: `first` with every file of the tree in the chain (F7 moves along). */ -function showDiff(all: readonly CreateChange[], first: CreateChange) { - void openDiff( - withChain( - all.map((c) => c.diff), - Math.max(0, all.indexOf(first)), - ), +/** `first`'s diff with every file of the tree in the chain (F7 moves along). */ +function chainedDiff(all: readonly CreateChange[], first: CreateChange) { + return withChain( + all.map((c) => c.diff), + Math.max(0, all.indexOf(first)), ); } +function showDiff(all: readonly CreateChange[], first: CreateChange) { + void openDiff(chainedDiff(all, first)); +} + function ChangesTree() { const rowHeight = useRowHeight(22); const changes = useCreatePr((s) => s.changes); @@ -184,6 +188,10 @@ function ChangesTree() { return out; }, [roots]); const [selection, setSelection] = useState>(new Set()); + // Show Diff on the first selected file, else on the tree's first file. + const content = useRef(null); + const lead = filesUnder(roots, selection)[0] ?? ordered[0]; + useDiffSource(content, lead ? { diff: () => chainedDiff(ordered, lead), show: () => showDiff(ordered, lead) } : null); if (!changes || changes.state === "loading") return ( @@ -209,32 +217,34 @@ function ChangesTree() { showContextMenu(e, items); }; return ( - > - key={changes.value.length ? "files" : "empty"} - className={styles.tree} - roots={roots} - getKey={(n) => n.key} - getLabel={(n) => n.name} - getChildren={(n) => n.children} - isExpandable={(n) => !n.file} - defaultExpanded={expanded} - selected={selection} - onSelectionChange={setSelection} - multiSelect - rowHeight={rowHeight} - aria-label="Changes" - speedSearch - onActivate={(n) => n.file && showDiff(ordered, n.file)} - onContextMenu={menu} - empty={ - - - Commit does not contain any changes - - - } - renderNode={(n) => } - /> +
+ > + key={changes.value.length ? "files" : "empty"} + className={styles.tree} + roots={roots} + getKey={(n) => n.key} + getLabel={(n) => n.name} + getChildren={(n) => n.children} + isExpandable={(n) => !n.file} + defaultExpanded={expanded} + selected={selection} + onSelectionChange={setSelection} + multiSelect + rowHeight={rowHeight} + aria-label="Changes" + speedSearch + onActivate={(n) => n.file && showDiff(ordered, n.file)} + onContextMenu={menu} + empty={ + + + Commit does not contain any changes + + + } + renderNode={(n) => } + /> +
); } diff --git a/src/features/github/create/Create.module.css b/src/features/github/create/Create.module.css index 04a6a25ad..b13f32505 100644 --- a/src/features/github/create/Create.module.css +++ b/src/features/github/create/Create.module.css @@ -205,6 +205,9 @@ .commitMeta { color: var(--fg-muted); } +.treePane { + display: contents; +} .tree { flex: 1; min-height: 0; diff --git a/src/features/github/details/ChangesTree.tsx b/src/features/github/details/ChangesTree.tsx index 700d96ba7..fd37d622f 100644 --- a/src/features/github/details/ChangesTree.tsx +++ b/src/features/github/details/ChangesTree.tsx @@ -9,7 +9,9 @@ */ import { useEffect, useLayoutEffect, useMemo, useRef, useState, type MouseEvent } from "react"; import clsx from "clsx"; +import { useDiffSource } from "@/features/vcs/diffSource"; import { dirKeys, type FileTreeNode } from "@/features/vcs/fileTree"; +import { filesUnder } from "@/features/vcs/logChanges"; import { dirName, fileCount, KIND_COLOR, renameFrom } from "@/features/vcs/model"; import { useTx } from "@/platform/i18n"; import { Icon, Tree, fileIconName, showContextMenu, T, type MenuItem, type TreeHandle } from "@/ui"; @@ -99,6 +101,11 @@ export function ChangesTree({ number, files, commitSha, viewedState, threads, ch }); useEffect(() => provideGithubHandler("toggleViewed", () => toggleRef.current()), []); + // Show Diff on the first selected file, else on the tree's first file. + const content = useRef(null); + const lead = filesUnder(roots, selection)[0] ?? filesUnder(roots)[0]; + useDiffSource(content, lead ? { show: () => openDiff(lead) } : null); + const onContextMenu = (_node: Node | null, keys: Set, e: MouseEvent) => { setSelection(keys); const chosen = selectedFiles(roots, keys); @@ -150,35 +157,37 @@ export function ChangesTree({ number, files, commitSha, viewedState, threads, ch ); return ( - - handleRef={tree} - roots={roots} - getKey={(n) => n.key} - getLabel={(n) => n.name} - getChildren={(n) => (n.file ? undefined : n.children)} - isExpandable={(n) => !n.file} - expanded={expanded} - onExpandedChange={setExpanded} - selected={selection} - onSelectionChange={(s) => setSelection(s)} - rowClassName={() => styles.changeRow} - renderNode={(n, state) => ( - n.file && toggle([n.file])} - onEdit={() => n.file && jumpToSource(n.file)} - /> - )} - onActivate={(n) => n.file && openDiff(n.file)} - onContextMenu={onContextMenu} - speedSearch - aria-label={tx("Changes")} - className={styles.tree} - /> +
+ + handleRef={tree} + roots={roots} + getKey={(n) => n.key} + getLabel={(n) => n.name} + getChildren={(n) => (n.file ? undefined : n.children)} + isExpandable={(n) => !n.file} + expanded={expanded} + onExpandedChange={setExpanded} + selected={selection} + onSelectionChange={(s) => setSelection(s)} + rowClassName={() => styles.changeRow} + renderNode={(n, state) => ( + n.file && toggle([n.file])} + onEdit={() => n.file && jumpToSource(n.file)} + /> + )} + onActivate={(n) => n.file && openDiff(n.file)} + onContextMenu={onContextMenu} + speedSearch + aria-label={tx("Changes")} + className={styles.tree} + /> +
); } diff --git a/src/features/github/details/Details.module.css b/src/features/github/details/Details.module.css index 99493b66b..daab5a1d6 100644 --- a/src/features/github/details/Details.module.css +++ b/src/features/github/details/Details.module.css @@ -260,6 +260,9 @@ flex-direction: column; min-height: 60px; } +.treePane { + display: contents; +} .tree { flex: 1; min-height: 0; diff --git a/src/features/github/showDiff.test.tsx b/src/features/github/showDiff.test.tsx new file mode 100644 index 000000000..c835ad2c0 --- /dev/null +++ b/src/features/github/showDiff.test.tsx @@ -0,0 +1,169 @@ +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { cleanup, fireEvent, render, screen, waitFor } from "@testing-library/react"; + +const mocks = vi.hoisted(() => ({ + pullRequestDiff: vi.fn(), + commitDiff: vi.fn(), + openDiff: vi.fn(), + diffWindow: vi.fn(), +})); +vi.mock("./data/local", async (original) => ({ + ...(await original()), + openPullRequestFileDiff: mocks.pullRequestDiff, +})); +vi.mock("./details/flow", async (original) => ({ + ...(await original()), + openCommitFileDiff: mocks.commitDiff, +})); +vi.mock("@/features/vcs/diffInput", async (original) => ({ + ...(await original()), + openDiff: mocks.openDiff, +})); +vi.mock("@/features/vcs/DiffWindow", () => ({ showDiffWindow: mocks.diffWindow })); + +import { registerVcsActions } from "@/features/vcs/actions"; +import type { DiffData } from "@/features/vcs/diffInput"; +import { useVcs } from "@/features/vcs/store"; +import type { Commit } from "@/platform/ipc"; +import { setContext } from "@/platform/context"; +import { installKeyboardDispatcher, useKeymap } from "@/platform/keymap"; +import { useProject } from "@/platform/project"; +import type { GHCommitFile } from "./api/types"; +import { ChangesPanel } from "./create/ChangesPanel"; +import { useCreatePr, type CreateChange } from "./create/store"; +import { ChangesTree } from "./details/ChangesTree"; + +const ctrlD = { key: "d", code: "KeyD", ctrlKey: true }; +const original = useKeymap.getState().overrides; +const cleanups: (() => void)[] = []; + +const bind = (action: "vcs.showDiff" | "vcs.showDiffInNewWindow") => + useKeymap.setState({ + overrides: { ...original, "vcs.showDiff": [], "vcs.showDiffInNewWindow": [], [action]: ["Ctrl+D"] }, + }); + +/** Presses Ctrl+D with the focus on the changes tree, after selecting `name` when given. */ +function press(name?: string) { + if (name) fireEvent.mouseDown(screen.getByText(name), { button: 0 }); + const tree = screen.getByRole("tree", { name: "Changes" }); + tree.focus(); + fireEvent.keyDown(tree, ctrlD); +} + +beforeEach(() => { + vi.clearAllMocks(); + useProject.setState({ project: { root: "/p", name: "p" } as never }); + useVcs.setState({ available: true, root: "/p", roots: ["/p"], repo: "/p" }); + setContext("vcsAvailable", true); + cleanups.push(registerVcsActions(), installKeyboardDispatcher()); + bind("vcs.showDiff"); +}); + +afterEach(() => { + while (cleanups.length) cleanups.pop()!(); + cleanup(); + setContext("vcsAvailable", false); + useKeymap.setState({ overrides: original }); + useProject.setState({ project: null }); + useVcs.setState({ available: false, root: null, roots: [], repo: null }); +}); + +describe("Show Diff in a pull request's changes", () => { + const file = (filename: string): GHCommitFile => ({ + sha: "s", + filename, + status: "modified", + additions: 1, + deletions: 0, + changes: 1, + patch: null, + previousFilename: null, + }); + const files = [file("src/a.ts"), file("src/b.ts")]; + const show = (commitSha: string | null) => + render( + , + ); + + it("opens the pull request's diff of the selected file", async () => { + show(null); + press("b.ts"); + await waitFor(() => expect(mocks.pullRequestDiff).toHaveBeenCalledWith(7, "src/b.ts")); + }); + + it("opens the selected file's diff in the chosen commit", async () => { + show("c0ffee"); + press("a.ts"); + await waitFor(() => expect(mocks.commitDiff).toHaveBeenCalledWith(7, "c0ffee", files[0])); + }); + + it("opens the first file of a selected directory", async () => { + show(null); + press("src"); + await waitFor(() => expect(mocks.pullRequestDiff).toHaveBeenCalledWith(7, "src/a.ts")); + }); + + it("opens the first file when nothing is selected", async () => { + show(null); + press(); + await waitFor(() => expect(mocks.pullRequestDiff).toHaveBeenCalledWith(7, "src/a.ts")); + }); + + it("opens the same diff from Show Diff in New Window", async () => { + bind("vcs.showDiffInNewWindow"); + show(null); + press("b.ts"); + await waitFor(() => expect(mocks.pullRequestDiff).toHaveBeenCalledWith(7, "src/b.ts")); + }); +}); + +describe("Show Diff in a new pull request's changes", () => { + const change = (path: string): CreateChange => ({ + path, + oldPath: null, + kind: "modified", + diff: { path: `/p/${path}` } as DiffData, + }); + + beforeEach(() => { + useCreatePr.setState({ + commits: { state: "ok", value: [{ id: "c1", summary: "one" } as unknown as Commit] }, + selectedCommit: null, + changes: { state: "ok", value: [change("src/a.ts"), change("src/b.ts")] }, + }); + }); + afterEach(() => useCreatePr.setState({ commits: null, selectedCommit: null, changes: null })); + + it("opens the selected file's diff with the other files in its chain", async () => { + render(); + press("b.ts"); + await waitFor(() => expect(mocks.openDiff).toHaveBeenCalledOnce()); + const data = mocks.openDiff.mock.calls[0]![0] as DiffData; + expect(data.path).toBe("/p/src/b.ts"); + expect(data.chain?.map((d) => d.path)).toEqual(["/p/src/a.ts", "/p/src/b.ts"]); + }); + + it("opens the first file of a selected directory, and when nothing is selected", async () => { + render(); + press(); + await waitFor(() => expect(mocks.openDiff).toHaveBeenCalledOnce()); + press("src"); + await waitFor(() => expect(mocks.openDiff).toHaveBeenCalledTimes(2)); + expect(mocks.openDiff.mock.calls.map(([data]) => (data as DiffData).path)).toEqual(["/p/src/a.ts", "/p/src/a.ts"]); + }); + + it("hands the same diff to Show Diff in New Window", async () => { + bind("vcs.showDiffInNewWindow"); + render(); + press("b.ts"); + await waitFor(() => expect(mocks.diffWindow).toHaveBeenCalledOnce()); + expect(mocks.diffWindow.mock.calls[0]![0]).toMatchObject({ path: "/p/src/b.ts" }); + }); +});