continue-custom/core/diff/myers.ts
2024-09-13 19:31:58 -07:00

25 lines
731 B
TypeScript

import { diffLines, type Change } from "diff";
import { DiffLine } from "..";
export function convertMyersChangeToDiffLines(change: Change): DiffLine[] {
const type: DiffLine["type"] = change.added
? "new"
: change.removed
? "old"
: "same";
const lines = change.value.split("\n");
// `diff` package is always adding an extra new line at the end of the array
if (lines[lines.length - 1] === "") {
lines.pop();
}
return lines.map((line) => ({ type, line }));
}
export function myersDiff(oldContent: string, newContent: string): DiffLine[] {
const theirFormat = diffLines(oldContent, newContent);
const ourFormat = theirFormat.flatMap(convertMyersChangeToDiffLines);
return ourFormat;
}