(content: string, edit: EditSpec)
| 63 | } |
| 64 | |
| 65 | function 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 | |
| 88 | function editOneFile(cwd: string, edits: EditSpec[]): string[] { |
| 89 | const filePath = resolveWorkspacePath(cwd, edits[0].file_path) |
no test coverage detected