| 1 | import { EditOperation } from "core/tools/definitions/multiEdit"; |
| 2 | |
| 3 | export const FOUND_MULTIPLE_FIND_STRINGS_ERROR = |
| 4 | "Either provide a more specific string with surrounding context to make it unique, or use replace_all=true to replace all occurrences."; |
| 5 | |
| 6 | /** |
| 7 | * Performs a find and replace operation on text content with proper handling of special characters |
| 8 | */ |
| 9 | export function performFindAndReplace( |
| 10 | content: string, |
| 11 | oldString: string, |
| 12 | newString: string, |
| 13 | replaceAll: boolean = false, |
| 14 | index?: number, // For error messages |
| 15 | ): string { |
| 16 | const errorContext = index !== undefined ? `edit at index ${index}: ` : ""; |
| 17 | // Check if old_string exists in current content |
| 18 | if (!content.includes(oldString)) { |
| 19 | throw new Error(`${errorContext}string not found in file: "${oldString}"`); |
| 20 | } |
| 21 | |
| 22 | if (replaceAll) { |
| 23 | // Replace all occurrences using replaceAll for proper handling of special characters |
| 24 | return content.replaceAll(oldString, newString); |
| 25 | } else { |
| 26 | // Handle empty oldString case (insertion) |
| 27 | if (oldString === "") { |
| 28 | return newString + content; |
| 29 | } |
| 30 | |
| 31 | // Count occurrences using indexOf for proper handling of special characters |
| 32 | let count = 0; |
| 33 | let searchIndex = content.indexOf(oldString); |
| 34 | while (searchIndex !== -1) { |
| 35 | count++; |
| 36 | searchIndex = content.indexOf(oldString, searchIndex + 1); |
| 37 | } |
| 38 | |
| 39 | if (count > 1) { |
| 40 | throw new Error( |
| 41 | `${errorContext}String "${oldString}" appears ${count} times in the file. ${FOUND_MULTIPLE_FIND_STRINGS_ERROR}`, |
| 42 | ); |
| 43 | } |
| 44 | |
| 45 | // Replace only the first occurrence |
| 46 | const firstIndex = content.indexOf(oldString); |
| 47 | return ( |
| 48 | content.substring(0, firstIndex) + |
| 49 | newString + |
| 50 | content.substring(firstIndex + oldString.length) |
| 51 | ); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Validates a single edit operation |
| 57 | */ |
| 58 | export function validateSingleEdit( |
| 59 | oldString: string, |
| 60 | newString: string, |
| 61 | index?: number, |
| 62 | ): void { |
| 63 | const context = index !== undefined ? `edit at index ${index}: ` : ""; |
| 64 | |
| 65 | if (!oldString && oldString !== "") { |
| 66 | throw new Error(`${context}old_string is required`); |
| 67 | } |
| 68 | if (newString === undefined) { |
| 69 | throw new Error(`${context}new_string is required`); |
| 70 | } |
| 71 | if (oldString === newString) { |
| 72 | throw new Error(`${context}old_string and new_string must be different`); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | export const EMPTY_NON_FIRST_EDIT_MESSAGE = |
| 77 | "contains empty old_string. Only the first edit can contain an empty old_string, which is only used for file creation."; |
| 78 | export function validateCreatingForMultiEdit(edits: EditOperation[]) { |
| 79 | const isCreating = edits[0].old_string === ""; |
| 80 | if (edits.length > 1) { |
| 81 | if (isCreating) { |
| 82 | throw new Error( |
| 83 | "cannot make subsequent edits on a file you are creating", |
| 84 | ); |
| 85 | } else { |
| 86 | for (let i = 1; i < edits.length; i++) { |
| 87 | if (edits[i].old_string === "") { |
| 88 | throw new Error( |
| 89 | `edit at index ${i}: ${EMPTY_NON_FIRST_EDIT_MESSAGE}`, |
| 90 | ); |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | return isCreating; |
| 97 | } |
| 98 | |
| 99 | export function trimEmptyLines({ |
| 100 | lines, |
| 101 | fromEnd, |
| 102 | }: { |
| 103 | lines: string[]; |
| 104 | fromEnd: boolean; |
| 105 | }): string[] { |
| 106 | lines = fromEnd ? lines.toReversed() : lines.slice(); |
| 107 | const newLines: string[] = []; |
| 108 | let shouldContinueRemoving = true; |
| 109 | for (let index = 0; index < lines.length; index++) { |
| 110 | const line = lines[index]; |
| 111 | if (shouldContinueRemoving && line.trim() === "") continue; |
| 112 | shouldContinueRemoving = false; |
| 113 | newLines.push(line); |
| 114 | } |
| 115 | return fromEnd ? newLines.reverse() : newLines; |
| 116 | } |