Skip to content

Commit ce2c3c1

Browse files
Merge pull request #42 from MrWangJustToDo/fix/diff-parse
fix diff parse error
2 parents 94aea51 + f5070c8 commit ce2c3c1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+3200
-4204
lines changed

package.json

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@
3838
"@git-diff-view/file": "workspace:*",
3939
"@rollup/plugin-alias": "^5.1.1",
4040
"@swc/core": "^1.10.15",
41-
"@types/lodash": "^4.17.15",
42-
"@types/node": "^22.13.1",
43-
"@types/react": "^18.2.79",
44-
"@types/react-dom": "^18.2.25",
45-
"autoprefixer": "^10.4.20",
41+
"@types/lodash": "^4.17.20",
42+
"@types/node": "^22.18.10",
43+
"@types/react": "^18.3.26",
44+
"@types/react-dom": "^18.3.7",
45+
"autoprefixer": "^10.4.21",
4646
"dts-bundle-generator": "^9.5.1",
4747
"eslint": "^8.57.0",
4848
"husky": "^8.0.2",
@@ -51,17 +51,19 @@
5151
"postcss-import": "^16.1.0",
5252
"postcss-prefix-selector": "^2.1.0",
5353
"prettier": "^3.6.2",
54-
"prettier-plugin-tailwindcss": "^0.6.11",
54+
"prettier-plugin-tailwindcss": "^0.6.14",
5555
"project-tool": "https://github.com/MrWangJustToDo/project-tool.git",
5656
"rollup-plugin-postcss": "^4.0.2",
57-
"tailwindcss": "^3.4.17",
57+
"tailwindcss": "^3.4.18",
5858
"ts-node": "^10.9.2",
5959
"tslib": "^2.8.0",
60-
"typescript": "5.3.3"
60+
"typescript": "^5.9.3"
6161
},
6262
"pnpm": {
6363
"onlyBuiltDependencies": [
64-
"esbuild"
64+
"esbuild",
65+
"@swc/core",
66+
"sharp"
6567
]
6668
}
6769
}

packages/cli/package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,16 @@
5555
"highlight.js": "^11.11.0",
5656
"lowlight": "^3.3.0",
5757
"fast-diff": "^1.3.0",
58-
"reactivity-store": "^0.3.11",
59-
"use-sync-external-store": "^1.4.0"
58+
"reactivity-store": "^0.3.12",
59+
"use-sync-external-store": "^1.6.0"
6060
},
6161
"devDependencies": {
62-
"@my-react/react": "^0.3.17",
63-
"@my-react/react-terminal": "^0.0.11",
64-
"@types/react": "^19.1.8"
62+
"@my-react/react": "^0.3.20",
63+
"@my-react/react-terminal": "^0.0.14",
64+
"@types/react": "^19.2.2"
6565
},
6666
"peerDependencies": {
67-
"react": "^19.1.0",
67+
"react": "^19.2.0",
6868
"ink": "^6.1.0"
6969
}
7070
}

packages/cli/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/* eslint-disable import/export */
2+
13
export * from "./components/DiffView";
24

35
export * from "@git-diff-view/core";

