| 1 | import { DiffLine } from "../.."; |
| 2 | |
| 3 | /** |
| 4 | * Given a diff of two editable regions, get the offset position at the last new line inside the editable region. |
| 5 | * @param diffLines Result of myersDiff. |
| 6 | * @param lineContentAtCursorPos Content of the line at cursor position. |
| 7 | * @param lineOffsetAtCursorPos Offset of the line at cursor position compared to the start of the editable region. |
| 8 | * @returns Offset position at last new line inside the editable region. |
| 9 | */ |
| 10 | export function getOffsetPositionAtLastNewLine( |
| 11 | diffLines: DiffLine[], |
| 12 | lineContentAtCursorPos: string, |
| 13 | lineOffsetAtCursorPos: number, |
| 14 | ): { |
| 15 | line: number; |
| 16 | character: number; |
| 17 | } { |
| 18 | let lastNewLineContent = ""; |
| 19 | let lineOffset = -1; |
| 20 | let currentResultLine = 0; |
| 21 | let hasChanges = false; |
| 22 | |
| 23 | // Build the string while tracking line numbers in the result |
| 24 | diffLines.reduce((acc, curr, i) => { |
| 25 | // Add the current line to our result |
| 26 | acc += curr.line; |
| 27 | |
| 28 | // Add newline if not the last line |
| 29 | if (i < diffLines.length - 1) { |
| 30 | acc += "\n"; |
| 31 | } |
| 32 | |
| 33 | // If this is a "new" or "same" line, it will be part of the result |
| 34 | if (curr.type === "new" || curr.type === "same") { |
| 35 | if (curr.type === "new") { |
| 36 | // If it's a new line, update our tracking |
| 37 | lastNewLineContent = curr.line; |
| 38 | lineOffset = currentResultLine; |
| 39 | hasChanges = true; |
| 40 | } |
| 41 | // Increment our position in the result |
| 42 | currentResultLine++; |
| 43 | } |
| 44 | |
| 45 | return acc; |
| 46 | }, ""); |
| 47 | |
| 48 | // If nothing has changed, return the original position |
| 49 | if (!hasChanges) { |
| 50 | lineOffset = lineOffsetAtCursorPos; |
| 51 | lastNewLineContent = lineContentAtCursorPos; |
| 52 | } |
| 53 | // Calculate the character position for the end of the last relevant line |
| 54 | const endOfCharPos = lastNewLineContent.length; |
| 55 | return { |
| 56 | line: lineOffset, |
| 57 | character: endOfCharPos, |
| 58 | }; |
| 59 | } |
| 60 | |
| 61 | export function getRenderableDiffWithGutterAnnotations( |
| 62 | diffLines: DiffLine[], |
| 63 | lineContentAtCursorPos: string, |
| 64 | lineOffsetAtCursorPos: number, |
| 65 | ): { |
| 66 | offset: { |
| 67 | line: number; |
| 68 | character: number; |
| 69 | }; |
| 70 | } { |
| 71 | let lastNewLineContent = ""; |
| 72 | let lineOffset = -1; |
| 73 | let currentResultLine = 0; |
| 74 | let hasChanges = false; |
| 75 | |
| 76 | // Build the string while tracking line numbers in the result |
| 77 | diffLines.reduce((acc, curr, i) => { |
| 78 | // Add the current line to our result |
| 79 | acc += curr.line; |
| 80 | |
| 81 | // Add newline if not the last line |
| 82 | if (i < diffLines.length - 1) { |
| 83 | acc += "\n"; |
| 84 | } |
| 85 | |
| 86 | // If this is a "new" or "same" line, it will be part of the result |
| 87 | if (curr.type === "new" || curr.type === "same") { |
| 88 | if (curr.type === "new") { |
| 89 | // If it's a new line, update our tracking |
| 90 | lastNewLineContent = curr.line; |
| 91 | lineOffset = currentResultLine; |
| 92 | hasChanges = true; |
| 93 | } |
| 94 | // Increment our position in the result |
| 95 | currentResultLine++; |
| 96 | } |
| 97 | |
| 98 | return acc; |
| 99 | }, ""); |
| 100 | |
| 101 | // If nothing has changed, return the original position |
| 102 | if (!hasChanges) { |
| 103 | lineOffset = lineOffsetAtCursorPos; |
| 104 | lastNewLineContent = lineContentAtCursorPos; |
| 105 | } |
| 106 | // Calculate the character position for the end of the last relevant line |
| 107 | const endOfCharPos = lastNewLineContent.length; |
| 108 | return { |
| 109 | offset: { |
| 110 | line: lineOffset, |
| 111 | character: endOfCharPos, |
| 112 | }, |
| 113 | }; |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Check if the diff is indeed a FIM. |
| 118 | * @param oldEditRange Original string content. |
| 119 | * @param newEditRange New string content. |
| 120 | * @param cursorPosition The position of the cursor in the old string. |
| 121 | * @returns boolean indicating if the change is purely additive (FIM) |
| 122 | * @returns string of FIM text content. |
| 123 | */ |
| 124 | export function checkFim( |
| 125 | oldEditRange: string, |
| 126 | newEditRange: string, |
| 127 | cursorPosition: { line: number; character: number }, |
| 128 | ): |
| 129 | | { |
| 130 | isFim: true; |
| 131 | fimText: string; |
| 132 | } |
| 133 | | { |
| 134 | isFim: false; |
| 135 | fimText: null; |
| 136 | } { |
| 137 | // Find the common prefix. |
| 138 | let prefixLength = 0; |
| 139 | while ( |
| 140 | prefixLength < oldEditRange.length && |
| 141 | prefixLength < newEditRange.length && |
| 142 | oldEditRange[prefixLength] === newEditRange[prefixLength] |
| 143 | ) { |
| 144 | prefixLength++; |
| 145 | } |
| 146 | |
| 147 | // Find the common suffix |
| 148 | let oldSuffixPos = oldEditRange.length - 1; |
| 149 | let newSuffixPos = newEditRange.length - 1; |
| 150 | |
| 151 | while ( |
| 152 | oldSuffixPos >= prefixLength && |
| 153 | newSuffixPos >= prefixLength && |
| 154 | oldEditRange[oldSuffixPos] === newEditRange[newSuffixPos] |
| 155 | ) { |
| 156 | oldSuffixPos--; |
| 157 | newSuffixPos--; |
| 158 | } |
| 159 | |
| 160 | // The old text is purely preserved if: |
| 161 | // 1. The prefix ends before or at the cursor. |
| 162 | // 2. The suffix starts after or at the cursor. |
| 163 | // 3. There's no gap between prefix and suffix in the old text. |
| 164 | |
| 165 | const suffixStartInOld = oldSuffixPos + 1; |
| 166 | const suffixStartInNew = newSuffixPos + 1; |
| 167 | |
| 168 | // Convert cursor position to an offset in the string. |
| 169 | // For simplicity, we need to calculate the cursor's position in the string. |
| 170 | // This requires knowledge of line endings in the oldEditRange. |
| 171 | const lines = oldEditRange.substring(0, prefixLength).split("\n"); |
| 172 | const cursorOffset = |
| 173 | lines.length > 1 |
| 174 | ? lines.slice(0, -1).reduce((sum, line) => sum + line.length + 1, 0) + |
| 175 | cursorPosition.character |
| 176 | : cursorPosition.character; |
| 177 | |
| 178 | // Check if the cursor is positioned between the prefix and suffix. |
| 179 | const cursorBetweenPrefixAndSuffix = |
| 180 | prefixLength <= cursorOffset && cursorOffset <= suffixStartInOld; |
| 181 | |
| 182 | // Check if the old text is completely preserved (no deletion). |
| 183 | const noTextDeleted = suffixStartInOld - prefixLength <= 0; |
| 184 | |
| 185 | const isFim = cursorBetweenPrefixAndSuffix && noTextDeleted; |
| 186 | |
| 187 | if (isFim) { |
| 188 | // Extract the content between prefix and suffix in the new string. |
| 189 | const fimText = newEditRange.substring(prefixLength, suffixStartInNew); |
| 190 | return { isFim, fimText }; |
| 191 | } else { |
| 192 | return { isFim, fimText: null }; |
| 193 | } |
| 194 | } |