| 1 | import { diffChars, diffLines, type Change } from "diff"; |
| 2 | |
| 3 | import { DiffChar, DiffLine } from ".."; |
| 4 | |
| 5 | export function convertMyersChangeToDiffLines(change: Change): DiffLine[] { |
| 6 | const type: DiffLine["type"] = change.added |
| 7 | ? "new" |
| 8 | : change.removed |
| 9 | ? "old" |
| 10 | : "same"; |
| 11 | const lines = change.value.split("\n"); |
| 12 | |
| 13 | // Ignore the \n at the end of the final line, if there is one |
| 14 | if (lines[lines.length - 1] === "") { |
| 15 | lines.pop(); |
| 16 | } |
| 17 | |
| 18 | return lines.map((line) => ({ type, line })); |
| 19 | } |
| 20 | |
| 21 | // The interpretation of lines in oldContent and newContent is the same as jsdiff |
| 22 | // Lines are separated by \n, with the exception that a trailing \n does *not* |
| 23 | // represent an empty line. |
| 24 | // |
| 25 | // The default for jsdiff is that "foo" and "foo\n" are *different* single-line |
| 26 | // contents, but we can't represent that: to avoid a diff |
| 27 | // [ { type: "old", line: "foo" }, { type: "new", line: "foo" } ], we |
| 28 | // pass ignoreNewlineAtEof: true. |
| 29 | export function myersDiff(oldContent: string, newContent: string): DiffLine[] { |
| 30 | const theirFormat = diffLines(oldContent, newContent, { |
| 31 | ignoreNewlineAtEof: true, |
| 32 | }); |
| 33 | let ourFormat = theirFormat.flatMap(convertMyersChangeToDiffLines); |
| 34 | |
| 35 | // Combine consecutive old/new pairs that are identical after trimming |
| 36 | for (let i = 0; i < ourFormat.length - 1; i++) { |
| 37 | if ( |
| 38 | ourFormat[i]?.type === "old" && |
| 39 | ourFormat[i + 1]?.type === "new" && |
| 40 | ourFormat[i].line.trim() === ourFormat[i + 1].line.trim() |
| 41 | ) { |
| 42 | ourFormat[i] = { type: "same", line: ourFormat[i].line }; |
| 43 | ourFormat.splice(i + 1, 1); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | // Remove trailing empty old lines |
| 48 | while ( |
| 49 | ourFormat.length > 0 && |
| 50 | ourFormat[ourFormat.length - 1].type === "old" && |
| 51 | ourFormat[ourFormat.length - 1].line === "" |
| 52 | ) { |
| 53 | ourFormat.pop(); |
| 54 | } |
| 55 | |
| 56 | return ourFormat; |
| 57 | } |
| 58 | |
| 59 | export function myersCharDiff( |
| 60 | oldContent: string, |
| 61 | newContent: string, |
| 62 | ): DiffChar[] { |
| 63 | // Process the content character by character. |
| 64 | // We will handle newlines separately, |
| 65 | // because diffChars does not have an option to ignore eol newlines. |
| 66 | const theirFormat = diffChars(oldContent, newContent); |
| 67 | |
| 68 | // Track indices as we process the diff. |
| 69 | let oldIndex = 0; |
| 70 | let newIndex = 0; |
| 71 | let oldLineIndex = 0; |
| 72 | let newLineIndex = 0; |
| 73 | let oldCharIndexInLine = 0; |
| 74 | let newCharIndexInLine = 0; |
| 75 | |
| 76 | const result: DiffChar[] = []; |
| 77 | |
| 78 | for (const change of theirFormat) { |
| 79 | // Split the change value by newlines to handle them separately. |
| 80 | if (change.value.includes("\n")) { |
| 81 | const parts = change.value.split(/(\n)/g); // This keeps the newlines as separate entries. |
| 82 | |
| 83 | for (let i = 0; i < parts.length; i++) { |
| 84 | const part = parts[i]; |
| 85 | if (part === "") continue; |
| 86 | |
| 87 | if (part === "\n") { |
| 88 | // Handle newline. |
| 89 | if (change.added) { |
| 90 | result.push({ |
| 91 | type: "new", |
| 92 | char: part, |
| 93 | newIndex: newIndex, |
| 94 | newLineIndex: newLineIndex, |
| 95 | newCharIndexInLine: newCharIndexInLine, |
| 96 | }); |
| 97 | newIndex += part.length; |
| 98 | newLineIndex++; |
| 99 | newCharIndexInLine = 0; // Reset when moving to a new line. |
| 100 | } else if (change.removed) { |
| 101 | result.push({ |
| 102 | type: "old", |
| 103 | char: part, |
| 104 | oldIndex: oldIndex, |
| 105 | oldLineIndex: oldLineIndex, |
| 106 | oldCharIndexInLine: oldCharIndexInLine, |
| 107 | }); |
| 108 | oldIndex += part.length; |
| 109 | oldLineIndex++; |
| 110 | oldCharIndexInLine = 0; // Reset when moving to a new line. |
| 111 | } else { |
| 112 | result.push({ |
| 113 | type: "same", |
| 114 | char: part, |
| 115 | oldIndex: oldIndex, |
| 116 | newIndex: newIndex, |
| 117 | oldLineIndex: oldLineIndex, |
| 118 | newLineIndex: newLineIndex, |
| 119 | oldCharIndexInLine: oldCharIndexInLine, |
| 120 | newCharIndexInLine: newCharIndexInLine, |
| 121 | }); |
| 122 | oldIndex += part.length; |
| 123 | newIndex += part.length; |
| 124 | oldLineIndex++; |
| 125 | newLineIndex++; |
| 126 | oldCharIndexInLine = 0; |
| 127 | newCharIndexInLine = 0; |
| 128 | } |
| 129 | } else { |
| 130 | // Handle regular text. |
| 131 | if (change.added) { |
| 132 | result.push({ |
| 133 | type: "new", |
| 134 | char: part, |
| 135 | newIndex: newIndex, |
| 136 | newLineIndex: newLineIndex, |
| 137 | newCharIndexInLine: newCharIndexInLine, |
| 138 | }); |
| 139 | newIndex += part.length; |
| 140 | newCharIndexInLine += part.length; |
| 141 | } else if (change.removed) { |
| 142 | result.push({ |
| 143 | type: "old", |
| 144 | char: part, |
| 145 | oldIndex: oldIndex, |
| 146 | oldLineIndex: oldLineIndex, |
| 147 | oldCharIndexInLine: oldCharIndexInLine, |
| 148 | }); |
| 149 | oldIndex += part.length; |
| 150 | oldCharIndexInLine += part.length; |
| 151 | } else { |
| 152 | result.push({ |
| 153 | type: "same", |
| 154 | char: part, |
| 155 | oldIndex: oldIndex, |
| 156 | newIndex: newIndex, |
| 157 | oldLineIndex: oldLineIndex, |
| 158 | newLineIndex: newLineIndex, |
| 159 | oldCharIndexInLine: oldCharIndexInLine, |
| 160 | newCharIndexInLine: newCharIndexInLine, |
| 161 | }); |
| 162 | oldIndex += part.length; |
| 163 | newIndex += part.length; |
| 164 | oldCharIndexInLine += part.length; |
| 165 | newCharIndexInLine += part.length; |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | } else { |
| 170 | // No newlines, handle as a simple change. |
| 171 | if (change.added) { |
| 172 | result.push({ |
| 173 | type: "new", |
| 174 | char: change.value, |
| 175 | newIndex: newIndex, |
| 176 | newLineIndex: newLineIndex, |
| 177 | newCharIndexInLine: newCharIndexInLine, |
| 178 | }); |
| 179 | newIndex += change.value.length; |
| 180 | newCharIndexInLine += change.value.length; |
| 181 | } else if (change.removed) { |
| 182 | result.push({ |
| 183 | type: "old", |
| 184 | char: change.value, |
| 185 | oldIndex: oldIndex, |
| 186 | oldLineIndex: oldLineIndex, |
| 187 | oldCharIndexInLine: oldCharIndexInLine, |
| 188 | }); |
| 189 | oldIndex += change.value.length; |
| 190 | oldCharIndexInLine += change.value.length; |
| 191 | } else { |
| 192 | result.push({ |
| 193 | type: "same", |
| 194 | char: change.value, |
| 195 | oldIndex: oldIndex, |
| 196 | newIndex: newIndex, |
| 197 | oldLineIndex: oldLineIndex, |
| 198 | newLineIndex: newLineIndex, |
| 199 | oldCharIndexInLine: oldCharIndexInLine, |
| 200 | newCharIndexInLine: newCharIndexInLine, |
| 201 | }); |
| 202 | oldIndex += change.value.length; |
| 203 | newIndex += change.value.length; |
| 204 | oldCharIndexInLine += change.value.length; |
| 205 | newCharIndexInLine += change.value.length; |
| 206 | } |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | return result; |
| 211 | } |