packages/core/index.d.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,16 @@ declare class File$1 {
3939
doRaw(): void;
4040
}
4141
declare const lowlight: {
42-
highlight: (language: string, value: string, options?: Readonly<import("lowlight").Options>) => import("hast").Root;
43-
highlightAuto: (value: string, options?: Readonly<import("lowlight").AutoOptions>) => import("hast").Root;
44-
listLanguages: () => string[];
42+
highlight: (language: string, value: string, options?: Readonly<import("lowlight").Options> | null | undefined) => import("hast").Root;
43+
highlightAuto: (value: string, options?: Readonly<import("lowlight").AutoOptions> | null | undefined) => import("hast").Root;
44+
listLanguages: () => Array<string>;
4545
register: {
4646
(grammars: Readonly<Record<string, import("highlight.js").LanguageFn>>): undefined;
4747
(name: string, grammar: import("highlight.js").LanguageFn): undefined;
4848
};
4949
registerAlias: {
50-
(aliases: Readonly<Record<string, string | readonly string[]>>): undefined;
51-
(language: string, alias: string | readonly string[]): undefined;
50+
(aliases: Readonly<Record<string, ReadonlyArray<string> | string>>): undefined;
51+
(language: string, alias: ReadonlyArray<string> | string): undefined;
5252
};
5353
registered: (aliasOrName: string) => boolean;
5454
};
@@ -456,6 +456,10 @@ export declare class DiffParser {
456456
export declare const DefaultDiffExpansionStep = 40;
457457
export declare const _cacheMap: Cache$1<string, File$1>;
458458
export declare const changeDefaultComposeLength: (compose: number) => void;
459+
/**
460+
* Change the maximum length of a line to ignore line diff.
461+
*/
462+
export declare const changeMaxLengthToIgnoreLineDiff: (length: number) => void;
459463
export declare const checkCurrentLineIsHidden: (diffFile: DiffFile, lineNumber: number, side: SplitSide) => {
460464
split: boolean;
461465
unified: boolean;
@@ -466,6 +470,7 @@ export declare const disableCache: () => void;
466470
export declare const getCurrentComposeLength: () => number;
467471
export declare const getEnableFastDiffTemplate: () => boolean;
468472
export declare const getLang: (fileName: string) => string;
473+
export declare const getMaxLengthToIgnoreLineDiff: () => number;
469474
export declare const getPlainDiffTemplate: ({ diffLine, rawLine, operator, }: {
470475
diffLine: DiffLine;
471476
rawLine: string;
@@ -537,6 +542,7 @@ export declare const processTransformForFile: (content: string) => string;
537542
export declare const processTransformTemplateContent: (content: string) => string;
538543
export declare const resetDefaultComposeLength: () => void;
539544
export declare const resetEnableFastDiffTemplate: () => void;
545+
export declare const resetMaxLengthToIgnoreLineDiff: () => void;
540546
/**
541547
* Resets all transformation functions to their default state and disables transformation.
542548
*

packages/core/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable import/export */
12
export * from "./parse";
23
export * from "./file";
34
export * from "./diff-file";

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,20 @@ export interface DiffRange {
2525
readonly newLineSymbol?: NewLineSymbol;
2626
}
2727

28-
const maxLength = 1000;
28+
let maxLengthToIgnoreLineDiff = 1000;
29+
30+
/**
31+
* Change the maximum length of a line to ignore line diff.
32+
*/
33+
export const changeMaxLengthToIgnoreLineDiff = (length: number) => {
34+
maxLengthToIgnoreLineDiff = length;
35+
};
36+
37+
export const resetMaxLengthToIgnoreLineDiff = () => {
38+
maxLengthToIgnoreLineDiff = 1000;
39+
};
40+
41+
export const getMaxLengthToIgnoreLineDiff = () => maxLengthToIgnoreLineDiff;
2942

3043
/** Get the maximum position in the range. */
3144
function rangeMax(range: IRange["range"]): number {
@@ -58,7 +71,7 @@ function commonLength(
5871
}
5972

6073
function isInValidString(s: string) {
61-
return s.trim().length === 0 || s.length >= maxLength;
74+
return s.trim().length === 0 || s.length >= maxLengthToIgnoreLineDiff;
6275
}
6376

6477
function checkNewLineSymbolChange(

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

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,22 @@ const DiffPrefixAdd = "+" as const;
3737
const DiffPrefixDelete = "-" as const;
3838
const DiffPrefixContext = " " as const;
3939
const DiffPrefixNoNewline = "\\" as const;
40+
// https://github.com/MrWangJustToDo/git-diff-view/issues/41
41+
// some line only have a new line symbol without any other character
42+
const DIffPrefixNewLine = "\n" as const;
4043

4144
type DiffLinePrefix =
4245
| typeof DiffPrefixAdd
4346
| typeof DiffPrefixDelete
4447
| typeof DiffPrefixContext
45-
| typeof DiffPrefixNoNewline;
48+
| typeof DiffPrefixNoNewline
49+
| typeof DIffPrefixNewLine;
4650
const DiffLinePrefixChars: Set<DiffLinePrefix> = new Set([
4751
DiffPrefixAdd,
4852
DiffPrefixDelete,
4953
DiffPrefixContext,
5054
DiffPrefixNoNewline,
55+
DIffPrefixNewLine,
5156
]);
5257

5358
interface IDiffHeaderInfo {
@@ -127,6 +132,19 @@ export class DiffParser {
127132

128133
// We've succeeded if there's anything to read in between the
129134
// start and the end
135+
/**
136+
* https://github.com/MrWangJustToDo/git-diff-view/issues/41
137+
* some file content may have multiple line without any character
138+
* such as
139+
* ```
140+
*
141+
*
142+
*
143+
* +
144+
* +
145+
* ```
146+
* this will cause ls === le, but we not in the end of file
147+
*/
130148
return this.ls !== this.le;
131149
}
132150

@@ -139,7 +157,7 @@ export class DiffParser {
139157
if (header) {
140158
return this.nextLine() ? this.text.substring(this.ls, this.le) : null;
141159
} else {
142-
return this.nextLine() ? this.text.substring(this.ls + 1, this.le + 1) : null;
160+
return this.nextLine() ? this.text.substring(this.ls + 1, this.le + 1) : this.text.length > this.ls ? "\n" : null;
143161
}
144162
}
145163

@@ -353,7 +371,7 @@ export class DiffParser {
353371
diffLine = new DiffLine(line, DiffLineType.Add, diffLineNumber, null, rollingDiffAfterCounter++);
354372
} else if (c === DiffPrefixDelete) {
355373
diffLine = new DiffLine(line, DiffLineType.Delete, diffLineNumber, rollingDiffBeforeCounter++, null);
356-
} else if (c === DiffPrefixContext) {
374+
} else if (c === DiffPrefixContext || c === DIffPrefixNewLine) {
357375
diffLine = new DiffLine(
358376
line,
359377
DiffLineType.Context,

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
import { diffChanges, relativeChanges } from "./change-range";
32
import { DiffLineType, DiffLine } from "./diff-line";
43
import { DiffHunkExpansionType } from "./raw-diff";

packages/file/index.d.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,16 @@ declare class File$1 {
3838
doRaw(): void;
3939
}
4040
declare const lowlight: {
41-
highlight: (language: string, value: string, options?: Readonly<import("lowlight").Options>) => import("hast").Root;
42-
highlightAuto: (value: string, options?: Readonly<import("lowlight").AutoOptions>) => import("hast").Root;
43-
listLanguages: () => string[];
41+
highlight: (language: string, value: string, options?: Readonly<import("lowlight").Options> | null | undefined) => import("hast").Root;
42+
highlightAuto: (value: string, options?: Readonly<import("lowlight").AutoOptions> | null | undefined) => import("hast").Root;
43+
listLanguages: () => Array<string>;
4444
register: {
4545
(grammars: Readonly<Record<string, import("highlight.js").LanguageFn>>): undefined;
4646
(name: string, grammar: import("highlight.js").LanguageFn): undefined;
4747
};
4848
registerAlias: {
49-
(aliases: Readonly<Record<string, string | readonly string[]>>): undefined;
50-
(language: string, alias: string | readonly string[]): undefined;
49+
(aliases: Readonly<Record<string, ReadonlyArray<string> | string>>): undefined;
50+
(language: string, alias: ReadonlyArray<string> | string): undefined;
5151
};
5252
registered: (aliasOrName: string) => boolean;
5353
};
@@ -454,6 +454,10 @@ export declare class DiffParser {
454454
export declare const DefaultDiffExpansionStep = 40;
455455
export declare const _cacheMap: Cache$1<string, File$1>;
456456
export declare const changeDefaultComposeLength: (compose: number) => void;
457+
/**
458+
* Change the maximum length of a line to ignore line diff.
459+
*/
460+
export declare const changeMaxLengthToIgnoreLineDiff: (length: number) => void;
457461
export declare const checkCurrentLineIsHidden: (diffFile: DiffFile, lineNumber: number, side: SplitSide) => {
458462
split: boolean;
459463
unified: boolean;
@@ -464,6 +468,7 @@ export declare const disableCache: () => void;
464468
export declare const getCurrentComposeLength: () => number;
465469
export declare const getEnableFastDiffTemplate: () => boolean;
466470
export declare const getLang: (fileName: string) => string;
471+
export declare const getMaxLengthToIgnoreLineDiff: () => number;
467472
export declare const getPlainDiffTemplate: ({ diffLine, rawLine, operator, }: {
468473
diffLine: DiffLine;
469474
rawLine: string;
@@ -535,6 +540,7 @@ export declare const processTransformForFile: (content: string) => string;
535540
export declare const processTransformTemplateContent: (content: string) => string;
536541
export declare const resetDefaultComposeLength: () => void;
537542
export declare const resetEnableFastDiffTemplate: () => void;
543+
export declare const resetMaxLengthToIgnoreLineDiff: () => void;
538544
/**
539545
* Resets all transformation functions to their default state and disables transformation.
540546
*

packages/lowlight/index.d.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,16 @@ export type DiffHighlighterLang = "arduino" | "bash" | "c" | "cpp" | "csharp" |
3232
export declare function _getAST(raw: string, fileName?: string, lang?: DiffHighlighterLang, theme?: "light" | "dark"): DiffAST;
3333
export declare function _getAST(raw: string, fileName?: string, lang?: string, theme?: "light" | "dark"): DiffAST;
3434
declare const lowlight: {
35-
highlight: (language: string, value: string, options?: Readonly<import("lowlight").Options>) => import("hast").Root;
36-
highlightAuto: (value: string, options?: Readonly<import("lowlight").AutoOptions>) => import("hast").Root;
37-
listLanguages: () => string[];
35+
highlight: (language: string, value: string, options?: Readonly<import("lowlight").Options> | null | undefined) => import("hast").Root;
36+
highlightAuto: (value: string, options?: Readonly<import("lowlight").AutoOptions> | null | undefined) => import("hast").Root;
37+
listLanguages: () => Array<string>;
3838
register: {
3939
(grammars: Readonly<Record<string, import("highlight.js").LanguageFn>>): undefined;
4040
(name: string, grammar: import("highlight.js").LanguageFn): undefined;
4141
};
4242
registerAlias: {
43-
(aliases: Readonly<Record<string, string | readonly string[]>>): undefined;
44-
(language: string, alias: string | readonly string[]): undefined;
43+
(aliases: Readonly<Record<string, ReadonlyArray<string> | string>>): undefined;
44+
(language: string, alias: ReadonlyArray<string> | string): undefined;
4545
};
4646
registered: (aliasOrName: string) => boolean;
4747
};

0 commit comments

Comments
 (0)