feat: keep track of indices
This commit is contained in:
parent
e0129c45b4
commit
c36b27da03
3 changed files with 83 additions and 41 deletions
|
|
@ -62,24 +62,62 @@ export function myersCharDiff(
|
|||
): DiffChar[] {
|
||||
const theirFormat = diffChars(oldContent, newContent);
|
||||
console.log(theirFormat);
|
||||
const ourFormat = theirFormat.flatMap(convertMyersChangeToChars);
|
||||
|
||||
// Track indices as we process the diff.
|
||||
let oldIndex = 0;
|
||||
let newIndex = 0;
|
||||
const ourFormat = theirFormat.flatMap((change) => {
|
||||
const result = convertMyersChangeToChars(change, oldIndex, newIndex);
|
||||
|
||||
// Update indices based on the change type.
|
||||
if (change.added) {
|
||||
newIndex += change.value.length;
|
||||
} else if (change.removed) {
|
||||
oldIndex += change.value.length;
|
||||
} else {
|
||||
oldIndex += change.value.length;
|
||||
newIndex += change.value.length;
|
||||
}
|
||||
|
||||
return result;
|
||||
});
|
||||
|
||||
return ourFormat;
|
||||
}
|
||||
|
||||
// NOTE: I'm not sure if we need this tbh.
|
||||
// diffChars already gives us a pretty decent format.
|
||||
// This is mostly to match parity with the line diff.
|
||||
function convertMyersChangeToChars(change: {
|
||||
value: string;
|
||||
added?: boolean;
|
||||
removed?: boolean;
|
||||
}): DiffChar[] {
|
||||
function convertMyersChangeToChars(
|
||||
change: {
|
||||
value: string;
|
||||
added?: boolean;
|
||||
removed?: boolean;
|
||||
},
|
||||
oldIndex: number,
|
||||
newIndex: number,
|
||||
): DiffChar[] {
|
||||
if (change.added) {
|
||||
return [{ type: "new", char: change.value }];
|
||||
return [
|
||||
{
|
||||
type: "new",
|
||||
char: change.value,
|
||||
newIndex: newIndex,
|
||||
},
|
||||
];
|
||||
} else if (change.removed) {
|
||||
return [{ type: "old", char: change.value }];
|
||||
return [
|
||||
{
|
||||
type: "old",
|
||||
char: change.value,
|
||||
oldIndex: oldIndex,
|
||||
},
|
||||
];
|
||||
} else {
|
||||
return [{ type: "same", char: change.value }];
|
||||
return [
|
||||
{
|
||||
type: "same",
|
||||
char: change.value,
|
||||
oldIndex: oldIndex,
|
||||
newIndex: newIndex,
|
||||
},
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -64,12 +64,12 @@ describe("Test myersCharDiff function", () => {
|
|||
|
||||
const diffChars = myersCharDiff(oldContent, newContent);
|
||||
expect(diffChars).toEqual([
|
||||
{ type: "same", char: "hello " },
|
||||
{ type: "old", char: "wo" },
|
||||
{ type: "new", char: "ea" },
|
||||
{ type: "same", char: "r" },
|
||||
{ type: "old", char: "ld" },
|
||||
{ type: "new", char: "th" },
|
||||
{ type: "same", char: "hello ", oldIndex: 0, newIndex: 0 },
|
||||
{ type: "old", char: "wo", oldIndex: 6 },
|
||||
{ type: "new", char: "ea", newIndex: 6 },
|
||||
{ type: "same", char: "r", oldIndex: 8, newIndex: 8 },
|
||||
{ type: "old", char: "ld", oldIndex: 9 },
|
||||
{ type: "new", char: "th", newIndex: 9 },
|
||||
]);
|
||||
});
|
||||
|
||||
|
|
@ -79,9 +79,9 @@ describe("Test myersCharDiff function", () => {
|
|||
|
||||
const diffChars = myersCharDiff(oldContent, newContent);
|
||||
expect(diffChars).toEqual([
|
||||
{ type: "same", char: "ab" },
|
||||
{ type: "new", char: "xyz" },
|
||||
{ type: "same", char: "c" },
|
||||
{ type: "same", char: "ab", oldIndex: 0, newIndex: 0 },
|
||||
{ type: "new", char: "xyz", newIndex: 2 },
|
||||
{ type: "same", char: "c", oldIndex: 2, newIndex: 5 },
|
||||
]);
|
||||
});
|
||||
|
||||
|
|
@ -91,9 +91,9 @@ describe("Test myersCharDiff function", () => {
|
|||
|
||||
const diffChars = myersCharDiff(oldContent, newContent);
|
||||
expect(diffChars).toEqual([
|
||||
{ type: "same", char: "ab" },
|
||||
{ type: "old", char: "xyz" },
|
||||
{ type: "same", char: "c" },
|
||||
{ type: "same", char: "ab", oldIndex: 0, newIndex: 0 },
|
||||
{ type: "old", char: "xyz", oldIndex: 2 },
|
||||
{ type: "same", char: "c", oldIndex: 5, newIndex: 2 },
|
||||
]);
|
||||
});
|
||||
|
||||
|
|
@ -102,14 +102,16 @@ describe("Test myersCharDiff function", () => {
|
|||
const newContent = "abc";
|
||||
|
||||
const diffChars = myersCharDiff(oldContent, newContent);
|
||||
expect(diffChars).toEqual([{ type: "new", char: "abc" }]);
|
||||
expect(diffChars).toEqual([{ type: "new", char: "abc", newIndex: 0 }]);
|
||||
});
|
||||
|
||||
test("should handle identical strings", () => {
|
||||
const content = "no changes here";
|
||||
|
||||
const diffChars = myersCharDiff(content, content);
|
||||
expect(diffChars).toEqual([{ type: "same", char: "no changes here" }]);
|
||||
expect(diffChars).toEqual([
|
||||
{ type: "same", char: "no changes here", oldIndex: 0, newIndex: 0 },
|
||||
]);
|
||||
});
|
||||
|
||||
test("should handle whitespace changes", () => {
|
||||
|
|
@ -118,9 +120,9 @@ describe("Test myersCharDiff function", () => {
|
|||
|
||||
const diffChars = myersCharDiff(oldContent, newContent);
|
||||
expect(diffChars).toEqual([
|
||||
{ type: "same", char: "hello " },
|
||||
{ type: "new", char: " " },
|
||||
{ type: "same", char: "world" },
|
||||
{ type: "same", char: "hello ", oldIndex: 0, newIndex: 0 },
|
||||
{ type: "new", char: " ", newIndex: 6 },
|
||||
{ type: "same", char: "world", oldIndex: 6, newIndex: 7 },
|
||||
]);
|
||||
});
|
||||
|
||||
|
|
@ -130,18 +132,18 @@ describe("Test myersCharDiff function", () => {
|
|||
|
||||
const diffChars = myersCharDiff(oldContent, newContent);
|
||||
expect(diffChars).toEqual([
|
||||
{ type: "same", char: "The " },
|
||||
{ type: "old", char: "quick" },
|
||||
{ type: "new", char: "fast" },
|
||||
{ type: "same", char: " brown fox " },
|
||||
{ type: "old", char: "jum" },
|
||||
{ type: "new", char: "lea" },
|
||||
{ type: "same", char: "ps over the " },
|
||||
{ type: "new", char: "s" },
|
||||
{ type: "same", char: "l" },
|
||||
{ type: "old", char: "azy" },
|
||||
{ type: "new", char: "eeping" },
|
||||
{ type: "same", char: " dog" },
|
||||
{ type: "same", char: "The ", oldIndex: 0, newIndex: 0 },
|
||||
{ type: "old", char: "quick", oldIndex: 4 },
|
||||
{ type: "new", char: "fast", newIndex: 4 },
|
||||
{ type: "same", char: " brown fox ", oldIndex: 9, newIndex: 8 },
|
||||
{ type: "old", char: "jum", oldIndex: 20 },
|
||||
{ type: "new", char: "lea", newIndex: 19 },
|
||||
{ type: "same", char: "ps over the ", oldIndex: 23, newIndex: 22 },
|
||||
{ type: "new", char: "s", newIndex: 34 },
|
||||
{ type: "same", char: "l", oldIndex: 35, newIndex: 35 },
|
||||
{ type: "old", char: "azy", oldIndex: 36 },
|
||||
{ type: "new", char: "eeping", newIndex: 36 },
|
||||
{ type: "same", char: " dog", oldIndex: 39, newIndex: 42 },
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
|
|
|||
2
core/index.d.ts
vendored
2
core/index.d.ts
vendored
|
|
@ -677,6 +677,8 @@ export interface DiffLine extends DiffObject {
|
|||
|
||||
interface DiffChar extends DiffObject {
|
||||
char: string;
|
||||
oldIndex?: number;
|
||||
newIndex?: number;
|
||||
}
|
||||
|
||||
export interface Problem {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue