Skip to content

Commit 9b219ed

Browse files
fix ignoreWhiteWpace diff
1 parent 63a0d79 commit 9b219ed

File tree

8 files changed

+49
-13
lines changed

8 files changed

+49
-13
lines changed

packages/core/index.d.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,10 @@ export declare const HiddenBidiCharsRegex: RegExp;
349349
export declare const _cacheMap: Cache$1<string, File$1>;
350350
export declare const checkDiffLineIncludeChange: (diffLine?: DiffLine) => boolean;
351351
export declare const composeLen = 40;
352-
export declare const getDiffRange: (additions: DiffLine[], deletions: DiffLine[]) => void;
352+
export declare const getDiffRange: (additions: DiffLine[], deletions: DiffLine[], { getAdditionRaw, getDeletionRaw, }: {
353+
getAdditionRaw: (lineNumber: number) => string;
354+
getDeletionRaw: (lineNumber: number) => string;
355+
}) => void;
353356
export declare const getFile: (raw: string, lang: string, theme: "light" | "dark", fileName?: string, uuid?: string) => File$1;
354357
export declare const getLang: (fileName: string) => string;
355358
export declare const getSplitContentLines: (diffFile: DiffFile) => DiffSplitContentLineItem[];

packages/core/src/diff-file.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,14 @@ export class DiffFile {
383383
#composeDiff() {
384384
if (!this.#diffListResults?.length) return;
385385

386+
const getAdditionRaw = (lineNumber: number) => {
387+
return this.#getNewRawLine(lineNumber);
388+
};
389+
390+
const getDeletionRaw = (lineNumber: number) => {
391+
return this.#getOldRawLine(lineNumber);
392+
};
393+
386394
this.#diffListResults.forEach((item) => {
387395
const hunks = item.hunks;
388396
hunks.forEach((hunk) => {
@@ -394,12 +402,12 @@ export class DiffFile {
394402
} else if (line.type === DiffLineType.Delete) {
395403
deletions.push(line);
396404
} else {
397-
getDiffRange(additions, deletions);
405+
getDiffRange(additions, deletions, { getAdditionRaw, getDeletionRaw });
398406
additions = [];
399407
deletions = [];
400408
}
401409
});
402-
getDiffRange(additions, deletions);
410+
getDiffRange(additions, deletions, { getAdditionRaw, getDeletionRaw });
403411
});
404412
});
405413

