Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions apps/web/src/components/DiffPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { useTheme } from "../hooks/useTheme";
import {
buildFileDiffRenderKey,
getDiffCollapseIconClassName,
getDiffLineStat,
getRenderablePatch,
resolveDiffThemeName,
resolveFileDiffPath,
Expand All @@ -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";
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
32 changes: 31 additions & 1 deletion apps/web/src/lib/diffRendering.test.ts
Original file line number Diff line number Diff line change
@@ -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", () => {
Expand Down Expand Up @@ -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 changes: 19 additions & 0 deletions apps/web/src/lib/diffRendering.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading