summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorDax Raad <[email protected]>2025-06-18 11:20:40 -0400
committerDax Raad <[email protected]>2025-06-18 11:20:40 -0400
commitc1250abdf8bd1b05f6d5a495e42a985a0017a78d (patch)
tree6cfc83921e50b0ca7a55b11b0f88ac94566ef848
parentebe51534a16eda0e4cd74c767f354eb1d52fd563 (diff)
downloadopencode-c1250abdf8bd1b05f6d5a495e42a985a0017a78d.tar.gz
opencode-c1250abdf8bd1b05f6d5a495e42a985a0017a78d.zip
implemented diff trimming
-rw-r--r--packages/opencode/src/tool/edit.ts40
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")
+}