| 1 | // Search and replace block markers using flexible regex patterns |
| 2 | const SEARCH_BLOCK_START_REGEX = /^[-]{3,} SEARCH$/; |
| 3 | const SEARCH_BLOCK_END_REGEX = /^[=]{3,}$/; |
| 4 | const REPLACE_BLOCK_END_REGEX = /^[+]{3,} REPLACE$/; |
| 5 | |
| 6 | export interface SearchReplaceBlockResult { |
| 7 | /** Whether a complete search/replace block has been parsed */ |
| 8 | isComplete: boolean; |
| 9 | /** The content to search for in the file */ |
| 10 | searchContent?: string; |
| 11 | /** The content to replace the search content with */ |
| 12 | replaceContent?: string; |
| 13 | /** Error message if parsing failed */ |
| 14 | error?: string; |
| 15 | } |
| 16 | |
| 17 | /** |
| 18 | * Parses a search and replace block from content. |
| 19 | * |
| 20 | * Expected format: |
| 21 | * ------- SEARCH |
| 22 | * [content to find] |
| 23 | * ======= |
| 24 | * [content to replace with] |
| 25 | * +++++++ REPLACE |
| 26 | * |
| 27 | * @param content - The content to parse |
| 28 | * @returns ParseResult with completion status and extracted content |
| 29 | */ |
| 30 | export function parseSearchReplaceBlock( |
| 31 | content: string, |
| 32 | ): SearchReplaceBlockResult { |
| 33 | const lines = content.split("\n"); |
| 34 | |
| 35 | let currentSearchContent = ""; |
| 36 | let currentReplaceContent = ""; |
| 37 | let inSearch = false; |
| 38 | let inReplace = false; |
| 39 | |
| 40 | for (const line of lines) { |
| 41 | if (SEARCH_BLOCK_START_REGEX.test(line)) { |
| 42 | inSearch = true; |
| 43 | inReplace = false; |
| 44 | currentSearchContent = ""; |
| 45 | currentReplaceContent = ""; |
| 46 | continue; |
| 47 | } |
| 48 | |
| 49 | if (SEARCH_BLOCK_END_REGEX.test(line)) { |
| 50 | if (!inSearch) { |
| 51 | return { |
| 52 | isComplete: false, |
| 53 | error: "Found search block end marker without matching start marker", |
| 54 | }; |
| 55 | } |
| 56 | inSearch = false; |
| 57 | inReplace = true; |
| 58 | continue; |
| 59 | } |
| 60 | |
| 61 | if (REPLACE_BLOCK_END_REGEX.test(line)) { |
| 62 | if (!inReplace) { |
| 63 | return { |
| 64 | isComplete: false, |
| 65 | error: |
| 66 | "Found replace block end marker without matching replace start marker", |
| 67 | }; |
| 68 | } |
| 69 | |
| 70 | // Complete block found |
| 71 | return { |
| 72 | isComplete: true, |
| 73 | searchContent: currentSearchContent.trim(), |
| 74 | replaceContent: currentReplaceContent.trim(), |
| 75 | }; |
| 76 | } |
| 77 | |
| 78 | // Accumulate content |
| 79 | if (inSearch) { |
| 80 | currentSearchContent += line + "\n"; |
| 81 | } else if (inReplace) { |
| 82 | currentReplaceContent += line + "\n"; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | // Block is not yet complete |
| 87 | return { |
| 88 | isComplete: false, |
| 89 | searchContent: currentSearchContent.trim(), |
| 90 | replaceContent: currentReplaceContent.trim(), |
| 91 | }; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Parses all complete search and replace blocks from content. |
| 96 | * |
| 97 | * @param content - The content containing multiple search/replace blocks |
| 98 | * @returns Array of complete search/replace blocks found |
| 99 | * @throws Error if any malformed blocks are encountered |
| 100 | */ |
| 101 | export function parseAllSearchReplaceBlocks( |
| 102 | content: string, |
| 103 | ): SearchReplaceBlockResult[] { |
| 104 | const blocks: SearchReplaceBlockResult[] = []; |
| 105 | const lines = content.split("\n"); |
| 106 | |
| 107 | let currentBlockLines: string[] = []; |
| 108 | let inBlock = false; |
| 109 | |
| 110 | for (const line of lines) { |
| 111 | if (SEARCH_BLOCK_START_REGEX.test(line)) { |
| 112 | // Start of new block |
| 113 | if (inBlock && currentBlockLines.length > 0) { |
| 114 | // Process previous incomplete block if any |
| 115 | const blockContent = currentBlockLines.join("\n"); |
| 116 | const result = parseSearchReplaceBlock(blockContent); |
| 117 | if (result.isComplete) { |
| 118 | blocks.push(result); |
| 119 | } else if (result.error) { |
| 120 | // Fail fast on any parsing error |
| 121 | throw new Error(result.error); |
| 122 | } |
| 123 | // If incomplete but no error, it's just a partial block - continue processing |
| 124 | } |
| 125 | currentBlockLines = [line]; |
| 126 | inBlock = true; |
| 127 | } else if (inBlock) { |
| 128 | currentBlockLines.push(line); |
| 129 | |
| 130 | // Check if this line completes the current block |
| 131 | if (REPLACE_BLOCK_END_REGEX.test(line)) { |
| 132 | const blockContent = currentBlockLines.join("\n"); |
| 133 | const result = parseSearchReplaceBlock(blockContent); |
| 134 | if (result.isComplete) { |
| 135 | blocks.push(result); |
| 136 | } else if (result.error) { |
| 137 | // Fail fast on any parsing error |
| 138 | throw new Error(result.error); |
| 139 | } |
| 140 | currentBlockLines = []; |
| 141 | inBlock = false; |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | return blocks; |
| 147 | } |