| 120 | const defaultOccurrence = intent.edit.occurrence ?? 1 |
| 121 | |
| 122 | const findAnchorLine = ( |
| 123 | anchor: string, |
| 124 | occurrence = defaultOccurrence, |
| 125 | afterIndex = -1 |
| 126 | ): { index: number; error?: string } => { |
| 127 | const trimmed = anchor.trim() |
| 128 | let count = 0 |
| 129 | for (let i = afterIndex + 1; i < lines.length; i++) { |
| 130 | if (lines[i].trim() === trimmed) { |
| 131 | count++ |
| 132 | if (count === occurrence) return { index: i } |
| 133 | } |
| 134 | } |
| 135 | if (count === 0) { |
| 136 | return { |
| 137 | index: -1, |
| 138 | error: `Anchor line not found in "${fileRecord.name}": "${anchor.slice(0, 100)}"`, |
| 139 | } |
| 140 | } |
| 141 | return { |
| 142 | index: -1, |
| 143 | error: `Anchor line occurrence ${occurrence} not found (only ${count} match${count > 1 ? 'es' : ''}) in "${fileRecord.name}": "${anchor.slice(0, 100)}"`, |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | if (intent.edit.mode === 'replace_between') { |
| 148 | if (!intent.edit.before_anchor || !intent.edit.after_anchor) { |