( filePath: string, oldContent: string, newContent: string, editMode: 'single' | 'multiple', )
| 168 | * to apply any edits the user may have made to the new contents. |
| 169 | */ |
| 170 | export function computeEditsFromContents( |
| 171 | filePath: string, |
| 172 | oldContent: string, |
| 173 | newContent: string, |
| 174 | editMode: 'single' | 'multiple', |
| 175 | ): FileEdit[] { |
| 176 | // Use unformatted patches, otherwise the edits will be formatted. |
| 177 | const singleHunk = editMode === 'single' |
| 178 | const patch = getPatchFromContents({ |
| 179 | filePath, |
| 180 | oldContent, |
| 181 | newContent, |
| 182 | singleHunk, |
| 183 | }) |
| 184 | |
| 185 | if (patch.length === 0) { |
| 186 | return [] |
| 187 | } |
| 188 | |
| 189 | // For single edit mode, verify we only got one hunk |
| 190 | if (singleHunk && patch.length > 1) { |
| 191 | logError( |
| 192 | new Error( |
| 193 | `Unexpected number of hunks: ${patch.length}. Expected 1 hunk.`, |
| 194 | ), |
| 195 | ) |
| 196 | } |
| 197 | |
| 198 | // Re-compute the edits to match the patch |
| 199 | return getEditsForPatch(patch) |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * Done if: |
no test coverage detected