diff options
| author | Dax Raad <[email protected]> | 2025-06-18 11:20:40 -0400 |
|---|---|---|
| committer | Dax Raad <[email protected]> | 2025-06-18 11:20:40 -0400 |
| commit | c1250abdf8bd1b05f6d5a495e42a985a0017a78d (patch) | |
| tree | 6cfc83921e50b0ca7a55b11b0f88ac94566ef848 | |
| parent | ebe51534a16eda0e4cd74c767f354eb1d52fd563 (diff) | |
| download | opencode-c1250abdf8bd1b05f6d5a495e42a985a0017a78d.tar.gz opencode-c1250abdf8bd1b05f6d5a495e42a985a0017a78d.zip | |
implemented diff trimming
| -rw-r--r-- | packages/opencode/src/tool/edit.ts | 40 |
1 files changed, 39 insertions, 1 deletions
diff --git a/packages/opencode/src/tool/edit.ts b/packages/opencode/src/tool/edit.ts index 26db51fae..8af2a0eeb 100644 --- a/packages/opencode/src/tool/edit.ts +++ b/packages/opencode/src/tool/edit.ts @@ -87,7 +87,9 @@ export const EditTool = Tool.define({ await file.write(contentNew) })() - const diff = createTwoFilesPatch(filepath, filepath, contentOld, contentNew) + const diff = trimDiff( + createTwoFilesPatch(filepath, filepath, contentOld, contentNew), + ) FileTimes.read(ctx.sessionID, filepath) @@ -113,3 +115,39 @@ export const EditTool = Tool.define({ } }, }) + +function trimDiff(diff: string): string { + const lines = diff.split("\n") + const contentLines = lines.filter( + (line) => + (line.startsWith("+") || line.startsWith("-") || line.startsWith(" ")) && + !line.startsWith("---") && + !line.startsWith("+++"), + ) + + if (contentLines.length === 0) return diff + + let min = Infinity + for (const line of contentLines) { + const content = line.slice(1) + if (content.trim().length > 0) { + const match = content.match(/^(\s*)/) + if (match) min = Math.min(min, match[1].length) + } + } + if (min === Infinity || min === 0) return diff + const trimmedLines = lines.map((line) => { + if ( + (line.startsWith("+") || line.startsWith("-") || line.startsWith(" ")) && + !line.startsWith("---") && + !line.startsWith("+++") + ) { + const prefix = line[0] + const content = line.slice(1) + return prefix + content.slice(min) + } + return line + }) + + return trimmedLines.join("\n") +} |
