Sfoglia il codice sorgente

implemented diff trimming

Dax Raad 8 mesi fa
parent
commit
c1250abdf8
1 ha cambiato i file con 39 aggiunte e 1 eliminazioni
  1. 39 1
      packages/opencode/src/tool/edit.ts

+ 39 - 1
packages/opencode/src/tool/edit.ts

@@ -87,7 +87,9 @@ export const EditTool = Tool.define({
       await file.write(contentNew)
       await file.write(contentNew)
     })()
     })()
 
 
-    const diff = createTwoFilesPatch(filepath, filepath, contentOld, contentNew)
+    const diff = trimDiff(
+      createTwoFilesPatch(filepath, filepath, contentOld, contentNew),
+    )
 
 
     FileTimes.read(ctx.sessionID, filepath)
     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")
+}