feat: create a character level diff algo
This commit is contained in:
parent
faeac81945
commit
e0129c45b4
5 changed files with 134 additions and 10 deletions
|
|
@ -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 }];
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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" },
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
||||
|
|
|
|||
|
|
@ -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<DiffLineType, "new" | "old"> | "modification";
|
||||
type MyersDiffTypes = Extract<DiffType, "new" | "old"> | "modification";
|
||||
|
||||
const UNIFIED_DIFF_SYMBOLS = {
|
||||
same: "",
|
||||
|
|
|
|||
13
core/index.d.ts
vendored
13
core/index.d.ts
vendored
|
|
@ -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;
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue