Skip to content

Commit d2ae8c2

Browse files
Merge pull request #52 from MrWangJustToDo/fix/parse
fix diff parse error
2 parents ffdf0f2 + a3c5fe3 commit d2ae8c2

File tree

16 files changed

+94
-30
lines changed

16 files changed

+94
-30
lines changed

packages/cli/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
"cli diff component"
5151
],
5252
"dependencies": {
53-
"@git-diff-view/core": "^0.0.35",
53+
"@git-diff-view/core": "^0.0.36",
5454
"@types/hast": "^3.0.0",
5555
"highlight.js": "^11.11.0",
5656
"lowlight": "^3.3.0",

packages/core/index.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,12 @@ export declare class DiffFile {
126126
getSplitLineIndexByLineNumber: (lineNumber: number, side: SplitSide) => number;
127127
getSplitRightLine: (index: number) => SplitLineItem;
128128
getSplitHunkLine: (index: number) => DiffHunkItem;
129-
onSplitHunkExpand: (dir: "up" | "down" | "all", index: number, needTrigger?: boolean) => void;
129+
onSplitHunkExpand: (dir: "up" | "down" | "all" | "up-all" | "down-all", index: number, needTrigger?: boolean) => void;
130130
getUnifiedLine: (index: number) => UnifiedLineItem;
131131
getUnifiedLineByLineNumber: (lienNumber: number, side: SplitSide) => UnifiedLineItem;
132132
getUnifiedLineIndexByLineNumber: (lineNumber: number, side: SplitSide) => number;
133133
getUnifiedHunkLine: (index: number) => DiffHunkItem;
134-
onUnifiedHunkExpand: (dir: "up" | "down" | "all", index: number, needTrigger?: boolean) => void;
134+
onUnifiedHunkExpand: (dir: "up" | "down" | "all" | "up-all" | "down-all", index: number, needTrigger?: boolean) => void;
135135
onAllExpand: (mode: "split" | "unified") => void;
136136
get hasExpandSplitAll(): boolean;
137137
get hasExpandUnifiedAll(): boolean;

packages/core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
"diff parse"
5454
],
5555
"dependencies": {
56-
"@git-diff-view/lowlight": "^0.0.35",
56+
"@git-diff-view/lowlight": "^0.0.36",
5757
"highlight.js": "^11.11.0",
5858
"lowlight": "^3.3.0",
5959
"fast-diff": "^1.3.0"

packages/core/src/diff-file.ts

Lines changed: 68 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1159,7 +1159,7 @@ export class DiffFile {
11591159
return this.#splitHunksLines?.[index];
11601160
};
11611161

1162-
onSplitHunkExpand = (dir: "up" | "down" | "all", index: number, needTrigger = true) => {
1162+
onSplitHunkExpand = (dir: "up" | "down" | "all" | "up-all" | "down-all", index: number, needTrigger = true) => {
11631163
const current = this.#splitHunksLines?.[index];
11641164
if (!current || !current.splitInfo) return;
11651165

@@ -1197,7 +1197,19 @@ export class DiffFile {
11971197
plainText: `@@ -${current.splitInfo.oldStartIndex},${current.splitInfo.oldLength} +${current.splitInfo.newStartIndex},${current.splitInfo.newLength}`,
11981198
};
11991199
}
1200-
} else {
1200+
} else if (dir === "down-all") {
1201+
for (let i = current.splitInfo.startHiddenIndex; i < current.splitInfo.endHiddenIndex; i++) {
1202+
const leftLine = this.#splitLeftLines[i];
1203+
const rightLine = this.#splitRightLines[i];
1204+
if (leftLine?.isHidden) leftLine.isHidden = false;
1205+
if (rightLine?.isHidden) rightLine.isHidden = false;
1206+
}
1207+
current.splitInfo = {
1208+
...current.splitInfo,
1209+
plainText: "",
1210+
startHiddenIndex: current.splitInfo.endHiddenIndex,
1211+
};
1212+
} else if (dir === "up") {
12011213
if (current.isLast) {
12021214
if (__DEV__) {
12031215
console.error("the last hunk can not expand up!");
@@ -1226,6 +1238,28 @@ export class DiffFile {
12261238

12271239
delete this.#splitHunksLines?.[index];
12281240

1241+
this.#splitHunksLines![current.splitInfo.endHiddenIndex] = current;
1242+
} else if (dir === "up-all") {
1243+
if (current.isLast) {
1244+
if (__DEV__) {
1245+
console.error("the last hunk can not expand up!");
1246+
}
1247+
return;
1248+
}
1249+
for (let i = current.splitInfo.startHiddenIndex; i < current.splitInfo.endHiddenIndex; i++) {
1250+
const leftLine = this.#splitLeftLines[i];
1251+
const rightLine = this.#splitRightLines[i];
1252+
if (leftLine?.isHidden) leftLine.isHidden = false;
1253+
if (rightLine?.isHidden) rightLine.isHidden = false;
1254+
}
1255+
current.splitInfo = {
1256+
...current.splitInfo,
1257+
plainText: "",
1258+
endHiddenIndex: current.splitInfo.startHiddenIndex,
1259+
};
1260+
1261+
delete this.#splitHunksLines?.[index];
1262+
12291263
this.#splitHunksLines![current.splitInfo.endHiddenIndex] = current;
12301264
}
12311265

@@ -1259,7 +1293,7 @@ export class DiffFile {
12591293
};
12601294

12611295
// TODO! support rollback?
1262-
onUnifiedHunkExpand = (dir: "up" | "down" | "all", index: number, needTrigger = true) => {
1296+
onUnifiedHunkExpand = (dir: "up" | "down" | "all" | "up-all" | "down-all", index: number, needTrigger = true) => {
12631297
if (this.#composeByDiff) return;
12641298

12651299
const current = this.#unifiedHunksLines?.[index];
@@ -1296,7 +1330,17 @@ export class DiffFile {
12961330
plainText: `@@ -${current.unifiedInfo.oldStartIndex},${current.unifiedInfo.oldLength} +${current.unifiedInfo.newStartIndex},${current.unifiedInfo.newLength}`,
12971331
};
12981332
}
1299-
} else {
1333+
} else if (dir === "down-all") {
1334+
for (let i = current.unifiedInfo.startHiddenIndex; i < current.unifiedInfo.endHiddenIndex; i++) {
1335+
const unifiedLine = this.#unifiedLines[i];
1336+
if (unifiedLine?.isHidden) unifiedLine.isHidden = false;
1337+
}
1338+
current.unifiedInfo = {
1339+
...current.unifiedInfo,
1340+
plainText: "",
1341+
startHiddenIndex: current.unifiedInfo.endHiddenIndex,
1342+
};
1343+
} else if (dir === "up") {
13001344
if (current.isLast) {
13011345
if (__DEV__) {
13021346
console.error("the last hunk can not expand up!");
@@ -1323,6 +1367,26 @@ export class DiffFile {
13231367

13241368
delete this.#unifiedHunksLines?.[index];
13251369

1370+
this.#unifiedHunksLines![current.unifiedInfo.endHiddenIndex] = current;
1371+
} else if (dir === "up-all") {
1372+
if (current.isLast) {
1373+
if (__DEV__) {
1374+
console.error("the last hunk can not expand up!");
1375+
}
1376+
return;
1377+
}
1378+
for (let i = current.unifiedInfo.startHiddenIndex; i < current.unifiedInfo.endHiddenIndex; i++) {
1379+
const unifiedLine = this.#unifiedLines[i];
1380+
if (unifiedLine?.isHidden) unifiedLine.isHidden = false;
1381+
}
1382+
current.unifiedInfo = {
1383+
...current.unifiedInfo,
1384+
plainText: "",
1385+
endHiddenIndex: current.unifiedInfo.startHiddenIndex,
1386+
};
1387+
1388+
delete this.#unifiedHunksLines?.[index];
1389+
13261390
this.#unifiedHunksLines![current.unifiedInfo.endHiddenIndex] = current;
13271391
}
13281392

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ export class DiffParser {
336336
while ((c = this.parseLinePrefix(this.peek()))) {
337337
const line = this.readLine(false);
338338

339-
if (!line) {
339+
if (line === null) {
340340
throw new Error("Expected unified diff line but reached end of diff");
341341
}
342342

packages/file/index.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,12 +124,12 @@ export declare class DiffFile {
124124
getSplitLineIndexByLineNumber: (lineNumber: number, side: SplitSide) => number;
125125
getSplitRightLine: (index: number) => SplitLineItem;
126126
getSplitHunkLine: (index: number) => DiffHunkItem;
127-
onSplitHunkExpand: (dir: "up" | "down" | "all", index: number, needTrigger?: boolean) => void;
127+
onSplitHunkExpand: (dir: "up" | "down" | "all" | "up-all" | "down-all", index: number, needTrigger?: boolean) => void;
128128
getUnifiedLine: (index: number) => UnifiedLineItem;
129129
getUnifiedLineByLineNumber: (lienNumber: number, side: SplitSide) => UnifiedLineItem;
130130
getUnifiedLineIndexByLineNumber: (lineNumber: number, side: SplitSide) => number;
131131
getUnifiedHunkLine: (index: number) => DiffHunkItem;
132-
onUnifiedHunkExpand: (dir: "up" | "down" | "all", index: number, needTrigger?: boolean) => void;
132+
onUnifiedHunkExpand: (dir: "up" | "down" | "all" | "up-all" | "down-all", index: number, needTrigger?: boolean) => void;
133133
onAllExpand: (mode: "split" | "unified") => void;
134134
get hasExpandSplitAll(): boolean;
135135
get hasExpandUnifiedAll(): boolean;

packages/file/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
"diff parse"
5454
],
5555
"dependencies": {
56-
"@git-diff-view/core": "^0.0.35",
56+
"@git-diff-view/core": "^0.0.36",
5757
"diff": "^8.0.2",
5858
"highlight.js": "^11.11.0",
5959
"lowlight": "^3.3.0",

packages/react/index.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,12 +124,12 @@ export declare class DiffFile {
124124
getSplitLineIndexByLineNumber: (lineNumber: number, side: SplitSide) => number;
125125
getSplitRightLine: (index: number) => SplitLineItem;
126126
getSplitHunkLine: (index: number) => DiffHunkItem;
127-
onSplitHunkExpand: (dir: "up" | "down" | "all", index: number, needTrigger?: boolean) => void;
127+
onSplitHunkExpand: (dir: "up" | "down" | "all" | "up-all" | "down-all", index: number, needTrigger?: boolean) => void;
128128
getUnifiedLine: (index: number) => UnifiedLineItem;
129129
getUnifiedLineByLineNumber: (lienNumber: number, side: SplitSide) => UnifiedLineItem;
130130
getUnifiedLineIndexByLineNumber: (lineNumber: number, side: SplitSide) => number;
131131
getUnifiedHunkLine: (index: number) => DiffHunkItem;
132-
onUnifiedHunkExpand: (dir: "up" | "down" | "all", index: number, needTrigger?: boolean) => void;
132+
onUnifiedHunkExpand: (dir: "up" | "down" | "all" | "up-all" | "down-all", index: number, needTrigger?: boolean) => void;
133133
onAllExpand: (mode: "split" | "unified") => void;
134134
get hasExpandSplitAll(): boolean;
135135
get hasExpandUnifiedAll(): boolean;

packages/react/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
"react diff component"
6868
],
6969
"dependencies": {
70-
"@git-diff-view/core": "^0.0.35",
70+
"@git-diff-view/core": "^0.0.36",
7171
"@types/hast": "^3.0.0",
7272
"fast-diff": "^1.3.0",
7373
"highlight.js": "^11.11.0",

packages/solid/index.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,12 +124,12 @@ export declare class DiffFile {
124124
getSplitLineIndexByLineNumber: (lineNumber: number, side: SplitSide) => number;
125125
getSplitRightLine: (index: number) => SplitLineItem;
126126
getSplitHunkLine: (index: number) => DiffHunkItem;
127-
onSplitHunkExpand: (dir: "up" | "down" | "all", index: number, needTrigger?: boolean) => void;
127+
onSplitHunkExpand: (dir: "up" | "down" | "all" | "up-all" | "down-all", index: number, needTrigger?: boolean) => void;
128128
getUnifiedLine: (index: number) => UnifiedLineItem;
129129
getUnifiedLineByLineNumber: (lienNumber: number, side: SplitSide) => UnifiedLineItem;
130130
getUnifiedLineIndexByLineNumber: (lineNumber: number, side: SplitSide) => number;
131131
getUnifiedHunkLine: (index: number) => DiffHunkItem;
132-
onUnifiedHunkExpand: (dir: "up" | "down" | "all", index: number, needTrigger?: boolean) => void;
132+
onUnifiedHunkExpand: (dir: "up" | "down" | "all" | "up-all" | "down-all", index: number, needTrigger?: boolean) => void;
133133
onAllExpand: (mode: "split" | "unified") => void;
134134
get hasExpandSplitAll(): boolean;
135135
get hasExpandUnifiedAll(): boolean;

0 commit comments

Comments
 (0)