diff --git a/core/diff/myers.ts b/core/diff/myers.ts index 0b5d23730..99e2a28b3 100644 --- a/core/diff/myers.ts +++ b/core/diff/myers.ts @@ -1,6 +1,6 @@ -import { diffLines, type Change } from "diff"; +import { diffChars, diffLines, type Change } from "diff"; -import { DiffLine } from ".."; +import { DiffChar, DiffLine } from ".."; export function convertMyersChangeToDiffLines(change: Change): DiffLine[] { const type: DiffLine["type"] = change.added @@ -55,3 +55,31 @@ export function myersDiff(oldContent: string, newContent: string): DiffLine[] { return ourFormat; } + +export function myersCharDiff( + oldContent: string, + newContent: string, +): DiffChar[] { + const theirFormat = diffChars(oldContent, newContent); + console.log(theirFormat); + const ourFormat = theirFormat.flatMap(convertMyersChangeToChars); + + 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[] { + if (change.added) { + return [{ type: "new", char: change.value }]; + } else if (change.removed) { + return [{ type: "old", char: change.value }]; + } else { + return [{ type: "same", char: change.value }]; + } +} diff --git a/core/diff/myers.vitest.ts b/core/diff/myers.vitest.ts index a5902142a..f56dcd32d 100644 --- a/core/diff/myers.vitest.ts +++ b/core/diff/myers.vitest.ts @@ -2,7 +2,7 @@ import { describe, expect, test } from "vitest"; import { dedent } from "../util"; -import { myersDiff } from "./myers"; +import { myersCharDiff, myersDiff } from "./myers"; describe("Test myers diff function", () => { test("should ...", () => { @@ -56,3 +56,92 @@ describe("Test myers diff function", () => { ]); }); }); + +describe("Test myersCharDiff function", () => { + test("should differentiate character changes", () => { + const oldContent = "hello world"; + const newContent = "hello earth"; + + 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" }, + ]); + }); + + test("should handle insertions", () => { + const oldContent = "abc"; + const newContent = "abxyzc"; + + const diffChars = myersCharDiff(oldContent, newContent); + expect(diffChars).toEqual([ + { type: "same", char: "ab" }, + { type: "new", char: "xyz" }, + { type: "same", char: "c" }, + ]); + }); + + test("should handle deletions", () => { + const oldContent = "abxyzc"; + const newContent = "abc"; + + const diffChars = myersCharDiff(oldContent, newContent); + expect(diffChars).toEqual([ + { type: "same", char: "ab" }, + { type: "old", char: "xyz" }, + { type: "same", char: "c" }, + ]); + }); + + test("should handle empty strings", () => { + const oldContent = ""; + const newContent = "abc"; + + const diffChars = myersCharDiff(oldContent, newContent); + expect(diffChars).toEqual([{ type: "new", char: "abc" }]); + }); + + test("should handle identical strings", () => { + const content = "no changes here"; + + const diffChars = myersCharDiff(content, content); + expect(diffChars).toEqual([{ type: "same", char: "no changes here" }]); + }); + + test("should handle whitespace changes", () => { + const oldContent = "hello world"; + const newContent = "hello world"; + + const diffChars = myersCharDiff(oldContent, newContent); + expect(diffChars).toEqual([ + { type: "same", char: "hello " }, + { type: "new", char: " " }, + { type: "same", char: "world" }, + ]); + }); + + test("should handle complex changes", () => { + const oldContent = "The quick brown fox jumps over the lazy dog"; + const newContent = "The fast brown fox leaps over the sleeping dog"; + + 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" }, + ]); + }); +}); diff --git a/core/diff/streamDiff.ts b/core/diff/streamDiff.ts index 436da1c09..6f8911679 100644 --- a/core/diff/streamDiff.ts +++ b/core/diff/streamDiff.ts @@ -1,4 +1,4 @@ -import { DiffLine, DiffLineType } from "../index.js"; +import { DiffLine, DiffType } from "../index.js"; import { LineStream, matchLine } from "./util.js"; @@ -33,7 +33,7 @@ export async function* streamDiff( seenIndentationMistake = true; } - let type: DiffLineType; + let type: DiffType; const isNewLine = matchIndex === -1; diff --git a/core/diff/streamDiff.vitest.ts b/core/diff/streamDiff.vitest.ts index 57c6e77f1..dbd329db1 100644 --- a/core/diff/streamDiff.vitest.ts +++ b/core/diff/streamDiff.vitest.ts @@ -6,11 +6,11 @@ import { describe, expect, test } from "vitest"; // @ts-ignore no typings available import { changed, diff as myersDiff } from "myers-diff"; import { streamDiff } from "../diff/streamDiff.js"; -import { DiffLine, DiffLineType } from "../index.js"; +import { DiffLine, DiffType } from "../index.js"; import { generateLines } from "./util.js"; // "modification" is an extra type used to represent an "old" + "new" diff line -type MyersDiffTypes = Extract | "modification"; +type MyersDiffTypes = Extract | "modification"; const UNIFIED_DIFF_SYMBOLS = { same: "", diff --git a/core/index.d.ts b/core/index.d.ts index 1ace46141..4cdec4d25 100644 --- a/core/index.d.ts +++ b/core/index.d.ts @@ -665,13 +665,20 @@ export type CustomLLM = RequireAtLeastOne< // IDE -export type DiffLineType = "new" | "old" | "same"; +export type DiffType = "new" | "old" | "same"; -export interface DiffLine { - type: DiffLineType; +export interface DiffObject { + type: DiffType; +} + +export interface DiffLine extends DiffObject { line: string; } +interface DiffChar extends DiffObject { + char: string; +} + export interface Problem { filepath: string; range: Range;