(originalContent: string, filePath: string, chunks: UpdateFileChunk[])
| 121 | * @returns The new file content |
| 122 | */ |
| 123 | export function applyChunksToContent(originalContent: string, filePath: string, chunks: UpdateFileChunk[]): string { |
| 124 | // Split content into lines |
| 125 | let originalLines = originalContent.split("\n") |
| 126 | |
| 127 | // Drop trailing empty element that results from final newline |
| 128 | // so that line counts match standard diff behavior |
| 129 | if (originalLines.length > 0 && originalLines[originalLines.length - 1] === "") { |
| 130 | originalLines = originalLines.slice(0, -1) |
| 131 | } |
| 132 | |
| 133 | const replacements = computeReplacements(originalLines, filePath, chunks) |
| 134 | let newLines = applyReplacements(originalLines, replacements) |
| 135 | |
| 136 | // Ensure file ends with newline |
| 137 | if (newLines.length === 0 || newLines[newLines.length - 1] !== "") { |
| 138 | newLines = [...newLines, ""] |
| 139 | } |
| 140 | |
| 141 | return newLines.join("\n") |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Process a single hunk and return the file change. |
no test coverage detected