| 1 | import { Position } from "shiki"; |
| 2 | import { DiffLine } from "../.."; |
| 3 | import { myersDiff } from "../../diff/myers"; |
| 4 | |
| 5 | /** |
| 6 | * Given a diff of two editable regions, get the offset position at the last new line inside the editable region. |
| 7 | * @param diffLines Result of myersDiff. |
| 8 | * @param lineContentAtCursorPos Content of the line at cursor position. |
| 9 | * @param lineOffsetAtCursorPos Offset of the line at cursor position compared to the start of the editable region. |
| 10 | * @returns Offset position at last new line inside the editable region. |
| 11 | */ |
| 12 | export function getOffsetPositionAtLastNewLine( |
| 13 | diffLines: DiffLine[], |
| 14 | lineContentAtCursorPos: string, |
| 15 | lineOffsetAtCursorPos: number, |
| 16 | ): { |
| 17 | line: number; |
| 18 | character: number; |
| 19 | } { |
| 20 | let lastNewLineContent = ""; |
| 21 | let lineOffset = -1; |
| 22 | let currentResultLine = 0; |
| 23 | let hasChanges = false; |
| 24 | |
| 25 | // Build the string while tracking line numbers in the result |
| 26 | diffLines.reduce((acc, curr, i) => { |
| 27 | // Add the current line to our result |
| 28 | acc += curr.line; |
| 29 | |
| 30 | // Add newline if not the last line |
| 31 | if (i < diffLines.length - 1) { |
| 32 | acc += "\n"; |
| 33 | } |
| 34 | |
| 35 | // If this is a "new" or "same" line, it will be part of the result |
| 36 | if (curr.type === "new" || curr.type === "same") { |
| 37 | if (curr.type === "new") { |
| 38 | // If it's a new line, update our tracking |
| 39 | lastNewLineContent = curr.line; |
| 40 | lineOffset = currentResultLine; |
| 41 | hasChanges = true; |
| 42 | } |
| 43 | // Increment our position in the result |
| 44 | currentResultLine++; |
| 45 | } |
| 46 | |
| 47 | return acc; |
| 48 | }, ""); |
| 49 | |
| 50 | // If nothing has changed, return the original position |
| 51 | if (!hasChanges) { |
| 52 | lineOffset = lineOffsetAtCursorPos; |
| 53 | lastNewLineContent = lineContentAtCursorPos; |
| 54 | } |
| 55 | // Calculate the character position for the end of the last relevant line |
| 56 | const endOfCharPos = lastNewLineContent.length; |
| 57 | return { |
| 58 | line: lineOffset, |
| 59 | character: endOfCharPos, |
| 60 | }; |
| 61 | } |
| 62 | |
| 63 | export function getRenderableDiffWithGutterAnnotations( |
| 64 | diffLines: DiffLine[], |
| 65 | lineContentAtCursorPos: string, |
| 66 | lineOffsetAtCursorPos: number, |
| 67 | ): { |
| 68 | offset: { |
| 69 | line: number; |
| 70 | character: number; |
| 71 | }; |
| 72 | } { |
| 73 | let lastNewLineContent = ""; |
| 74 | let lineOffset = -1; |
| 75 | let currentResultLine = 0; |
| 76 | let hasChanges = false; |
| 77 | |
| 78 | // Build the string while tracking line numbers in the result |
| 79 | diffLines.reduce((acc, curr, i) => { |
| 80 | // Add the current line to our result |
| 81 | acc += curr.line; |
| 82 | |
| 83 | // Add newline if not the last line |
| 84 | if (i < diffLines.length - 1) { |
| 85 | acc += "\n"; |
| 86 | } |
| 87 | |
| 88 | // If this is a "new" or "same" line, it will be part of the result |
| 89 | if (curr.type === "new" || curr.type === "same") { |
| 90 | if (curr.type === "new") { |
| 91 | // If it's a new line, update our tracking |
| 92 | lastNewLineContent = curr.line; |
| 93 | lineOffset = currentResultLine; |
| 94 | hasChanges = true; |
| 95 | } |
| 96 | // Increment our position in the result |
| 97 | currentResultLine++; |
| 98 | } |
| 99 | |
| 100 | return acc; |
| 101 | }, ""); |
| 102 | |
| 103 | // If nothing has changed, return the original position |
| 104 | if (!hasChanges) { |
| 105 | lineOffset = lineOffsetAtCursorPos; |
| 106 | lastNewLineContent = lineContentAtCursorPos; |
| 107 | } |
| 108 | // Calculate the character position for the end of the last relevant line |
| 109 | const endOfCharPos = lastNewLineContent.length; |
| 110 | return { |
| 111 | offset: { |
| 112 | line: lineOffset, |
| 113 | character: endOfCharPos, |
| 114 | }, |
| 115 | }; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Check if the diff is indeed a FIM. |
| 120 | * @param oldEditRange Original string content. |
| 121 | * @param newEditRange New string content. |
| 122 | * @param cursorPosition The position of the cursor in the old string. |
| 123 | * @returns boolean indicating if the change is purely additive (FIM) |
| 124 | * @returns string of FIM text content. |
| 125 | */ |
| 126 | export function checkFim( |
| 127 | oldEditRange: string, |
| 128 | newEditRange: string, |
| 129 | cursorPosition: { line: number; character: number }, |
| 130 | ): |
| 131 | | { |
| 132 | isFim: true; |
| 133 | fimText: string; |
| 134 | } |
| 135 | | { |
| 136 | isFim: false; |
| 137 | fimText: null; |
| 138 | } { |
| 139 | // console.log("oldEditRange", oldEditRange); |
| 140 | // console.log("newEditRange", newEditRange); |
| 141 | // Find the common prefix. |
| 142 | let prefixLength = 0; |
| 143 | while ( |
| 144 | prefixLength < oldEditRange.length && |
| 145 | prefixLength < newEditRange.length && |
| 146 | oldEditRange[prefixLength] === newEditRange[prefixLength] |
| 147 | ) { |
| 148 | prefixLength++; |
| 149 | } |
| 150 | |
| 151 | // Find the common suffix |
| 152 | let oldSuffixPos = oldEditRange.length - 1; |
| 153 | let newSuffixPos = newEditRange.length - 1; |
| 154 | |
| 155 | while ( |
| 156 | oldSuffixPos >= prefixLength && |
| 157 | newSuffixPos >= prefixLength && |
| 158 | oldEditRange[oldSuffixPos] === newEditRange[newSuffixPos] |
| 159 | ) { |
| 160 | oldSuffixPos--; |
| 161 | newSuffixPos--; |
| 162 | } |
| 163 | |
| 164 | // The old text is purely preserved if: |
| 165 | // 1. The prefix ends before or at the cursor. |
| 166 | // 2. The suffix starts after or at the cursor. |
| 167 | // 3. There's no gap between prefix and suffix in the old text. |
| 168 | |
| 169 | const suffixStartInOld = oldSuffixPos + 1; |
| 170 | const suffixStartInNew = newSuffixPos + 1; |
| 171 | |
| 172 | // Convert cursor position to an offset in the string. |
| 173 | // For simplicity, we need to calculate the cursor's position in the string. |
| 174 | // This requires knowledge of line endings in the oldEditRange. |
| 175 | // const lines = oldEditRange.substring(0, prefixLength).split("\n"); |
| 176 | // const lines = oldEditRange.split("\n"); |
| 177 | // const cursorOffset = |
| 178 | // lines.length > 1 |
| 179 | // ? lines.slice(0, -1).reduce((sum, line) => sum + line.length + 1, 0) + |
| 180 | // cursorPosition.character |
| 181 | // : cursorPosition.character; |
| 182 | const oldEditLines = oldEditRange.split("\n"); |
| 183 | const cursorOffset = |
| 184 | oldEditLines |
| 185 | .slice(0, cursorPosition.line) |
| 186 | .reduce((sum, line) => sum + line.length + 1, 0) + |
| 187 | cursorPosition.character; |
| 188 | |
| 189 | // Check if the cursor is positioned between the prefix and suffix. |
| 190 | const cursorBetweenPrefixAndSuffix = |
| 191 | prefixLength <= cursorOffset && cursorOffset <= suffixStartInOld; |
| 192 | |
| 193 | // Check if the old text is completely preserved (no deletion). |
| 194 | const noTextDeleted = suffixStartInOld - prefixLength <= 0; |
| 195 | |
| 196 | const isFim = cursorBetweenPrefixAndSuffix && noTextDeleted; |
| 197 | |
| 198 | if (isFim) { |
| 199 | // Extract the content between prefix and suffix in the new string. |
| 200 | const fimText = newEditRange.substring(prefixLength, suffixStartInNew); |
| 201 | return { isFim, fimText }; |
| 202 | } else { |
| 203 | return { isFim, fimText: null }; |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | export function calculateFinalCursorPosition( |
| 208 | currCursorPos: Position, |
| 209 | editableRegionStartLine: number, |
| 210 | oldEditRangeSlice: string, |
| 211 | newEditRangeSlice: string, |
| 212 | ) { |
| 213 | // How far away is the current line from the start of the editable region? |
| 214 | const lineOffsetAtCursorPos = currCursorPos.line - editableRegionStartLine; |
| 215 | |
| 216 | // How long is the line at the current cursor position? |
| 217 | const lineContentAtCursorPos = |
| 218 | newEditRangeSlice.split("\n")[lineOffsetAtCursorPos]; |
| 219 | |
| 220 | const diffLines = myersDiff(oldEditRangeSlice, newEditRangeSlice); |
| 221 | |
| 222 | const offset = getOffsetPositionAtLastNewLine( |
| 223 | diffLines, |
| 224 | lineContentAtCursorPos, |
| 225 | lineOffsetAtCursorPos, |
| 226 | ); |
| 227 | |
| 228 | // Calculate the actual line number in the editor by adding the startPos offset |
| 229 | // to the line number from the diff calculation. |
| 230 | const finalCursorPos: Position = { |
| 231 | line: editableRegionStartLine + offset.line, |
| 232 | character: offset.character, |
| 233 | }; |
| 234 | |
| 235 | return finalCursorPos; |
| 236 | } |
| 237 | |
| 238 | /** |
| 239 | * Applies a completion to file content by replacing lines starting from a specific line number |
| 240 | * |
| 241 | * @param fileContent The original file content |
| 242 | * @param completion The completion text to apply |
| 243 | * @param startLineNumber The line number (0-based) where replacement should start |
| 244 | * @param linesToReplace Optional number of lines to replace; if not provided, will replace the same number of lines as in the completion |
| 245 | * @returns The file content with the completion applied |
| 246 | */ |
| 247 | export function applyCompletionToFile( |
| 248 | fileContent: string, |
| 249 | completion: string, |
| 250 | startLineNumber: number, |
| 251 | linesToReplace?: number, |
| 252 | ): string { |
| 253 | const lines = fileContent.split("\n"); |
| 254 | const completionLines = completion.split("\n"); |
| 255 | |
| 256 | // Determine how many lines to replace |
| 257 | const numLinesToReplace = |
| 258 | linesToReplace !== undefined ? linesToReplace : completionLines.length; |
| 259 | |
| 260 | // Replace the lines |
| 261 | const newLines = [ |
| 262 | ...lines.slice(0, startLineNumber), |
| 263 | ...completionLines, |
| 264 | ...lines.slice(startLineNumber + numLinesToReplace), |
| 265 | ]; |
| 266 | |
| 267 | return newLines.join("\n"); |
| 268 | } |