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")} - - - + ); } })}