MCPcopy Create free account
hub / github.com/MatterAIOrg/OrbCode / applyEdit

Function applyEdit

src/tools/executors/files.ts:65–86  ·  view source on GitHub ↗
(content: string, edit: EditSpec)

Source from the content-addressed store, hash-verified

63}
64
65function applyEdit(content: string, edit: EditSpec): { content: string; error?: string } {
66 const { old_string, new_string } = edit
67 const replace_all = parseReplaceAll(edit.replace_all)
68 if (old_string === new_string) {
69 return { content, error: "old_string and new_string are identical" }
70 }
71 if (old_string === "") {
72 // Empty old_string replaces the entire file.
73 return { content: new_string }
74 }
75 const occurrences = content.split(old_string).length - 1
76 if (occurrences === 0) {
77 return { content, error: "old_string not found in file" }
78 }
79 if (occurrences > 1 && !replace_all) {
80 return {
81 content,
82 error: `old_string matched ${occurrences} times; provide more context for a unique match or set replace_all to true`,
83 }
84 }
85 return { content: content.split(old_string).join(new_string) }
86}
87
88function editOneFile(cwd: string, edits: EditSpec[]): string[] {
89 const filePath = resolveWorkspacePath(cwd, edits[0].file_path)

Callers 2

editOneFileFunction · 0.85
previewFileChangeFunction · 0.85

Calls 1

parseReplaceAllFunction · 0.85

Tested by

no test coverage detected