дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-03 16:51:16 +02:00
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 <[email protected]>
Цей коміт міститься в:
5 files changed
+256
-62
No files matched your search
@@ -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<ReadonlySet<string>>(new Set());
|
||||
// Show Diff on the first selected file, else on the tree's first file.
|
||||
const content = useRef<HTMLDivElement>(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 (
|
||||
<Centered>
|
||||
@@ -209,32 +217,34 @@ function ChangesTree() {
|
||||
showContextMenu(e, items);
|
||||
};
|
||||
return (
|
||||
<Tree<FileTreeNode<CreateChange>>
|
||||
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={
|
||||
<Centered>
|
||||
<span className={styles.hint}>
|
||||
<T>Commit does not contain any changes</T>
|
||||
</span>
|
||||
</Centered>
|
||||
}
|
||||
renderNode={(n) => <ChangeRow node={n} />}
|
||||
/>
|
||||
<div ref={content} className={styles.treePane}>
|
||||
<Tree<FileTreeNode<CreateChange>>
|
||||
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={
|
||||
<Centered>
|
||||
<span className={styles.hint}>
|
||||
<T>Commit does not contain any changes</T>
|
||||
</span>
|
||||
</Centered>
|
||||
}
|
||||
renderNode={(n) => <ChangeRow node={n} />}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -205,6 +205,9 @@
|
||||
.commitMeta {
|
||||
color: var(--fg-muted);
|
||||
}
|
||||
.treePane {
|
||||
display: contents;
|
||||
}
|
||||
.tree {
|
||||
flex: 1;
|
||||
min-height: 0;
|
||||
|
||||
@@ -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<HTMLDivElement>(null);
|
||||
const lead = filesUnder(roots, selection)[0] ?? filesUnder(roots)[0];
|
||||
useDiffSource(content, lead ? { show: () => openDiff(lead) } : null);
|
||||
|
||||
const onContextMenu = (_node: Node | null, keys: Set<string>, e: MouseEvent) => {
|
||||
setSelection(keys);
|
||||
const chosen = selectedFiles(roots, keys);
|
||||
@@ -150,35 +157,37 @@ export function ChangesTree({ number, files, commitSha, viewedState, threads, ch
|
||||
);
|
||||
|
||||
return (
|
||||
<Tree<Node>
|
||||
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) => (
|
||||
<ChangeRow
|
||||
node={n}
|
||||
flat={!grouped}
|
||||
progress={loading || (!n.file && state.expanded) ? null : progressOf(n, leaf)}
|
||||
hasViewedState={hasViewedState}
|
||||
canEdit={!!n.file && canEdit(n.file)}
|
||||
onToggleViewed={() => 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}
|
||||
/>
|
||||
<div ref={content} className={styles.treePane}>
|
||||
<Tree<Node>
|
||||
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) => (
|
||||
<ChangeRow
|
||||
node={n}
|
||||
flat={!grouped}
|
||||
progress={loading || (!n.file && state.expanded) ? null : progressOf(n, leaf)}
|
||||
hasViewedState={hasViewedState}
|
||||
canEdit={!!n.file && canEdit(n.file)}
|
||||
onToggleViewed={() => 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}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -260,6 +260,9 @@
|
||||
flex-direction: column;
|
||||
min-height: 60px;
|
||||
}
|
||||
.treePane {
|
||||
display: contents;
|
||||
}
|
||||
.tree {
|
||||
flex: 1;
|
||||
min-height: 0;
|
||||
|
||||
@@ -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<typeof import("./data/local")>()),
|
||||
openPullRequestFileDiff: mocks.pullRequestDiff,
|
||||
}));
|
||||
vi.mock("./details/flow", async (original) => ({
|
||||
...(await original<typeof import("./details/flow")>()),
|
||||
openCommitFileDiff: mocks.commitDiff,
|
||||
}));
|
||||
vi.mock("@/features/vcs/diffInput", async (original) => ({
|
||||
...(await original<typeof import("@/features/vcs/diffInput")>()),
|
||||
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(
|
||||
<ChangesTree
|
||||
number={7}
|
||||
files={files}
|
||||
commitSha={commitSha}
|
||||
viewedState={undefined}
|
||||
threads={[]}
|
||||
checkedOut={false}
|
||||
/>,
|
||||
);
|
||||
|
||||
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(<ChangesPanel />);
|
||||
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(<ChangesPanel />);
|
||||
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(<ChangesPanel />);
|
||||
press("b.ts");
|
||||
await waitFor(() => expect(mocks.diffWindow).toHaveBeenCalledOnce());
|
||||
expect(mocks.diffWindow.mock.calls[0]![0]).toMatchObject({ path: "/p/src/b.ts" });
|
||||
});
|
||||
});
|
||||
Reference in new issue
Заблокувати користувача