| 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, |