Skip to content

Commit

Permalink
fix ignoreWhiteWpace diff
Browse files Browse the repository at this point in the history
  • Loading branch information
MrWangJustToDo committed Oct 29, 2024
1 parent 63a0d79 commit 9b219ed
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 13 deletions.
5 changes: 4 additions & 1 deletion packages/core/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,10 @@ export declare const HiddenBidiCharsRegex: RegExp;
export declare const _cacheMap: Cache$1<string, File$1>;
export declare const checkDiffLineIncludeChange: (diffLine?: DiffLine) => boolean;
export declare const composeLen = 40;
export declare const getDiffRange: (additions: DiffLine[], deletions: DiffLine[]) => void;
export declare const getDiffRange: (additions: DiffLine[], deletions: DiffLine[], { getAdditionRaw, getDeletionRaw, }: {
getAdditionRaw: (lineNumber: number) => string;
getDeletionRaw: (lineNumber: number) => string;
}) => void;
export declare const getFile: (raw: string, lang: string, theme: "light" | "dark", fileName?: string, uuid?: string) => File$1;
export declare const getLang: (fileName: string) => string;
export declare const getSplitContentLines: (diffFile: DiffFile) => DiffSplitContentLineItem[];
Expand Down
16 changes: 12 additions & 4 deletions packages/core/src/diff-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,14 @@ export class DiffFile {
#composeDiff() {
if (!this.#diffListResults?.length) return;

const getAdditionRaw = (lineNumber: number) => {
return this.#getNewRawLine(lineNumber);
};

const getDeletionRaw = (lineNumber: number) => {
return this.#getOldRawLine(lineNumber);
};

this.#diffListResults.forEach((item) => {
const hunks = item.hunks;
hunks.forEach((hunk) => {
Expand All @@ -394,12 +402,12 @@ export class DiffFile {
} else if (line.type === DiffLineType.Delete) {
deletions.push(line);
} else {
getDiffRange(additions, deletions);
getDiffRange(additions, deletions, { getAdditionRaw, getDeletionRaw });
additions = [];
deletions = [];
}
});
getDiffRange(additions, deletions);
getDiffRange(additions, deletions, { getAdditionRaw, getDeletionRaw });
});
});

