(
oldEditRange: string,
newEditRange: string,
cursorPosition: { line: number; character: number },
)
| 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 = |
no outgoing calls
no test coverage detected