Skip to content

Commit f010d73

Browse files
update
1 parent 42419f8 commit f010d73

File tree

12 files changed

+97
-23
lines changed

12 files changed

+97
-23
lines changed

packages/core/index.d.ts

+8
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
declare class Cache$1<K, V> extends Map<K, V> {
44

55
name: string;
6+
get maxLength(): number;
67
setMaxLength(length: number): void;
78
set(key: K, value: V): this;
89
}
@@ -59,6 +60,8 @@ export declare class DiffFile {
5960
splitLineLength: number;
6061
unifiedLineLength: number;
6162
fileLineLength: number;
63+
additionLength: number;
64+
deletionLength: number;
6265
hasExpandSplitAll: boolean;
6366
hasExpandUnifiedAll: boolean;
6467
hasSomeLineCollapsed: boolean;
@@ -110,6 +113,8 @@ export declare class DiffFile {
110113
splitLineLength: number;
111114
unifiedLineLength: number;
112115
fileLineLength: number;
116+
additionLength: number;
117+
deletionLength: number;
113118
splitLeftLines: SplitLineItem[];
114119
splitRightLines: SplitLineItem[];
115120
splitHunkLines: Record<string, DiffHunkItem>;
@@ -151,6 +156,8 @@ export declare class DiffFile {
151156
splitLineLength: number;
152157
unifiedLineLength: number;
153158
fileLineLength: number;
159+
additionLength: number;
160+
deletionLength: number;
154161
splitLeftLines: SplitLineItem[];
155162
splitRightLines: SplitLineItem[];
156163
splitHunkLines: Record<string, DiffHunkItem>;
@@ -354,6 +361,7 @@ export declare const HiddenBidiCharsRegex: RegExp;
354361
export declare const _cacheMap: Cache$1<string, File$1>;
355362
export declare const checkDiffLineIncludeChange: (diffLine?: DiffLine) => boolean;
356363
export declare const composeLen = 40;
364+
export declare const disableCache: () => void;
357365
export declare const getDiffRange: (additions: DiffLine[], deletions: DiffLine[], { getAdditionRaw, getDeletionRaw, }: {
358366
getAdditionRaw: (lineNumber: number) => string;
359367
getDeletionRaw: (lineNumber: number) => string;

packages/core/src/cache.ts

+5
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,17 @@ export class Cache<K, V> extends Map<K, V> {
55

66
#maxLength = 30;
77

8+
get maxLength() {
9+
return this.#maxLength;
10+
}
11+
812
setMaxLength(length: number) {
913
this.#maxLength = length;
1014
this.#checkLength();
1115
}
1216

1317
set(key: K, value: V): this {
18+
if (this.#maxLength <= 0) return this;
1419
if (this.has(key)) return this;
1520
this.#keyArray.push(key);
1621
this.#checkLength();

packages/core/src/diff-file.ts

+53-18
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,10 @@ export class DiffFile {
163163

164164
fileLineLength: number = 0;
165165

166+
additionLength: number = 0;
167+
168+
deletionLength: number = 0;
169+
166170
hasExpandSplitAll: boolean = false;
167171

168172
hasExpandUnifiedAll: boolean = false;
@@ -433,6 +437,14 @@ export class DiffFile {
433437
return this.#getOldRawLine(lineNumber);
434438
};
435439

440+
this.#diffLines = [];
441+
442+
this.additionLength = 0;
443+
444+
this.deletionLength = 0;
445+
446+
const tmp: DiffLine[] = [];
447+
436448
this.#diffListResults.forEach((item) => {
437449
const hunks = item.hunks;
438450
hunks.forEach((hunk) => {
@@ -441,28 +453,21 @@ export class DiffFile {
441453
hunk.lines.forEach((line) => {
442454
if (line.type === DiffLineType.Add) {
443455
additions.push(line);
456+
this.additionLength++;
444457
} else if (line.type === DiffLineType.Delete) {
445458
deletions.push(line);
459+
this.deletionLength++;
446460
} else {
447461
getDiffRange(additions, deletions, { getAdditionRaw, getDeletionRaw });
448462
additions = [];
449463
deletions = [];
450464
}
465+
tmp.push(line);
451466
});
452467
getDiffRange(additions, deletions, { getAdditionRaw, getDeletionRaw });
453468
});
454469
});
455470

456-
this.#diffLines = [];
457-
458-
const tmp: DiffLine[] = [];
459-
460-
this.#diffListResults.forEach((item) => {
461-
item.hunks.forEach((_item) => {
462-
tmp.push(..._item.lines);
463-
});
464-
});
465-
466471
let prevHunkLine: DiffHunkItem | null = null;
467472

468473
this.#diffLines = tmp.map<DiffLineItem>((i, index) => {
@@ -515,21 +520,43 @@ export class DiffFile {
515520

516521
this.#oldFileDiffLines = {};
517522

523+
this.#newFileDiffLines = {};
524+
525+
let maxOldLineNumber = -1;
526+
527+
let maxNewLineNumber = -1;
528+
518529
this.#diffLines.forEach((item) => {
519530
if (item.oldLineNumber) {
520531
this.diffLineLength = Math.max(this.diffLineLength, item.oldLineNumber);
521532

522533
this.#oldFileDiffLines[item.oldLineNumber] = item;
523-
}
524-
});
525534

526-
this.#newFileDiffLines = {};
535+
if (__DEV__) {
536+
if (item.oldLineNumber <= maxOldLineNumber) {
537+
console.warn(
538+
'the "lineNumber" from "diff" should be in ascending order, maybe current "diff" string is not a valid "diff" string'
539+
);
540+
}
541+
542+
maxOldLineNumber = Math.max(maxOldLineNumber, item.oldLineNumber);
543+
}
544+
}
527545

528-
this.#diffLines.forEach((item) => {
529546
if (item.newLineNumber) {
530547
this.diffLineLength = Math.max(this.diffLineLength, item.newLineNumber);
531548

532549
this.#newFileDiffLines[item.newLineNumber] = item;
550+
551+
if (__DEV__) {
552+
if (item.newLineNumber <= maxNewLineNumber) {
553+
console.warn(
554+
'the "lineNumber" from "diff" should be in ascending order, maybe current "diff" string is not a valid "diff" string'
555+
);
556+
}
557+
558+
maxNewLineNumber = Math.max(maxNewLineNumber, item.newLineNumber);
559+
}
533560
}
534561
});
535562
}
@@ -635,7 +662,7 @@ export class DiffFile {
635662
const maxOldFileLineNumber = this.#oldFileResult?.maxLineNumber || 0;
636663
const maxNewFileLineNumber = this.#newFileResult?.maxLineNumber || 0;
637664

638-
if (__DEV__ && !this.#oldFileResult && !this.#newFileResult) {
665+
if (__DEV__ && !this.#oldFileResult && !this.#newFileResult && this.#composeByMerge) {
639666
console.error(
640667
"this instance can not `buildSplitDiffLines` because of the data missing, try to use '_getFullBundle' & '_mergeFullBundle' instead of 'getBundle' & 'mergeBundle'"
641668
);
@@ -821,7 +848,7 @@ export class DiffFile {
821848
const maxOldFileLineNumber = this.#oldFileResult?.maxLineNumber || 0;
822849
const maxNewFileLineNumber = this.#newFileResult?.maxLineNumber || 0;
823850

824-
if (__DEV__ && !this.#oldFileResult && !this.#newFileResult) {
851+
if (__DEV__ && !this.#oldFileResult && !this.#newFileResult && this.#composeByMerge) {
825852
console.error(
826853
"this instance can not `buildUnifiedDiffLines` because of the data missing, try to use '_getFullBundle' & '_mergeFullBundle' instead of 'getBundle' & 'mergeBundle'"
827854
);
@@ -1081,11 +1108,13 @@ export class DiffFile {
10811108
return this.#unifiedHunksLines?.[index];
10821109
};
10831110

1111+
// TODO! support rollback?
10841112
onUnifiedHunkExpand = (dir: "up" | "down" | "all", index: number, needTrigger = true) => {
1113+
if (this.#composeByDiff) return;
1114+
10851115
const current = this.#unifiedHunksLines?.[index];
1086-
if (!current || !current.unifiedInfo) return;
10871116

1088-
if (this.#composeByDiff) return;
1117+
if (!current || !current.unifiedInfo) return;
10891118

10901119
if (dir === "all") {
10911120
for (let i = current.unifiedInfo.startHiddenIndex; i < current.unifiedInfo.endHiddenIndex; i++) {
@@ -1290,6 +1319,8 @@ export class DiffFile {
12901319
const splitLineLength = this.splitLineLength;
12911320
const unifiedLineLength = this.unifiedLineLength;
12921321
const fileLineLength = this.fileLineLength;
1322+
const additionLength = this.additionLength;
1323+
const deletionLength = this.deletionLength;
12931324
const composeByDiff = this.#composeByDiff;
12941325
const highlighterName = this.#highlighterName;
12951326
const highlighterType = this.#highlighterType;
@@ -1323,6 +1354,8 @@ export class DiffFile {
13231354
splitLineLength,
13241355
unifiedLineLength,
13251356
fileLineLength,
1357+
additionLength,
1358+
deletionLength,
13261359
splitLeftLines,
13271360
splitRightLines,
13281361
splitHunkLines,
@@ -1362,6 +1395,8 @@ export class DiffFile {
13621395
this.splitLineLength = data.splitLineLength;
13631396
this.unifiedLineLength = data.unifiedLineLength;
13641397
this.fileLineLength = data.fileLineLength;
1398+
this.additionLength = data.additionLength;
1399+
this.deletionLength = data.deletionLength;
13651400
this.hasSomeLineCollapsed = data.hasSomeLineCollapsed;
13661401

13671402
this.#splitLeftLines = data.splitLeftLines;

packages/core/src/file.ts

+2
Original file line numberDiff line numberDiff line change
@@ -219,3 +219,5 @@ export function getFile(
219219
}
220220

221221
export const _cacheMap = map;
222+
223+
export const disableCache = () => map.setMaxLength(0);

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ export const getDiffRange = (
114114
const { addRange, delRange } = relativeChanges(_addition, _deletion);
115115
addition.changes = addRange;
116116
deletion.changes = delRange;
117-
// full diff
117+
// TODO! support word diff
118118
// const { addRange: _addRange, delRange: _delRange } = diffChanges(_addition, _deletion);
119119
// addition.diffChanges = _addRange;
120120
// deletion.diffChanges = _delRange;

packages/file/index.d.ts

+8
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { PatchOptions } from 'diff';
44

55
declare class Cache$1<K, V> extends Map<K, V> {
66
name: string;
7+
get maxLength(): number;
78
setMaxLength(length: number): void;
89
set(key: K, value: V): this;
910
}
@@ -58,6 +59,8 @@ export declare class DiffFile {
5859
splitLineLength: number;
5960
unifiedLineLength: number;
6061
fileLineLength: number;
62+
additionLength: number;
63+
deletionLength: number;
6164
hasExpandSplitAll: boolean;
6265
hasExpandUnifiedAll: boolean;
6366
hasSomeLineCollapsed: boolean;
@@ -109,6 +112,8 @@ export declare class DiffFile {
109112
splitLineLength: number;
110113
unifiedLineLength: number;
111114
fileLineLength: number;
115+
additionLength: number;
116+
deletionLength: number;
112117
splitLeftLines: SplitLineItem[];
113118
splitRightLines: SplitLineItem[];
114119
splitHunkLines: Record<string, DiffHunkItem>;
@@ -150,6 +155,8 @@ export declare class DiffFile {
150155
splitLineLength: number;
151156
unifiedLineLength: number;
152157
fileLineLength: number;
158+
additionLength: number;
159+
deletionLength: number;
153160
splitLeftLines: SplitLineItem[];
154161
splitRightLines: SplitLineItem[];
155162
splitHunkLines: Record<string, DiffHunkItem>;
@@ -353,6 +360,7 @@ export declare const HiddenBidiCharsRegex: RegExp;
353360
export declare const _cacheMap: Cache$1<string, File$1>;
354361
export declare const checkDiffLineIncludeChange: (diffLine?: DiffLine) => boolean;
355362
export declare const composeLen = 40;
363+
export declare const disableCache: () => void;
356364
export declare const getDiffRange: (additions: DiffLine[], deletions: DiffLine[], { getAdditionRaw, getDeletionRaw, }: {
357365
getAdditionRaw: (lineNumber: number) => string;
358366
getDeletionRaw: (lineNumber: number) => string;

packages/react/index.d.ts

+8
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { CSSProperties, ForwardedRef, ReactNode } from 'react';
44

55
declare class Cache$1<K, V> extends Map<K, V> {
66
name: string;
7+
get maxLength(): number;
78
setMaxLength(length: number): void;
89
set(key: K, value: V): this;
910
}
@@ -58,6 +59,8 @@ export declare class DiffFile {
5859
splitLineLength: number;
5960
unifiedLineLength: number;
6061
fileLineLength: number;
62+
additionLength: number;
63+
deletionLength: number;
6164
hasExpandSplitAll: boolean;
6265
hasExpandUnifiedAll: boolean;
6366
hasSomeLineCollapsed: boolean;
@@ -109,6 +112,8 @@ export declare class DiffFile {
109112
splitLineLength: number;
110113
unifiedLineLength: number;
111114
fileLineLength: number;
115+
additionLength: number;
116+
deletionLength: number;
112117
splitLeftLines: SplitLineItem[];
113118
splitRightLines: SplitLineItem[];
114119
splitHunkLines: Record<string, DiffHunkItem>;
@@ -150,6 +155,8 @@ export declare class DiffFile {
150155
splitLineLength: number;
151156
unifiedLineLength: number;
152157
fileLineLength: number;
158+
additionLength: number;
159+
deletionLength: number;
153160
splitLeftLines: SplitLineItem[];
154161
splitRightLines: SplitLineItem[];
155162
splitHunkLines: Record<string, DiffHunkItem>;
@@ -353,6 +360,7 @@ export declare const HiddenBidiCharsRegex: RegExp;
353360
export declare const _cacheMap: Cache$1<string, File$1>;
354361
export declare const checkDiffLineIncludeChange: (diffLine?: DiffLine) => boolean;
355362
export declare const composeLen = 40;
363+
export declare const disableCache: () => void;
356364
export declare const getDiffRange: (additions: DiffLine[], deletions: DiffLine[], { getAdditionRaw, getDeletionRaw, }: {
357365
getAdditionRaw: (lineNumber: number) => string;
358366
getDeletionRaw: (lineNumber: number) => string;

packages/react/src/components/DiffSplitHunkLineWrap.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ const DiffSplitHunkLineGitLab = ({
215215
</div>
216216
</td>
217217
<td
218-
className="diff-line-hunk-action relative w-[1%] min-w-[40px] select-none border-l-[1px] p-[1px]"
218+
className="diff-line-hunk-action relative w-[1%] min-w-[40px] select-none border-l-[1px] p-[1px] z-[1]"
219219
style={{
220220
backgroundColor: `var(${hunkLineNumberBGName})`,
221221
color: `var(${plainLineNumberColorName})`,

packages/react/src/components/v2/DiffSplitHunkLineWrap_v2.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ const DiffSplitHunkLineGitLab = ({
207207
</div>
208208
<div className="diff-split-line w-[1px] flex-shrink-0" style={{ backgroundColor: `var(${borderColorName})` }} />
209209
<div
210-
className="diff-line-hunk-action relative flex w-[1%] min-w-[40px] select-none flex-col items-center justify-center p-[1px]"
210+
className="diff-line-hunk-action relative z-[1] flex w-[1%] min-w-[40px] select-none flex-col items-center justify-center p-[1px]"
211211
style={{
212212
backgroundColor: `var(${hunkLineNumberBGName})`,
213213
color: `var(${plainLineNumberColorName})`,

packages/vue/index.d.ts

+8
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { CSSProperties, SlotsType } from 'vue';
44

55
declare class Cache$1<K, V> extends Map<K, V> {
66
name: string;
7+
get maxLength(): number;
78
setMaxLength(length: number): void;
89
set(key: K, value: V): this;
910
}
@@ -58,6 +59,8 @@ export declare class DiffFile {
5859
splitLineLength: number;
5960
unifiedLineLength: number;
6061
fileLineLength: number;
62+
additionLength: number;
63+
deletionLength: number;
6164
hasExpandSplitAll: boolean;
6265
hasExpandUnifiedAll: boolean;
6366
hasSomeLineCollapsed: boolean;
@@ -109,6 +112,8 @@ export declare class DiffFile {
109112
splitLineLength: number;
110113
unifiedLineLength: number;
111114
fileLineLength: number;
115+
additionLength: number;
116+
deletionLength: number;
112117
splitLeftLines: SplitLineItem[];
113118
splitRightLines: SplitLineItem[];
114119
splitHunkLines: Record<string, DiffHunkItem>;
@@ -150,6 +155,8 @@ export declare class DiffFile {
150155
splitLineLength: number;
151156
unifiedLineLength: number;
152157
fileLineLength: number;
158+
additionLength: number;
159+
deletionLength: number;
153160
splitLeftLines: SplitLineItem[];
154161
splitRightLines: SplitLineItem[];
155162
splitHunkLines: Record<string, DiffHunkItem>;
@@ -353,6 +360,7 @@ export declare const HiddenBidiCharsRegex: RegExp;
353360
export declare const _cacheMap: Cache$1<string, File$1>;
354361
export declare const checkDiffLineIncludeChange: (diffLine?: DiffLine) => boolean;
355362
export declare const composeLen = 40;
363+
export declare const disableCache: () => void;
356364
export declare const getDiffRange: (additions: DiffLine[], deletions: DiffLine[], { getAdditionRaw, getDeletionRaw, }: {
357365
getAdditionRaw: (lineNumber: number) => string;
358366
getDeletionRaw: (lineNumber: number) => string;

packages/vue/src/components/DiffSplitHunkLineWrap.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ const DiffSplitHunkLineGitLab = defineComponent(
273273
</div>
274274
</td>
275275
<td
276-
class="diff-line-hunk-action relative w-[1%] min-w-[40px] select-none border-l-[1px] p-[1px]"
276+
class="diff-line-hunk-action relative z-[1] w-[1%] min-w-[40px] select-none border-l-[1px] p-[1px]"
277277
style={{
278278
backgroundColor: `var(${hunkLineNumberBGName})`,
279279
color: `var(${plainLineNumberColorName})`,

0 commit comments

Comments
 (0)