diff --git a/src/tools/writeFile.ts b/src/tools/writeFile.ts index 50b27322a..77c75f9bf 100644 --- a/src/tools/writeFile.ts +++ b/src/tools/writeFile.ts @@ -1,57 +1,18 @@ import * as fs from "fs"; import * as path from "path"; +import { createTwoFilesPatch } from "diff"; import { Tool } from "./types.js"; function generateDiff(oldContent: string, newContent: string, filePath: string): string { - const oldLines = oldContent.split('\n'); - const newLines = newContent.split('\n'); - - let diff = `--- ${filePath}\n+++ ${filePath}\n`; - - // Simple line-by-line diff - const maxLines = Math.max(oldLines.length, newLines.length); - let hunkStart = -1; - let hunkOldCount = 0; - let hunkNewCount = 0; - let hunkLines: string[] = []; - - for (let i = 0; i < maxLines; i++) { - const oldLine = oldLines[i] || ''; - const newLine = newLines[i] || ''; - - if (oldLine !== newLine) { - if (hunkStart === -1) { - hunkStart = i; - } - - if (i < oldLines.length) { - hunkLines.push(`-${oldLine}`); - hunkOldCount++; - } - if (i < newLines.length) { - hunkLines.push(`+${newLine}`); - hunkNewCount++; - } - } else { - if (hunkStart !== -1) { - // End of hunk - diff += `@@ -${hunkStart + 1},${hunkOldCount} +${hunkStart + 1},${hunkNewCount} @@\n`; - diff += hunkLines.join('\n') + '\n'; - hunkStart = -1; - hunkOldCount = 0; - hunkNewCount = 0; - hunkLines = []; - } - } - } - - // Handle final hunk - if (hunkStart !== -1) { - diff += `@@ -${hunkStart + 1},${hunkOldCount} +${hunkStart + 1},${hunkNewCount} @@\n`; - diff += hunkLines.join('\n') + '\n'; - } - - return diff; + return createTwoFilesPatch( + filePath, + filePath, + oldContent, + newContent, + undefined, + undefined, + { context: 3 } + ); } export const writeFileTool: Tool = { diff --git a/src/ui/ColoredDiff.tsx b/src/ui/ColoredDiff.tsx index a375e1e6d..d265b91ed 100644 --- a/src/ui/ColoredDiff.tsx +++ b/src/ui/ColoredDiff.tsx @@ -83,19 +83,19 @@ export const ColoredDiff: React.FC<{ diffContent: string }> = ({ diffContent }) {displayableLines.map((line, index) => { const lineKey = `diff-line-${index}`; let gutterNumStr = ''; - let color: string | undefined = undefined; let prefixSymbol = ' '; let dim = false; + let backgroundColor: string | undefined = undefined; switch (line.type) { case 'add': gutterNumStr = (line.newLine ?? '').toString(); - color = 'green'; + backgroundColor = '#1a4a1a'; prefixSymbol = '+'; break; case 'del': gutterNumStr = (line.oldLine ?? '').toString(); - color = 'red'; + backgroundColor = '#4a1a1a'; prefixSymbol = '-'; break; case 'context': @@ -110,10 +110,10 @@ export const ColoredDiff: React.FC<{ diffContent: string }> = ({ diffContent }) return ( {gutterNumStr.padEnd(4)} - + {prefixSymbol}{' '} - + {line.content}