Expand Down Expand Up @@ -539,10 +547,10 @@ export class DiffFile {

initRaw() {
if (this.#hasInitRaw) return;
this.#doDiff();
this.#composeDiff();
this.#doFile();
this.#composeRaw();
this.#doDiff();
this.#composeDiff();
this.#composeFile();
this.#hasInitRaw = true;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/parse/change-range.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ function checkNewLineSymbolChange(
};
}

// TODO maybe could use the original content line
// TODO maybe could use the original content line. fixed
/** Get the changed ranges in the strings, relative to each other. */
export function relativeChanges(addition: DiffLine, deletion: DiffLine): { addRange: IRange; delRange: IRange } {
const stringA = addition.text;
Expand Down
20 changes: 17 additions & 3 deletions packages/core/src/parse/diff-tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,31 @@ export const getLang = (fileName: string) => {
return extension;
};

export const getDiffRange = (additions: DiffLine[], deletions: DiffLine[]) => {
export const getDiffRange = (
additions: DiffLine[],
deletions: DiffLine[],
{
getAdditionRaw,
getDeletionRaw,
}: {
getAdditionRaw: (lineNumber: number) => string;
getDeletionRaw: (lineNumber: number) => string;
}
) => {
if (additions.length === deletions.length) {
const len = additions.length;
for (let i = 0; i < len; i++) {
const addition = additions[i];
const deletion = deletions[i];
const { addRange, delRange } = relativeChanges(addition, deletion);
// use the original text content to computed diff range
// fix: get diff with ignoreWhiteSpace config
const _addition = addition.clone(getAdditionRaw(addition.newLineNumber) || addition.text || '');
const _deletion = deletion.clone(getDeletionRaw(deletion.oldLineNumber) || deletion.text || '');
const { addRange, delRange } = relativeChanges(_addition, _deletion);
addition.changes = addRange;
deletion.changes = delRange;
// full diff
// const { addRange: _addRange, delRange: _delRange } = diffChanges(addition, deletion);
// const { addRange: _addRange, delRange: _delRange } = diffChanges(_addition, _deletion);
// addition.diffChanges = _addRange;
// deletion.diffChanges = _delRange;
}
Expand Down
5 changes: 4 additions & 1 deletion packages/file/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,10 @@ export declare const HiddenBidiCharsRegex: RegExp;
export declare const _cacheMap: Cache$1<string, File$1>;
export declare const checkDiffLineIncludeChange: (diffLine?: DiffLine) => boolean;
export declare const composeLen = 40;
export declare const getDiffRange: (additions: DiffLine[], deletions: DiffLine[]) => void;
export declare const getDiffRange: (additions: DiffLine[], deletions: DiffLine[], { getAdditionRaw, getDeletionRaw, }: {
getAdditionRaw: (lineNumber: number) => string;
getDeletionRaw: (lineNumber: number) => string;
}) => void;
export declare const getFile: (raw: string, lang: string, theme: "light" | "dark", fileName?: string, uuid?: string) => File$1;
export declare const getLang: (fileName: string) => string;
export declare const getSplitContentLines: (diffFile: DiffFile) => DiffSplitContentLineItem[];
Expand Down
5 changes: 4 additions & 1 deletion packages/react/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,10 @@ export declare const HiddenBidiCharsRegex: RegExp;
export declare const _cacheMap: Cache$1<string, File$1>;
export declare const checkDiffLineIncludeChange: (diffLine?: DiffLine) => boolean;
export declare const composeLen = 40;
export declare const getDiffRange: (additions: DiffLine[], deletions: DiffLine[]) => void;
export declare const getDiffRange: (additions: DiffLine[], deletions: DiffLine[], { getAdditionRaw, getDeletionRaw, }: {
getAdditionRaw: (lineNumber: number) => string;
getDeletionRaw: (lineNumber: number) => string;
}) => void;
export declare const getFile: (raw: string, lang: string, theme: "light" | "dark", fileName?: string, uuid?: string) => File$1;
export declare const getLang: (fileName: string) => string;
export declare const getSplitContentLines: (diffFile: DiffFile) => DiffSplitContentLineItem[];
Expand Down
5 changes: 4 additions & 1 deletion packages/vue/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,10 @@ export declare const HiddenBidiCharsRegex: RegExp;
export declare const _cacheMap: Cache$1<string, File$1>;
export declare const checkDiffLineIncludeChange: (diffLine?: DiffLine) => boolean;
export declare const composeLen = 40;
export declare const getDiffRange: (additions: DiffLine[], deletions: DiffLine[]) => void;
export declare const getDiffRange: (additions: DiffLine[], deletions: DiffLine[], { getAdditionRaw, getDeletionRaw, }: {
getAdditionRaw: (lineNumber: number) => string;
getDeletionRaw: (lineNumber: number) => string;
}) => void;
export declare const getFile: (raw: string, lang: string, theme: "light" | "dark", fileName?: string, uuid?: string) => File$1;
export declare const getLang: (fileName: string) => string;
export declare const getSplitContentLines: (diffFile: DiffFile) => DiffSplitContentLineItem[];
Expand Down
4 changes: 3 additions & 1 deletion ui/react-example/src/components/PlayGroundContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,9 @@ const _PlayGroundFileDiff = ({ onClick }: { onClick: () => void }) => {
setDiffInstance(undefined);
return;
}
const data = generateDiffFile(`foo.${lang1}`, file1, `foo.${lang2}`, file2, lang1, lang2);
const data = generateDiffFile(`foo.${lang1}`, file1, `foo.${lang2}`, file2, lang1, lang2, {
ignoreWhitespace: true,
});
try {
data?.init();
data?.buildSplitDiffLines();
Expand Down

0 comments on commit 9b219ed

Please sign in to comment.