From c36b27da03a4676e9caee70d1eb80221550b657d Mon Sep 17 00:00:00 2001 From: Jacob Kim Date: Fri, 11 Jul 2025 11:39:33 -0700 Subject: [PATCH] feat: keep track of indices --- core/diff/myers.ts | 62 +++++++++++++++++++++++++++++++-------- core/diff/myers.vitest.ts | 60 +++++++++++++++++++------------------ core/index.d.ts | 2 ++ 3 files changed, 83 insertions(+), 41 deletions(-) diff --git a/core/diff/myers.ts b/core/diff/myers.ts index 99e2a28b3..3e19a5d77 100644 --- a/core/diff/myers.ts +++ b/core/diff/myers.ts @@ -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, + }, + ]; } } diff --git a/core/diff/myers.vitest.ts b/core/diff/myers.vitest.ts index f56dcd32d..900341e1a 100644 --- a/core/diff/myers.vitest.ts +++ b/core/diff/myers.vitest.ts @@ -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 }, ]); }); }); diff --git a/core/index.d.ts b/core/index.d.ts index 4cdec4d25..689d3e6e7 100644 --- a/core/index.d.ts +++ b/core/index.d.ts @@ -677,6 +677,8 @@ export interface DiffLine extends DiffObject { interface DiffChar extends DiffObject { char: string; + oldIndex?: number; + newIndex?: number; } export interface Problem {