(existingLines: string[], edits: PlanEdit[])
| 29 | * state BEFORE any edits in this batch (offsets are adjusted internally) |
| 30 | */ |
| 31 | export function applyEdits(existingLines: string[], edits: PlanEdit[]): string[] { |
| 32 | const sorted = [...edits].sort((a, b) => a.start - b.start); |
| 33 | const lines = [...existingLines]; |
| 34 | let offset = 0; |
| 35 | |
| 36 | for (const edit of sorted) { |
| 37 | const start = edit.start - 1 + offset; |
| 38 | const end = edit.end != null |
| 39 | ? edit.end + offset |
| 40 | : lines.length; |
| 41 | |
| 42 | const newLines = edit.content ? edit.content.split("\n") : []; |
| 43 | const removedCount = end - start; |
| 44 | lines.splice(start, removedCount, ...newLines); |
| 45 | offset += newLines.length - removedCount; |
| 46 | } |
| 47 | |
| 48 | return lines; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Validate a batch of edits against the current file state. |
no outgoing calls
no test coverage detected