( currCursorPos: Position, editableRegionStartLine: number, oldEditRangeSlice: string, newEditRangeSlice: string, )
| 205 | } |
| 206 | |
| 207 | export function calculateFinalCursorPosition( |
| 208 | currCursorPos: Position, |
| 209 | editableRegionStartLine: number, |
| 210 | oldEditRangeSlice: string, |
| 211 | newEditRangeSlice: string, |
| 212 | ) { |
| 213 | if (newEditRangeSlice === "") { |
| 214 | return currCursorPos; |
| 215 | } |
| 216 | // How far away is the current line from the start of the editable region? |
| 217 | const lineOffsetAtCursorPos = currCursorPos.line - editableRegionStartLine; |
| 218 | |
| 219 | // How long is the line at the current cursor position? |
| 220 | const lineContentAtCursorPos = |
| 221 | newEditRangeSlice.split("\n")[lineOffsetAtCursorPos]; |
| 222 | |
| 223 | const diffLines = myersDiff(oldEditRangeSlice, newEditRangeSlice); |
| 224 | |
| 225 | const offset = getOffsetPositionAtLastNewLine( |
| 226 | diffLines, |
| 227 | lineContentAtCursorPos, |
| 228 | lineOffsetAtCursorPos, |
| 229 | ); |
| 230 | |
| 231 | // Calculate the actual line number in the editor by adding the startPos offset |
| 232 | // to the line number from the diff calculation. |
| 233 | const finalCursorPos: Position = { |
| 234 | line: editableRegionStartLine + offset.line, |
| 235 | character: offset.character, |
| 236 | }; |
| 237 | |
| 238 | return finalCursorPos; |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * Applies a completion to file content by replacing lines starting from a specific line number |
no test coverage detected