@@ -539,10 +547,10 @@ export class DiffFile {
539547

540548
initRaw() {
541549
if (this.#hasInitRaw) return;
542-
this.#doDiff();
543-
this.#composeDiff();
544550
this.#doFile();
545551
this.#composeRaw();
552+
this.#doDiff();
553+
this.#composeDiff();
546554
this.#composeFile();
547555
this.#hasInitRaw = true;
548556
}

packages/core/src/parse/change-range.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ function checkNewLineSymbolChange(
108108
};
109109
}
110110

111-
// TODO maybe could use the original content line
111+
// TODO maybe could use the original content line. fixed
112112
/** Get the changed ranges in the strings, relative to each other. */
113113
export function relativeChanges(addition: DiffLine, deletion: DiffLine): { addRange: IRange; delRange: IRange } {
114114
const stringA = addition.text;

packages/core/src/parse/diff-tool.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,17 +91,31 @@ export const getLang = (fileName: string) => {
9191
return extension;
9292
};
9393

94-
export const getDiffRange = (additions: DiffLine[], deletions: DiffLine[]) => {
94+
export const getDiffRange = (
95+
additions: DiffLine[],
96+
deletions: DiffLine[],
97+
{
98+
getAdditionRaw,
99+
getDeletionRaw,
100+
}: {
101+
getAdditionRaw: (lineNumber: number) => string;
102+
getDeletionRaw: (lineNumber: number) => string;
103+
}
104+
) => {
95105
if (additions.length === deletions.length) {
96106
const len = additions.length;
97107
for (let i = 0; i < len; i++) {
98108
const addition = additions[i];
99109
const deletion = deletions[i];
100-
const { addRange, delRange } = relativeChanges(addition, deletion);
110+
// use the original text content to computed diff range
111+
// fix: get diff with ignoreWhiteSpace config
112+
const _addition = addition.clone(getAdditionRaw(addition.newLineNumber) || addition.text || '');
113+
const _deletion = deletion.clone(getDeletionRaw(deletion.oldLineNumber) || deletion.text || '');
114+
const { addRange, delRange } = relativeChanges(_addition, _deletion);
101115
addition.changes = addRange;
102116
deletion.changes = delRange;
103117
// full diff
104-
// const { addRange: _addRange, delRange: _delRange } = diffChanges(addition, deletion);
118+
// const { addRange: _addRange, delRange: _delRange } = diffChanges(_addition, _deletion);
105119
// addition.diffChanges = _addRange;
106120
// deletion.diffChanges = _delRange;
107121
}

packages/file/index.d.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,10 @@ export declare const HiddenBidiCharsRegex: RegExp;
348348
export declare const _cacheMap: Cache$1<string, File$1>;
349349
export declare const checkDiffLineIncludeChange: (diffLine?: DiffLine) => boolean;
350350
export declare const composeLen = 40;
351-
export declare const getDiffRange: (additions: DiffLine[], deletions: DiffLine[]) => void;
351+
export declare const getDiffRange: (additions: DiffLine[], deletions: DiffLine[], { getAdditionRaw, getDeletionRaw, }: {
352+
getAdditionRaw: (lineNumber: number) => string;
353+
getDeletionRaw: (lineNumber: number) => string;
354+
}) => void;
352355
export declare const getFile: (raw: string, lang: string, theme: "light" | "dark", fileName?: string, uuid?: string) => File$1;
353356
export declare const getLang: (fileName: string) => string;
354357
export declare const getSplitContentLines: (diffFile: DiffFile) => DiffSplitContentLineItem[];

packages/react/index.d.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,10 @@ export declare const HiddenBidiCharsRegex: RegExp;
348348
export declare const _cacheMap: Cache$1<string, File$1>;
349349
export declare const checkDiffLineIncludeChange: (diffLine?: DiffLine) => boolean;
350350
export declare const composeLen = 40;
351-
export declare const getDiffRange: (additions: DiffLine[], deletions: DiffLine[]) => void;
351+
export declare const getDiffRange: (additions: DiffLine[], deletions: DiffLine[], { getAdditionRaw, getDeletionRaw, }: {
352+
getAdditionRaw: (lineNumber: number) => string;
353+
getDeletionRaw: (lineNumber: number) => string;
354+
}) => void;
352355
export declare const getFile: (raw: string, lang: string, theme: "light" | "dark", fileName?: string, uuid?: string) => File$1;
353356
export declare const getLang: (fileName: string) => string;
354357
export declare const getSplitContentLines: (diffFile: DiffFile) => DiffSplitContentLineItem[];

packages/vue/index.d.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,10 @@ export declare const HiddenBidiCharsRegex: RegExp;
348348
export declare const _cacheMap: Cache$1<string, File$1>;
349349
export declare const checkDiffLineIncludeChange: (diffLine?: DiffLine) => boolean;
350350
export declare const composeLen = 40;
351-
export declare const getDiffRange: (additions: DiffLine[], deletions: DiffLine[]) => void;
351+
export declare const getDiffRange: (additions: DiffLine[], deletions: DiffLine[], { getAdditionRaw, getDeletionRaw, }: {
352+
getAdditionRaw: (lineNumber: number) => string;
353+
getDeletionRaw: (lineNumber: number) => string;
354+
}) => void;
352355
export declare const getFile: (raw: string, lang: string, theme: "light" | "dark", fileName?: string, uuid?: string) => File$1;
353356
export declare const getLang: (fileName: string) => string;
354357
export declare const getSplitContentLines: (diffFile: DiffFile) => DiffSplitContentLineItem[];

ui/react-example/src/components/PlayGroundContent.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,9 @@ const _PlayGroundFileDiff = ({ onClick }: { onClick: () => void }) => {
125125
setDiffInstance(undefined);
126126
return;
127127
}
128-
const data = generateDiffFile(`foo.${lang1}`, file1, `foo.${lang2}`, file2, lang1, lang2);
128+
const data = generateDiffFile(`foo.${lang1}`, file1, `foo.${lang2}`, file2, lang1, lang2, {
129+
ignoreWhitespace: true,
130+
});
129131
try {
130132
data?.init();
131133
data?.buildSplitDiffLines();

0 commit comments

Comments
 (0)