MCPcopy Index your code
hub / github.com/anomalyco/opencode / trimDiff

Function trimDiff

packages/opencode/src/tool/edit.ts:646–680  ·  view source on GitHub ↗
(diff: string)

Source from the content-addressed store, hash-verified

644}
645
646export function trimDiff(diff: string): string {
647 const lines = diff.split("\n")
648 const contentLines = lines.filter(
649 (line) =>
650 (line.startsWith("+") || line.startsWith("-") || line.startsWith(" ")) &&
651 !line.startsWith("---") &&
652 !line.startsWith("+++"),
653 )
654
655 if (contentLines.length === 0) return diff
656
657 let min = Infinity
658 for (const line of contentLines) {
659 const content = line.slice(1)
660 if (content.trim().length > 0) {
661 const match = content.match(/^(\s*)/)
662 if (match) min = Math.min(min, match[1].length)
663 }
664 }
665 if (min === Infinity || min === 0) return diff
666 const trimmedLines = lines.map((line) => {
667 if (
668 (line.startsWith("+") || line.startsWith("-") || line.startsWith(" ")) &&
669 !line.startsWith("---") &&
670 !line.startsWith("+++")
671 ) {
672 const prefix = line[0]
673 const content = line.slice(1)
674 return prefix + content.slice(min)
675 }
676 return line
677 })
678
679 return trimmedLines.join("\n")
680}
681
682export function replace(content: string, oldString: string, newString: string, replaceAll = false): string {
683 if (oldString === newString) {

Callers 3

write.tsFile · 0.90
apply_patch.tsFile · 0.90
edit.tsFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected