summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-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")
+}