fix(vcs): show declaration icons in the Compare tool window (#4300)

The Compare tool window chose every icon from the file name alone. When a revision is compared with the working tree, files that still exist now take their icon from the shared file presentation cache, as the Project tree does: Java and Kotlin classes, interfaces and tests, and Spring config files. Deleted files and comparisons between two revisions keep their file-type icons.

Co-authored-by: liziing <108126796+liziing@users.noreply.github.com>
This commit is contained in:
liziing
2026-11-18 21:33:45 +08:00
committed by GitHub
parent c3034b25e8
commit 90df4ba48e
3 changed files with 71 additions and 3 deletions
+3 -1
View File
@@ -12,6 +12,7 @@ export function ComparisonTree<F>({
current,
onSelect,
diffFor,
renderFileIcon,
renderDetails,
ariaLabel,
openOnActivate = false,
@@ -23,6 +24,7 @@ export function ComparisonTree<F>({
current: F | null;
onSelect: (entry: F | null) => void;
diffFor: (entry: F) => DiffData | null;
renderFileIcon?: (file: F) => ReactNode;
renderDetails: (node: FileTreeNode<F>) => ReactNode;
ariaLabel: string;
openOnActivate?: boolean;
@@ -64,7 +66,7 @@ export function ComparisonTree<F>({
className={styles.comparisonTree}
renderNode={(n) => (
<span className={styles.fileRow} style={{ flex: 1, padding: 0 }}>
<Icon name={n.file ? fileIconName(n.name) : "folder"} size={14} />
{(n.file && renderFileIcon?.(n.file)) ?? <Icon name={n.file ? fileIconName(n.name) : "folder"} size={14} />}
<span className={styles.fileName} style={{ color: n.file ? getColor?.(n.file) : undefined }}>
{n.name}
</span>
@@ -3,6 +3,8 @@ import { cleanup, fireEvent, render, screen } from "@testing-library/react";
const mocks = vi.hoisted(() => ({
worktree: vi.fn(),
revisions: vi.fn(),
presentations: vi.fn(),
openDiff: vi.fn(),
openFile: vi.fn(),
}));
@@ -10,9 +12,19 @@ vi.mock("@/platform/ipc", async (original) => {
const real = await original<typeof import("@/platform/ipc")>();
return {
...real,
commands: { ...real.commands, vcsDiffWithWorktree: mocks.worktree },
isTauri: () => true,
commands: {
...real.commands,
vcsDiffWithWorktree: mocks.worktree,
vcsDiffRevisions: mocks.revisions,
jvmFilePresentations: mocks.presentations,
},
};
});
vi.mock("@/ui", async (original) => ({
...(await original<typeof import("@/ui")>()),
Icon: ({ name }: { name: string }) => <span data-testid={`icon-${name}`} />,
}));
vi.mock("../diffInput", async (original) => {
const real = await original<typeof import("../diffInput")>();
return { ...real, openVcsDiff: mocks.openDiff };
@@ -25,7 +37,7 @@ vi.mock("@/workbench/contributions", async (original) => {
return { ...real, maybeEditorService: () => ({ openFile: mocks.openFile }) };
});
import { useProject } from "@/platform/project";
import { comparisonDiffData, type ComparisonFile } from "../operations";
import { ChangesBetweenDialog, comparisonDiffData, type ComparisonFile } from "../operations";
import { CompareToolWindow, useCompareTool } from "../compareTool";
import { useVcs } from "../store";
@@ -114,6 +126,47 @@ describe("CompareToolWindow", () => {
expect(mocks.openFile).toHaveBeenCalledWith("/workspace/client/src/app.ts", { focus: true });
});
it("shows Java and Kotlin declarations from the compared repository and skips deleted files", async () => {
const base = "/workspace/client/src/";
mocks.worktree.mockResolvedValue({
status: "ok",
data: [
{ ...modified, path: "src/PricingRule.java", kind: "renamed", oldPath: "src/Rule.java" },
{ ...modified, path: "src/Order.java" },
{ ...modified, path: "src/OrderTest.java", kind: "added" },
{ ...modified, path: "src/Removed.java", kind: "deleted" },
{ ...modified, path: "src/Pricing.kt" },
modified,
],
});
mocks.presentations.mockResolvedValue({
status: "ok",
data: [
{ path: base + "PricingRule.java", presentation: { kind: "interface" } },
{ path: base + "Order.java", presentation: { kind: "class" } },
{ path: base + "OrderTest.java", presentation: { kind: "class" } },
{ path: base + "Pricing.kt", presentation: { kind: "object" } },
],
});
render(<CompareToolWindow />);
expect(await screen.findByTestId("icon-interface")).toBeInTheDocument();
expect(screen.getByText("PricingRule.java").previousElementSibling).toBe(screen.getByTestId("icon-interface"));
expect(screen.getByText("Order.java").previousElementSibling).toBe(screen.getByTestId("icon-class"));
expect(screen.getByText("OrderTest.java").previousElementSibling).toBe(screen.getByTestId("icon-jvm-test"));
expect(screen.getByText("Removed.java").previousElementSibling).toBe(screen.getByTestId("icon-file-java"));
expect(screen.getByText("Pricing.kt").previousElementSibling).toBe(screen.getByTestId("icon-kotlin-object"));
expect(mocks.presentations.mock.calls.flatMap(([paths]) => paths).sort()).toEqual(
["Order.java", "OrderTest.java", "Pricing.kt", "PricingRule.java"].map((name) => base + name),
);
});
it("keeps the file-type icon for a comparison of two revisions", async () => {
mocks.revisions.mockResolvedValue({ status: "ok", data: [{ ...modified, path: "src/PricingRule.java" }] });
render(<ChangesBetweenDialog from={from} to={{ rev: "next", label: "next" }} onClose={() => {}} />);
expect(await screen.findByTestId("icon-file-java")).toBeInTheDocument();
expect(mocks.presentations).not.toHaveBeenCalled();
});
it("lists files without an inline diff and opens a file's diff in the editor", async () => {
mocks.openDiff.mockResolvedValue(undefined);
render(<CompareToolWindow />);
+13
View File
@@ -9,6 +9,7 @@ import type { ChangedFile, Commit, CompareResult, ResetMode, WorkingTreeChange }
import { executeAction } from "@/platform/actions";
import { call, commands, isTauri, nextRequestId } from "@/platform/ipc";
import { Button, Checkbox, Dialog, Icon, Input, Spinner, T } from "@/ui";
import { useFilePresentation } from "@/ui/filePresentation";
import { notifications } from "@/workbench/contributions";
import { windowBackgroundHex } from "@/workbench/theme";
import { diffInput, openVcsDiff, vcsDiffInFront, type DiffData, type DiffSide } from "./diffInput";
@@ -614,6 +615,17 @@ export interface ComparisonFile extends ChangedFile {
removed?: number;
}
function WorktreeFileIcon({ path }: { path: string }) {
const { icon } = useFilePresentation(path);
return <Icon name={icon} size={14} />;
}
/** A working-tree file shows the Project tree's icon (Java and Kotlin declarations, Spring config). */
function renderWorktreeFileIcon(file: ComparisonFile) {
if (!file.root || file.kind === "deleted") return null;
return <WorktreeFileIcon path={repositoryPath(file.root, file.path)} />;
}
/**
* Diff content of one comparison file. `swapped` exchanges the two sides
* (the Compare tool window's Swap Sides), for worktree and revision diffs alike.
@@ -804,6 +816,7 @@ export function ChangesBetweenDialog({
current={current}
onSelect={setCurrent}
diffFor={diffFor}
renderFileIcon={listOnly && !to ? renderWorktreeFileIcon : undefined}
getColor={(f) => KIND_COLOR[f.kind === "untracked" ? "added" : f.kind]}
ariaLabel="Changed files"
inlineDiff={!listOnly}