From 191b5ea63bc77ca0910a1542497dd689b13de911 Mon Sep 17 00:00:00 2001 From: Nate Date: Wed, 30 Jul 2025 10:54:51 -0700 Subject: [PATCH 1/6] feat: word-level diff --- src/ui/ColoredDiff.test.ts | 199 +++++++++++++++++++++++++++++++++++++ src/ui/ColoredDiff.tsx | 148 ++++++++++++++++++++------- 2 files changed, 311 insertions(+), 36 deletions(-) create mode 100644 src/ui/ColoredDiff.test.ts diff --git a/src/ui/ColoredDiff.test.ts b/src/ui/ColoredDiff.test.ts new file mode 100644 index 000000000..2d7093eaf --- /dev/null +++ b/src/ui/ColoredDiff.test.ts @@ -0,0 +1,199 @@ +import { render } from "ink-testing-library"; +import React from "react"; +import { ColoredDiff } from "./ColoredDiff.js"; + +describe("ColoredDiff", () => { + it("renders simple line additions", () => { + const diffContent = `@@ -1,3 +1,4 @@ + function hello() { ++ console.log("added line"); + return "world"; + }`; + + const { lastFrame } = render(React.createElement(ColoredDiff, { diffContent })); + const frame = lastFrame(); + + expect(frame).toContain("console.log(\"added line\")"); + expect(frame).toContain("+"); + }); + + it("renders simple line deletions", () => { + const diffContent = `@@ -1,4 +1,3 @@ + function hello() { +- console.log("removed line"); + return "world"; + }`; + + const { lastFrame } = render(React.createElement(ColoredDiff, { diffContent })); + const frame = lastFrame(); + + expect(frame).toContain("console.log(\"removed line\")"); + expect(frame).toContain("-"); + }); + + it("renders context lines", () => { + const diffContent = `@@ -1,4 +1,4 @@ + function hello() { +- console.log("old"); ++ console.log("new"); + return "world"; + }`; + + const { lastFrame } = render(React.createElement(ColoredDiff, { diffContent })); + const frame = lastFrame(); + + expect(frame).toContain("function hello()"); + expect(frame).toContain("return \"world\""); + }); + + it("renders word-level diffs for modified lines", () => { + const diffContent = `@@ -1,3 +1,3 @@ + function hello() { +- return "old text"; ++ return "new text"; + }`; + + const { lastFrame } = render(React.createElement(ColoredDiff, { diffContent })); + const frame = lastFrame(); + + expect(frame).toContain("return \""); + expect(frame).toContain("text\""); + // Should show both old and new versions for word-level diff + expect(frame).toContain("-"); + expect(frame).toContain("+"); + }); + + it("handles empty diff content", () => { + const { lastFrame } = render(React.createElement(ColoredDiff, { diffContent: "" })); + const frame = lastFrame(); + + expect(frame).toContain("No changes detected"); + }); + + it("truncates long diffs", () => { + // Create a diff with more than 16 lines + const lines = Array.from({ length: 20 }, (_, i) => `+ line ${i + 1}`).join('\n'); + const diffContent = `@@ -1,1 +1,20 @@ + function hello() { +${lines} + }`; + + const { lastFrame } = render(React.createElement(ColoredDiff, { diffContent })); + const frame = lastFrame(); + + expect(frame).toContain("more lines"); + }); + + it("handles mixed add/delete groups for word-level diffing", () => { + const diffContent = `@@ -1,4 +1,4 @@ + function hello() { +- const x = "old value"; +- const y = "another old"; ++ const x = "new value"; ++ const y = "another new"; + return x + y; + }`; + + const { lastFrame } = render(React.createElement(ColoredDiff, { diffContent })); + const frame = lastFrame(); + + // Should render word-level diffs + expect(frame).toContain("const x = \""); + expect(frame).toContain("value\""); + expect(frame).toContain("const y = \"another"); + }); + + it("handles single-type changes without word-level diffing", () => { + const diffContent = `@@ -1,3 +1,4 @@ + function hello() { ++ console.log("new line 1"); ++ console.log("new line 2"); + return "world"; + }`; + + const { lastFrame } = render(React.createElement(ColoredDiff, { diffContent })); + const frame = lastFrame(); + + // Should render as normal line additions, not word-level + expect(frame).toContain("console.log(\"new line 1\")"); + expect(frame).toContain("console.log(\"new line 2\")"); + expect(frame).toContain("+"); + }); + + it("text content matches expected diff output ignoring styling", () => { + const diffContent = `@@ -1,4 +1,4 @@ + function hello() { +- return "old text"; ++ return "new text"; + }`; + + const { lastFrame } = render(React.createElement(ColoredDiff, { diffContent })); + const frame = lastFrame(); + + // Extract just the line numbers and content, ignoring colors/styling + // The output should contain these essential elements: + expect(frame).toContain("function hello()"); + expect(frame).toContain("- "); + expect(frame).toContain("+ "); + expect(frame).toContain("return \""); + expect(frame).toContain("text\""); + + // Verify we have both old and new text content + expect(frame).toContain("old text"); + expect(frame).toContain("new text"); + + // Verify we don't have corrupted or missing content + const lineMatches = frame?.match(/return "/g) || []; + expect(lineMatches.length).toBeGreaterThanOrEqual(2); // Should appear in both - and + lines + }); + + it("word-level diff preserves all original content", () => { + const diffContent = `@@ -1,3 +1,3 @@ + const x = 1; +-const y = "hello world"; ++const y = "hello universe"; + const z = 3;`; + + const { lastFrame } = render(React.createElement(ColoredDiff, { diffContent })); + const frame = lastFrame(); + + // All original text should be preserved + expect(frame).toContain("const x = 1"); + expect(frame).toContain("const z = 3"); + + // Both old and new versions should be shown + expect(frame).toContain("hello world"); + expect(frame).toContain("hello universe"); + expect(frame).toContain("const y = \"hello"); + + // Should have proper line prefixes + expect(frame).toContain("- "); + expect(frame).toContain("+ "); + }); + + it("complex word-level diff maintains content integrity", () => { + const diffContent = `@@ -1,5 +1,5 @@ + function calculate() { +- const result = oldFunction(a, b); +- return result.value; ++ const result = newFunction(a, b, c); ++ return result.data; + }`; + + const { lastFrame } = render(React.createElement(ColoredDiff, { diffContent })); + const frame = lastFrame(); + + // Check that all unique content appears + expect(frame).toContain("function calculate()"); + expect(frame).toContain("const result ="); + expect(frame).toContain("oldFunction(a, b)"); + expect(frame).toContain("newFunction(a, b, c)"); + expect(frame).toContain("return result."); + expect(frame).toContain("value"); + expect(frame).toContain("data"); + + // Verify structure is maintained + expect(frame).toContain("- "); + expect(frame).toContain("+ "); + }); +}); \ No newline at end of file diff --git a/src/ui/ColoredDiff.tsx b/src/ui/ColoredDiff.tsx index 1dcae6de5..c3ceefb5d 100644 --- a/src/ui/ColoredDiff.tsx +++ b/src/ui/ColoredDiff.tsx @@ -1,5 +1,6 @@ import { Box, Text } from "ink"; import React from "react"; +import { diffWordsWithSpace } from "diff"; interface DiffLine { type: 'add' | 'del' | 'context' | 'hunk' | 'other'; @@ -67,6 +68,25 @@ function parseDiffWithLineNumbers(diffContent: string): DiffLine[] { return result; } +function getWordLevelContent(oldContent: string, newContent: string, showType: 'removed' | 'added'): string { + const changes = diffWordsWithSpace(oldContent, newContent); + let result = ''; + + changes.forEach((change) => { + if (showType === 'removed') { + if (change.removed || !change.added) { + result += change.value; + } + } else { + if (change.added || !change.removed) { + result += change.value; + } + } + }); + + return result; +} + export const ColoredDiff: React.FC<{ diffContent: string }> = ({ diffContent }) => { const parsedLines = parseDiffWithLineNumbers(diffContent); @@ -81,46 +101,102 @@ export const ColoredDiff: React.FC<{ diffContent: string }> = ({ diffContent }) const truncatedLines = displayableLines.slice(0, 16); const isTruncated = displayableLines.length > 16; + // Group consecutive add/delete lines for word-level diffing + const groupedLines: Array<{ type: 'group' | 'single', lines: DiffLine[] }> = []; + let currentGroup: DiffLine[] = []; + + for (let i = 0; i < truncatedLines.length; i++) { + const line = truncatedLines[i]; + const nextLine = truncatedLines[i + 1]; + + if (line.type === 'add' || line.type === 'del') { + currentGroup.push(line); + + // If next line is different type or end of array, finalize the group + if (!nextLine || (nextLine.type !== 'add' && nextLine.type !== 'del')) { + if (currentGroup.length > 1) { + const delLines = currentGroup.filter(l => l.type === 'del'); + const addLines = currentGroup.filter(l => l.type === 'add'); + + // Only do word-level diff if we have both deletions and additions + if (delLines.length > 0 && addLines.length > 0) { + groupedLines.push({ type: 'group', lines: currentGroup }); + } else { + // Single type, treat as individual lines + currentGroup.forEach(l => groupedLines.push({ type: 'single', lines: [l] })); + } + } else { + groupedLines.push({ type: 'single', lines: currentGroup }); + } + currentGroup = []; + } + } else { + groupedLines.push({ type: 'single', lines: [line] }); + } + } + return ( - {truncatedLines.map((line, index) => { - const lineKey = `diff-line-${index}`; - let gutterNumStr = ''; - let prefixSymbol = ' '; - let dim = false; - let backgroundColor: string | undefined = undefined; + {groupedLines.map((group, groupIndex) => { + if (group.type === 'single') { + const line = group.lines[0]; + const lineKey = `diff-line-${groupIndex}`; + let gutterNumStr = ''; + let prefixSymbol = ' '; + let dim = false; + let backgroundColor: string | undefined = undefined; - switch (line.type) { - case 'add': - gutterNumStr = (line.newLine ?? '').toString(); - backgroundColor = '#1a4a1a'; - prefixSymbol = '+'; - break; - case 'del': - gutterNumStr = (line.oldLine ?? '').toString(); - backgroundColor = '#4a1a1a'; - prefixSymbol = '-'; - break; - case 'context': - gutterNumStr = (line.newLine ?? '').toString(); - dim = true; - prefixSymbol = ' '; - break; - default: - return null; + switch (line.type) { + case 'add': + gutterNumStr = (line.newLine ?? '').toString(); + backgroundColor = '#1a4a1a'; + prefixSymbol = '+'; + break; + case 'del': + gutterNumStr = (line.oldLine ?? '').toString(); + backgroundColor = '#4a1a1a'; + prefixSymbol = '-'; + break; + case 'context': + gutterNumStr = (line.newLine ?? '').toString(); + dim = true; + prefixSymbol = ' '; + break; + default: + return null; + } + + return ( + + {gutterNumStr.padEnd(4)} + + {prefixSymbol}{' '} + + + {line.content} + + + ); + } else { + // Word-level diff group + const delLines = group.lines.filter(l => l.type === 'del'); + const addLines = group.lines.filter(l => l.type === 'add'); + const oldContent = delLines.map(l => l.content).join('\n'); + const newContent = addLines.map(l => l.content).join('\n'); + + return ( + + + {(delLines[0]?.oldLine ?? '').toString().padEnd(4)} + - {getWordLevelContent(oldContent, newContent, 'removed')} + + + {(addLines[0]?.newLine ?? '').toString().padEnd(4)} + + {getWordLevelContent(oldContent, newContent, 'added')} + + + ); } - - return ( - - {gutterNumStr.padEnd(4)} - - {prefixSymbol}{' '} - - - {line.content} - - - ); })} {isTruncated && ( From 186b5b62a2f7bde590cd79819067998b02523da1 Mon Sep 17 00:00:00 2001 From: Nate Date: Wed, 30 Jul 2025 11:16:02 -0700 Subject: [PATCH 2/6] fix: coloring of word level diff --- src/ui/ColoredDiff.tsx | 175 ++++++++++++++++++++++++----------------- 1 file changed, 105 insertions(+), 70 deletions(-) diff --git a/src/ui/ColoredDiff.tsx b/src/ui/ColoredDiff.tsx index c3ceefb5d..991d2576f 100644 --- a/src/ui/ColoredDiff.tsx +++ b/src/ui/ColoredDiff.tsx @@ -1,16 +1,16 @@ +import { diffWordsWithSpace } from "diff"; import { Box, Text } from "ink"; import React from "react"; -import { diffWordsWithSpace } from "diff"; interface DiffLine { - type: 'add' | 'del' | 'context' | 'hunk' | 'other'; + type: "add" | "del" | "context" | "hunk" | "other"; oldLine?: number; newLine?: number; content: string; } function parseDiffWithLineNumbers(diffContent: string): DiffLine[] { - const lines = diffContent.split('\n'); + const lines = diffContent.split("\n"); const result: DiffLine[] = []; let currentOldLine = 0; let currentNewLine = 0; @@ -23,144 +23,169 @@ function parseDiffWithLineNumbers(diffContent: string): DiffLine[] { currentOldLine = parseInt(hunkMatch[1], 10); currentNewLine = parseInt(hunkMatch[2], 10); inHunk = true; - result.push({ type: 'hunk', content: line }); + result.push({ type: "hunk", content: line }); currentOldLine--; currentNewLine--; continue; } if (!inHunk) { if ( - line.startsWith('--- ') || - line.startsWith('+++ ') || - line.startsWith('diff --git') || - line.startsWith('index ') + line.startsWith("--- ") || + line.startsWith("+++ ") || + line.startsWith("diff --git") || + line.startsWith("index ") ) continue; continue; } - if (line.startsWith('+')) { + if (line.startsWith("+")) { currentNewLine++; result.push({ - type: 'add', + type: "add", newLine: currentNewLine, content: line.substring(1), }); - } else if (line.startsWith('-')) { + } else if (line.startsWith("-")) { currentOldLine++; result.push({ - type: 'del', + type: "del", oldLine: currentOldLine, content: line.substring(1), }); - } else if (line.startsWith(' ')) { + } else if (line.startsWith(" ")) { currentOldLine++; currentNewLine++; result.push({ - type: 'context', + type: "context", oldLine: currentOldLine, newLine: currentNewLine, content: line.substring(1), }); - } else if (line.startsWith('\\')) { - result.push({ type: 'other', content: line }); + } else if (line.startsWith("\\")) { + result.push({ type: "other", content: line }); } } return result; } -function getWordLevelContent(oldContent: string, newContent: string, showType: 'removed' | 'added'): string { +function renderWordLevelContent( + oldContent: string, + newContent: string, + showType: "removed" | "added" +): React.ReactNode { const changes = diffWordsWithSpace(oldContent, newContent); - let result = ''; - - changes.forEach((change) => { - if (showType === 'removed') { - if (change.removed || !change.added) { - result += change.value; + const segments: React.ReactNode[] = []; + + changes.forEach((change, index) => { + if (showType === "removed") { + if (change.removed) { + // Highlight removed words with darker red + segments.push( + + {change.value} + + ); + } else if (!change.added) { + // Unchanged text - no special styling + segments.push(change.value); } } else { - if (change.added || !change.removed) { - result += change.value; + if (change.added) { + // Highlight added words with darker green + segments.push( + + {change.value} + + ); + } else if (!change.removed) { + // Unchanged text - no special styling + segments.push(change.value); } } }); - - return result; + + return <>{segments}; } -export const ColoredDiff: React.FC<{ diffContent: string }> = ({ diffContent }) => { +export const ColoredDiff: React.FC<{ diffContent: string }> = ({ + diffContent, +}) => { const parsedLines = parseDiffWithLineNumbers(diffContent); - + if (parsedLines.length === 0) { return No changes detected.; } const displayableLines = parsedLines.filter( - (l) => l.type !== 'hunk' && l.type !== 'other', + (l) => l.type !== "hunk" && l.type !== "other" ); - const truncatedLines = displayableLines.slice(0, 16); - const isTruncated = displayableLines.length > 16; + const truncatedLines = displayableLines; + const isTruncated = false; // Group consecutive add/delete lines for word-level diffing - const groupedLines: Array<{ type: 'group' | 'single', lines: DiffLine[] }> = []; + const groupedLines: Array<{ type: "group" | "single"; lines: DiffLine[] }> = + []; let currentGroup: DiffLine[] = []; - + for (let i = 0; i < truncatedLines.length; i++) { const line = truncatedLines[i]; const nextLine = truncatedLines[i + 1]; - - if (line.type === 'add' || line.type === 'del') { + + if (line.type === "add" || line.type === "del") { currentGroup.push(line); - + // If next line is different type or end of array, finalize the group - if (!nextLine || (nextLine.type !== 'add' && nextLine.type !== 'del')) { + if (!nextLine || (nextLine.type !== "add" && nextLine.type !== "del")) { if (currentGroup.length > 1) { - const delLines = currentGroup.filter(l => l.type === 'del'); - const addLines = currentGroup.filter(l => l.type === 'add'); - + const delLines = currentGroup.filter((l) => l.type === "del"); + const addLines = currentGroup.filter((l) => l.type === "add"); + // Only do word-level diff if we have both deletions and additions if (delLines.length > 0 && addLines.length > 0) { - groupedLines.push({ type: 'group', lines: currentGroup }); + groupedLines.push({ type: "group", lines: currentGroup }); } else { // Single type, treat as individual lines - currentGroup.forEach(l => groupedLines.push({ type: 'single', lines: [l] })); + currentGroup.forEach((l) => + groupedLines.push({ type: "single", lines: [l] }) + ); } } else { - groupedLines.push({ type: 'single', lines: currentGroup }); + groupedLines.push({ type: "single", lines: currentGroup }); } currentGroup = []; } } else { - groupedLines.push({ type: 'single', lines: [line] }); + groupedLines.push({ type: "single", lines: [line] }); } } return ( {groupedLines.map((group, groupIndex) => { - if (group.type === 'single') { + if (group.type === "single") { const line = group.lines[0]; const lineKey = `diff-line-${groupIndex}`; - let gutterNumStr = ''; - let prefixSymbol = ' '; + let gutterNumStr = ""; + let prefixSymbol = " "; let dim = false; let backgroundColor: string | undefined = undefined; switch (line.type) { - case 'add': - gutterNumStr = (line.newLine ?? '').toString(); - backgroundColor = '#1a4a1a'; - prefixSymbol = '+'; + case "add": + gutterNumStr = (line.newLine ?? "").toString(); + backgroundColor = "green"; + prefixSymbol = "+"; break; - case 'del': - gutterNumStr = (line.oldLine ?? '').toString(); - backgroundColor = '#4a1a1a'; - prefixSymbol = '-'; + case "del": + gutterNumStr = (line.oldLine ?? "").toString(); + backgroundColor = "red"; + prefixSymbol = "-"; break; - case 'context': - gutterNumStr = (line.newLine ?? '').toString(); + case "context": + gutterNumStr = (line.newLine ?? "").toString(); dim = true; - prefixSymbol = ' '; + prefixSymbol = " "; break; default: return null; @@ -170,7 +195,7 @@ export const ColoredDiff: React.FC<{ diffContent: string }> = ({ diffContent }) {gutterNumStr.padEnd(4)} - {prefixSymbol}{' '} + {prefixSymbol}{" "} {line.content} @@ -179,20 +204,30 @@ export const ColoredDiff: React.FC<{ diffContent: string }> = ({ diffContent }) ); } else { // Word-level diff group - const delLines = group.lines.filter(l => l.type === 'del'); - const addLines = group.lines.filter(l => l.type === 'add'); - const oldContent = delLines.map(l => l.content).join('\n'); - const newContent = addLines.map(l => l.content).join('\n'); - + const delLines = group.lines.filter((l) => l.type === "del"); + const addLines = group.lines.filter((l) => l.type === "add"); + const oldContent = delLines.map((l) => l.content).join("\n"); + const newContent = addLines.map((l) => l.content).join("\n"); + return ( - {(delLines[0]?.oldLine ?? '').toString().padEnd(4)} - - {getWordLevelContent(oldContent, newContent, 'removed')} + + {(delLines[0]?.oldLine ?? "").toString().padEnd(4)}{" "} + + - + + {renderWordLevelContent(oldContent, newContent, "removed")} + - {(addLines[0]?.newLine ?? '').toString().padEnd(4)} - + {getWordLevelContent(oldContent, newContent, 'added')} + + {(addLines[0]?.newLine ?? "").toString().padEnd(4)}{" "} + + + + + {renderWordLevelContent(oldContent, newContent, "added")} + ); @@ -205,4 +240,4 @@ export const ColoredDiff: React.FC<{ diffContent: string }> = ({ diffContent }) )} ); -}; \ No newline at end of file +}; From 4db1cd9fcd013af2a2e3ceaa54ad7dd33eba568b Mon Sep 17 00:00:00 2001 From: Nate Date: Wed, 30 Jul 2025 13:36:20 -0700 Subject: [PATCH 3/6] update diff colors --- src/ui/ColoredDiff.tsx | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/src/ui/ColoredDiff.tsx b/src/ui/ColoredDiff.tsx index 991d2576f..803225f71 100644 --- a/src/ui/ColoredDiff.tsx +++ b/src/ui/ColoredDiff.tsx @@ -2,6 +2,15 @@ import { diffWordsWithSpace } from "diff"; import { Box, Text } from "ink"; import React from "react"; +// Color constants for diff display +const COLORS = { + ADDITION_BG: "#325b30", + DELETION_BG: "#712f37", + ADDITION_HIGHLIGHT: "#59a467", + DELETION_HIGHLIGHT: "#a75e6d", + LINE_NUMBER: "gray", +} as const; + interface DiffLine { type: "add" | "del" | "context" | "hunk" | "other"; oldLine?: number; @@ -81,7 +90,7 @@ function renderWordLevelContent( if (change.removed) { // Highlight removed words with darker red segments.push( - + {change.value} ); @@ -93,7 +102,7 @@ function renderWordLevelContent( if (change.added) { // Highlight added words with darker green segments.push( - + {change.value} ); @@ -174,12 +183,12 @@ export const ColoredDiff: React.FC<{ diffContent: string }> = ({ switch (line.type) { case "add": gutterNumStr = (line.newLine ?? "").toString(); - backgroundColor = "green"; + backgroundColor = COLORS.ADDITION_BG; prefixSymbol = "+"; break; case "del": gutterNumStr = (line.oldLine ?? "").toString(); - backgroundColor = "red"; + backgroundColor = COLORS.DELETION_BG; prefixSymbol = "-"; break; case "context": @@ -193,7 +202,7 @@ export const ColoredDiff: React.FC<{ diffContent: string }> = ({ return ( - {gutterNumStr.padEnd(4)} + {gutterNumStr.padEnd(4)} {prefixSymbol}{" "} @@ -212,20 +221,20 @@ export const ColoredDiff: React.FC<{ diffContent: string }> = ({ return ( - + {(delLines[0]?.oldLine ?? "").toString().padEnd(4)}{" "} - - - + - + {renderWordLevelContent(oldContent, newContent, "removed")} - + {(addLines[0]?.newLine ?? "").toString().padEnd(4)}{" "} - + - + + + {renderWordLevelContent(oldContent, newContent, "added")} From c498b67fd65eb64d1fb1ffdf45e6366d84056ff1 Mon Sep 17 00:00:00 2001 From: Nate Date: Wed, 30 Jul 2025 13:51:50 -0700 Subject: [PATCH 4/6] fix tests --- src/ui/ColoredDiff.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ui/ColoredDiff.tsx b/src/ui/ColoredDiff.tsx index 803225f71..dd74a4d4f 100644 --- a/src/ui/ColoredDiff.tsx +++ b/src/ui/ColoredDiff.tsx @@ -129,8 +129,8 @@ export const ColoredDiff: React.FC<{ diffContent: string }> = ({ (l) => l.type !== "hunk" && l.type !== "other" ); - const truncatedLines = displayableLines; - const isTruncated = false; + const truncatedLines = displayableLines.slice(0, 16); + const isTruncated = displayableLines.length > 16; // Group consecutive add/delete lines for word-level diffing const groupedLines: Array<{ type: "group" | "single"; lines: DiffLine[] }> = From 8a15233bc9d7e332953137dd26dae7ff328b9007 Mon Sep 17 00:00:00 2001 From: Nate Date: Wed, 30 Jul 2025 22:29:42 -0700 Subject: [PATCH 5/6] address feedback about memo --- src/ui/ColoredDiff.tsx | 73 +++++++++++++++++++++++++++--------------- 1 file changed, 47 insertions(+), 26 deletions(-) diff --git a/src/ui/ColoredDiff.tsx b/src/ui/ColoredDiff.tsx index dd74a4d4f..d266484de 100644 --- a/src/ui/ColoredDiff.tsx +++ b/src/ui/ColoredDiff.tsx @@ -1,6 +1,6 @@ import { diffWordsWithSpace } from "diff"; import { Box, Text } from "ink"; -import React from "react"; +import React, { useMemo } from "react"; // Color constants for diff display const COLORS = { @@ -77,12 +77,16 @@ function parseDiffWithLineNumbers(diffContent: string): DiffLine[] { return result; } +function useWordLevelDiff(oldContent: string, newContent: string) { + return useMemo(() => { + return diffWordsWithSpace(oldContent, newContent); + }, [oldContent, newContent]); +} + function renderWordLevelContent( - oldContent: string, - newContent: string, + changes: ReturnType, showType: "removed" | "added" ): React.ReactNode { - const changes = diffWordsWithSpace(oldContent, newContent); const segments: React.ReactNode[] = []; changes.forEach((change, index) => { @@ -116,6 +120,39 @@ function renderWordLevelContent( return <>{segments}; } +const WordLevelDiffGroup: React.FC<{ + groupIndex: number; + delLines: DiffLine[]; + addLines: DiffLine[]; +}> = ({ groupIndex, delLines, addLines }) => { + const oldContent = delLines.map(l => l.content).join('\n'); + const newContent = addLines.map(l => l.content).join('\n'); + const changes = useWordLevelDiff(oldContent, newContent); + + return ( + + + + {(delLines[0]?.oldLine ?? "").toString().padEnd(4)}{" "} + + - + + {renderWordLevelContent(changes, "removed")} + + + + + {(addLines[0]?.newLine ?? "").toString().padEnd(4)}{" "} + + + + + {renderWordLevelContent(changes, "added")} + + + + ); +}; + export const ColoredDiff: React.FC<{ diffContent: string }> = ({ diffContent, }) => { @@ -215,30 +252,14 @@ export const ColoredDiff: React.FC<{ diffContent: string }> = ({ // Word-level diff group const delLines = group.lines.filter((l) => l.type === "del"); const addLines = group.lines.filter((l) => l.type === "add"); - const oldContent = delLines.map((l) => l.content).join("\n"); - const newContent = addLines.map((l) => l.content).join("\n"); return ( - - - - {(delLines[0]?.oldLine ?? "").toString().padEnd(4)}{" "} - - - - - {renderWordLevelContent(oldContent, newContent, "removed")} - - - - - {(addLines[0]?.newLine ?? "").toString().padEnd(4)}{" "} - - + - - {renderWordLevelContent(oldContent, newContent, "added")} - - - + ); } })} From 447f4d7e9e9a8d8609ece748e7e552a645fc92e3 Mon Sep 17 00:00:00 2001 From: Nate Date: Wed, 30 Jul 2025 22:36:11 -0700 Subject: [PATCH 6/6] fix errors --- src/ui/ColoredDiff.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ui/ColoredDiff.tsx b/src/ui/ColoredDiff.tsx index d266484de..40b13c239 100644 --- a/src/ui/ColoredDiff.tsx +++ b/src/ui/ColoredDiff.tsx @@ -1,4 +1,4 @@ -import { diffWordsWithSpace } from "diff"; +import { diffWordsWithSpace, Change } from "diff"; import { Box, Text } from "ink"; import React, { useMemo } from "react"; @@ -84,7 +84,7 @@ function useWordLevelDiff(oldContent: string, newContent: string) { } function renderWordLevelContent( - changes: ReturnType, + changes: Change[], showType: "removed" | "added" ): React.ReactNode { const segments: React.ReactNode[] = [];