feat(diff-panel): show total line additions and deletions (#4674)

This commit is contained in:
jan authored and GitHub committed 2026-07-27 22:05:15 +02:00
1 parent 10bca3f44e
commit eea3ea4c6f
3 files changed
+61 -1

No files matched your search

+11
View File
@@ -30,6 +30,7 @@ import { useTheme } from "../hooks/useTheme";
import {
buildFileDiffRenderKey,
getDiffCollapseIconClassName,
getDiffLineStat,
getRenderablePatch,
resolveDiffThemeName,
resolveFileDiffPath,
@@ -41,6 +42,7 @@ import { resolveThreadRouteRef } from "../threadRoutes";
import { useClientSettings } from "../hooks/useSettings";
import { formatShortTimestamp } from "../timestampFormat";
import { DiffPanelLoadingState, DiffPanelShell, type DiffPanelMode } from "./DiffPanelShell";
import { DiffStatLabel } from "./chat/DiffStatLabel";
import { AnnotatableCodeView, type AnnotatableCodeViewHandle } from "./diffs/AnnotatableCodeView";
import { Button } from "./ui/button";
import { ToggleGroup, Toggle } from "./ui/toggle-group";
@@ -452,6 +454,7 @@ export default function DiffPanel({
);
const diffFileKeys = useMemo(() => codeViewFiles.map((file) => file.fileKey), [codeViewFiles]);
const allDiffFilesCollapsed = areAllDiffFilesCollapsed(diffFileKeys, collapsedDiffFileKeys);
const diffLineStat = useMemo(() => getDiffLineStat(renderableFiles), [renderableFiles]);
useEffect(() => {
if (!selectedFilePath) return;
@@ -713,6 +716,14 @@ export default function DiffPanel({
)}
</div>
<div className="flex shrink-0 items-center gap-1 [-webkit-app-region:no-drag]">
{codeViewFiles.length > 0 && (
<DiffStatLabel
additions={diffLineStat.additions}
deletions={diffLineStat.deletions}
className="mr-1 text-[11px]"
layout="inline"
/>
)}
{codeViewFiles.length > 0 && (
<Tooltip>
<TooltipTrigger
+31 -1
View File
@@ -1,5 +1,5 @@
import { describe, expect, it } from "vite-plus/test";
import { buildPatchCacheKey, getRenderablePatch } from "./diffRendering";
import { buildPatchCacheKey, getDiffLineStat, getRenderablePatch } from "./diffRendering";
describe("buildPatchCacheKey", () => {
it("returns a stable cache key for identical content", () => {
@@ -82,3 +82,33 @@ describe("getRenderablePatch", () => {
expect(parsed.files[0]?.hunks[0]?.unifiedLineStart).toBe(47);
});
});
describe("getDiffLineStat", () => {
it("totals additions and deletions across every file and hunk", () => {
const patch = [
"diff --git a/example.ts b/example.ts",
"--- a/example.ts",
"+++ b/example.ts",
"@@ -1,2 +1,3 @@",
"-before",
"+after",
"+added",
" context",
"@@ -10,2 +11,1 @@",
"-removed",
" context",
"diff --git a/README.md b/README.md",
"--- a/README.md",
"+++ b/README.md",
"@@ -1 +1,2 @@",
" title",
"+description",
].join("\n");
const parsed = getRenderablePatch(patch);
expect(parsed?.kind).toBe("files");
if (parsed?.kind !== "files") return;
expect(getDiffLineStat(parsed.files)).toEqual({ additions: 3, deletions: 2 });
});
});
+19
View File
@@ -52,6 +52,25 @@ export type RenderablePatch =
reason: string;
};
export interface DiffLineStat {
additions: number;
deletions: number;
}
export function getDiffLineStat(files: ReadonlyArray<FileDiffMetadata>): DiffLineStat {
return files.reduce<DiffLineStat>(
(total, file) => {
for (const hunk of file.hunks) {
total.additions += hunk.additionLines;
total.deletions += hunk.deletionLines;
}
return total;
},
{ additions: 0, deletions: 0 },
);
}
interface RenderablePatchOptions {
/**
* Pierre's partial-patch parser keeps hunk render starts in source-file