import { For, Show, createMemo } from "solid-js"; interface DiffViewerProps { diff: string; } interface DiffLine { type: "add" | "remove" | "context"; content: string; } interface ParsedDiff { startLine: number; endLine: number; lines: DiffLine[]; additions: number; deletions: number; } export interface DiffStats { additions: number; deletions: number; } export function getDiffStats(diff: string): DiffStats { const lines = diff.split("\n"); let additions = 0; let deletions = 0; for (const line of lines) { if (line.startsWith("+") && !line.startsWith("+++")) { additions++; } else if (line.startsWith("-") && !line.startsWith("---")) { deletions++; } } return { additions, deletions }; } function parseDiff(diff: string): ParsedDiff { const rawLines = diff.split("\n"); const lines: DiffLine[] = []; let startLine = 0; let currentLine = 0; let additions = 0; let deletions = 0; for (const line of rawLines) { if (line.startsWith("@@")) { const match = line.match(/@@ -\d+(?:,\d+)? \+(\d+)(?:,\d+)? @@/); if (match) { startLine = parseInt(match[1], 10); currentLine = startLine; } } else if (line.startsWith("+") && !line.startsWith("+++")) { lines.push({ type: "add", content: line.slice(1) }); currentLine++; additions++; } else if (line.startsWith("-") && !line.startsWith("---")) { lines.push({ type: "remove", content: line.slice(1) }); deletions++; } else if (line.startsWith(" ")) { lines.push({ type: "context", content: line.slice(1) }); currentLine++; } } return { startLine, endLine: currentLine - 1, lines, additions, deletions, }; } export function DiffViewer(props: DiffViewerProps) { const parsed = createMemo(() => parseDiff(props.diff)); return (
0}>
{parsed().startLine}
{(line) => (
{line.type === "add" ? "+" : line.type === "remove" ? "-" : " "} {line.content}
)}
0}>
{parsed().endLine}
); }