( filePath: string, chunks: UpdateFileChunk[], originalText: string, )
| 305 | } |
| 306 | |
| 307 | export function deriveNewContentsFromChunks( |
| 308 | filePath: string, |
| 309 | chunks: UpdateFileChunk[], |
| 310 | originalText: string, |
| 311 | ): ApplyPatchFileUpdate { |
| 312 | const originalContent = Bom.split(originalText) |
| 313 | |
| 314 | let originalLines = originalContent.text.split("\n") |
| 315 | |
| 316 | // Drop trailing empty element for consistent line counting |
| 317 | if (originalLines.length > 0 && originalLines[originalLines.length - 1] === "") { |
| 318 | originalLines.pop() |
| 319 | } |
| 320 | |
| 321 | const replacements = computeReplacements(originalLines, filePath, chunks) |
| 322 | let newLines = applyReplacements(originalLines, replacements) |
| 323 | |
| 324 | // Ensure trailing newline |
| 325 | if (newLines.length === 0 || newLines[newLines.length - 1] !== "") { |
| 326 | newLines.push("") |
| 327 | } |
| 328 | |
| 329 | const next = Bom.split(newLines.join("\n")) |
| 330 | const newContent = next.text |
| 331 | |
| 332 | // Generate unified diff |
| 333 | const unifiedDiff = generateUnifiedDiff(originalContent.text, newContent) |
| 334 | |
| 335 | return { |
| 336 | unified_diff: unifiedDiff, |
| 337 | content: newContent, |
| 338 | bom: originalContent.bom || next.bom, |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | function computeReplacements( |
| 343 | originalLines: string[], |
no test coverage detected