| 1337 | } |
| 1338 | |
| 1339 | function findTextDiff(oldText: string, newText: string): { oldSubstring: string; newSubstring: string; prefixLen: number } | null { |
| 1340 | if (oldText === newText) return null; |
| 1341 | |
| 1342 | let prefixLen = 0; |
| 1343 | while (prefixLen < oldText.length && prefixLen < newText.length && oldText[prefixLen] === newText[prefixLen]) { |
| 1344 | prefixLen++; |
| 1345 | } |
| 1346 | |
| 1347 | let oldSuffixStart = oldText.length; |
| 1348 | let newSuffixStart = newText.length; |
| 1349 | while ( |
| 1350 | oldSuffixStart > prefixLen && |
| 1351 | newSuffixStart > prefixLen && |
| 1352 | oldText[oldSuffixStart - 1] === newText[newSuffixStart - 1] |
| 1353 | ) { |
| 1354 | oldSuffixStart--; |
| 1355 | newSuffixStart--; |
| 1356 | } |
| 1357 | |
| 1358 | const oldSubstring = oldText.slice(prefixLen, oldSuffixStart); |
| 1359 | const newSubstring = newText.slice(prefixLen, newSuffixStart); |
| 1360 | |
| 1361 | if (!oldSubstring && !newSubstring) return null; |
| 1362 | return { oldSubstring, newSubstring, prefixLen }; |
| 1363 | } |
| 1364 | |
| 1365 | function anchorMatchesAt(renderedText: string, start: number, oldSubstring: string, textAnchor: TextEditAnchor): boolean { |
| 1366 | const oldLength = textAnchor.end - textAnchor.start; |