Skip to content

Commit 23c376e

Browse files
Merge pull request #15 from MrWangJustToDo/feature/type
type improve
2 parents bfcb0f1 + efacd01 commit 23c376e

File tree

22 files changed

+568
-83
lines changed

22 files changed

+568
-83
lines changed

packages/core/index.d.ts

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ declare class Cache$1<K, V> extends Map<K, V> {
99
declare class File$1 {
1010

1111
readonly raw: string;
12-
readonly lang: string;
12+
readonly lang: DiffHighlighterLang | string;
1313
readonly fileName?: string;
1414
ast?: DiffAST;
1515
rawFile: Record<number, string>;
@@ -22,7 +22,8 @@ declare class File$1 {
2222
highlighterType?: DiffHighlighter["type"];
2323
maxLineNumber: number;
2424
static createInstance(data: File$1): File$1;
25-
constructor(raw: string, lang: string, fileName?: string);
25+
constructor(row: string, lang: DiffHighlighterLang, fileName?: string);
26+
constructor(row: string, lang: string, fileName?: string);
2627
doSyntax({ registerHighlighter, theme, }: {
2728
registerHighlighter?: Omit<DiffHighlighter, "getHighlighterEngine">;
2829
theme?: "light" | "dark";
@@ -49,10 +50,10 @@ export declare class DiffFile {
4950
_version_: string;
5051
_oldFileName: string;
5152
_oldFileContent: string;
52-
_oldFileLang: string;
53+
_oldFileLang: DiffHighlighterLang | string;
5354
_newFileName: string;
5455
_newFileContent: string;
55-
_newFileLang: string;
56+
_newFileLang: DiffHighlighterLang | string;
5657
_diffList: string[];
5758
diffLineLength: number;
5859
splitLineLength: number;
@@ -61,7 +62,9 @@ export declare class DiffFile {
6162
hasExpandSplitAll: boolean;
6263
hasExpandUnifiedAll: boolean;
6364
hasSomeLineCollapsed: boolean;
64-
static createInstance(data: FileData, bundle?: ReturnType<DiffFile["getBundle"] | DiffFile["_getFullBundle"]>): DiffFile;
65+
static createInstance(data: FileData_1, bundle?: ReturnType<DiffFile["getBundle"] | DiffFile["_getFullBundle"]>): DiffFile;
66+
static createInstance(data: FileData_2, bundle?: ReturnType<DiffFile["getBundle"] | DiffFile["_getFullBundle"]>): DiffFile;
67+
constructor(_oldFileName: string, _oldFileContent: string, _newFileName: string, _newFileContent: string, _diffList: string[], _oldFileLang?: DiffHighlighterLang, _newFileLang?: DiffHighlighterLang, uuid?: string);
6568
constructor(_oldFileName: string, _oldFileContent: string, _newFileName: string, _newFileContent: string, _diffList: string[], _oldFileLang?: string, _newFileLang?: string, uuid?: string);
6669
initId(): void;
6770
getId(): string;
@@ -353,7 +356,6 @@ export declare const getDiffRange: (additions: DiffLine[], deletions: DiffLine[]
353356
getAdditionRaw: (lineNumber: number) => string;
354357
getDeletionRaw: (lineNumber: number) => string;
355358
}) => void;
356-
export declare const getFile: (raw: string, lang: string, theme: "light" | "dark", fileName?: string, uuid?: string) => File$1;
357359
export declare const getLang: (fileName: string) => string;
358360
export declare const getSplitContentLines: (diffFile: DiffFile) => DiffSplitContentLineItem[];
359361
export declare const getSplitLines: (diffFile: DiffFile) => DiffSplitLineItem[];
@@ -413,6 +415,10 @@ export declare function diffChanges(addition: DiffLine, deletion: DiffLine): {
413415
addRange: DiffRange;
414416
delRange: DiffRange;
415417
};
418+
export declare function getAst(raw: string, fileName?: string, lang?: DiffHighlighterLang, theme?: "light" | "dark"): DiffAST;
419+
export declare function getAst(raw: string, fileName?: string, lang?: string, theme?: "light" | "dark"): DiffAST;
420+
export declare function getFile(raw: string, lang: DiffHighlighterLang, theme: "light" | "dark", fileName?: string, uuid?: string): File$1;
421+
export declare function getFile(raw: string, lang: string, theme: "light" | "dark", fileName?: string, uuid?: string): File$1;
416422
/**
417423
* Calculates whether or not a hunk header can be expanded up, down, both, or if
418424
* the space represented by the hunk header is short and expansion there would
@@ -519,14 +525,15 @@ export type DiffHighlighter = {
519525
setMaxLineToIgnoreSyntax: (v: number) => void;
520526
ignoreSyntaxHighlightList: (string | RegExp)[];
521527
setIgnoreSyntaxHighlightList: (v: (string | RegExp)[]) => void;
522-
getAST: (raw: string, fileName?: string, lang?: string, theme?: "light" | "dark") => DiffAST;
528+
getAST: typeof getAst;
523529
processAST: (ast: DiffAST) => {
524530
syntaxFileObject: Record<number, SyntaxLine>;
525531
syntaxFileLineNumber: number;
526532
};
527533
hasRegisteredCurrentLang: (lang: string) => boolean;
528534
getHighlighterEngine: () => typeof lowlight;
529535
};
536+
export type DiffHighlighterLang = "arduino" | "bash" | "c" | "cpp" | "csharp" | "css" | "diff" | "go" | "graphql" | "ini" | "java" | "javascript" | "jsx" | "json" | "kotlin" | "less" | "lua" | "makefile" | "markdown" | "objectivec" | "perl" | "php" | "php-template" | "plaintext" | "python" | "python-repl" | "r" | "ruby" | "rust" | "scss" | "shell" | "sql" | "swift" | "typescript" | "tsx" | "vbnet" | "wasm" | "xml" | "yaml" | "abnf" | "accesslog" | "actionscript" | "ada" | "angelscript" | "apache" | "applescript" | "arcade" | "armasm" | "asciidoc" | "aspectj" | "autohotkey" | "autoit" | "avrasm" | "awk" | "axapta" | "basic" | "bnf" | "brainfuck" | "cal" | "capnproto" | "ceylon" | "clean" | "clojure" | "clojure-repl" | "cmake" | "coffeescript" | "coq" | "cos" | "crmsh" | "crystal" | "csp" | "d" | "dart" | "delphi" | "django" | "dns" | "dockerfile" | "dos" | "dsconfig" | "dts" | "dust" | "ebnf" | "elixir" | "elm" | "erb" | "erlang" | "erlang-repl" | "excel" | "fix" | "flix" | "fortran" | "fsharp" | "gams" | "gauss" | "gcode" | "gherkin" | "glsl" | "gml" | "golo" | "gradle" | "groovy" | "haml" | "handlebars" | "haskell" | "haxe" | "hsp" | "http" | "hy" | "inform7" | "irpf90" | "isbl" | "jboss-cli" | "julia" | "julia-repl" | "lasso" | "latex" | "ldif" | "leaf" | "lisp" | "livecodeserver" | "livescript" | "llvm" | "lsl" | "mathematica" | "matlab" | "maxima" | "mel" | "mercury" | "mipsasm" | "mizar" | "mojolicious" | "monkey" | "moonscript" | "n1ql" | "nestedtext" | "nginx" | "nim" | "nix" | "node-repl" | "nsis" | "ocaml" | "openscad" | "oxygene" | "parser3" | "pf" | "pgsql" | "pony" | "powershell" | "processing" | "profile" | "prolog" | "properties" | "protobuf" | "puppet" | "purebasic" | "q" | "qml" | "reasonml" | "rib" | "roboconf" | "routeros" | "rsl" | "ruleslanguage" | "sas" | "scala" | "scheme" | "scilab" | "smali" | "smalltalk" | "sml" | "sqf" | "stan" | "stata" | "step21" | "stylus" | "subunit" | "taggerscript" | "tap" | "tcl" | "thrift" | "tp" | "twig" | "vala" | "vbscript" | "vbscript-html" | "verilog" | "vhdl" | "vim" | "wren" | "x86asm" | "xl" | "xquery" | "zephi";
530537
export type DiffSplitContentLineItem = {
531538
type: DiffFileLineType.content;
532539
index: number;
@@ -552,7 +559,20 @@ export type DiffUnifiedLineItem = {
552559
index: number;
553560
lineNumber: number;
554561
};
555-
export type FileData = {
562+
export type FileData_1 = {
563+
oldFile?: {
564+
fileName?: string | null;
565+
fileLang?: DiffHighlighterLang | null;
566+
content?: string | null;
567+
};
568+
newFile?: {
569+
fileName?: string | null;
570+
fileLang?: DiffHighlighterLang | null;
571+
content?: string | null;
572+
};
573+
hunks?: string[];
574+
};
575+
export type FileData_2 = {
556576
oldFile?: {
557577
fileName?: string | null;
558578
fileLang?: string | null;

packages/core/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"description": "@git-diff-view/core",
44
"author": "MrWangJustToDo",
55
"license": "MIT",
6-
"version": "0.0.20",
6+
"version": "0.0.21",
77
"main": "index.js",
88
"types": "index.d.ts",
99
"files": [
@@ -52,7 +52,7 @@
5252
"diff parse"
5353
],
5454
"dependencies": {
55-
"@git-diff-view/lowlight": "^0.0.20",
55+
"@git-diff-view/lowlight": "^0.0.21",
5656
"highlight.js": "^11.9.0",
5757
"lowlight": "^3.1.0",
5858
"fast-diff": "^1.3.0"

packages/core/src/diff-file.ts

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { getFile, File } from "./file";
44
import { DiffLine, DiffLineType, parseInstance, getDiffRange, getLang } from "./parse";
55

66
import type { IRawDiff } from "./parse";
7-
import type { DiffHighlighter } from "@git-diff-view/lowlight";
7+
import type { DiffHighlighter, DiffHighlighterLang } from "@git-diff-view/lowlight";
88

99
export const composeLen = 40;
1010

@@ -60,12 +60,24 @@ export interface DiffHunkItem extends DiffLineItem {
6060
unifiedInfo?: HunkLineInfo & HunkInfo;
6161
}
6262

63-
type FileData = {
63+
type FileData_1 = {
64+
oldFile?: { fileName?: string | null; fileLang?: DiffHighlighterLang | null; content?: string | null };
65+
newFile?: { fileName?: string | null; fileLang?: DiffHighlighterLang | null; content?: string | null };
66+
hunks?: string[];
67+
};
68+
69+
type FileData_2 = {
6470
oldFile?: { fileName?: string | null; fileLang?: string | null; content?: string | null };
6571
newFile?: { fileName?: string | null; fileLang?: string | null; content?: string | null };
6672
hunks?: string[];
6773
};
6874

75+
type FileData_3 = {
76+
oldFile?: { fileName?: string | null; fileLang?: DiffHighlighterLang | string | null; content?: string | null };
77+
newFile?: { fileName?: string | null; fileLang?: DiffHighlighterLang | string | null; content?: string | null };
78+
hunks?: string[];
79+
};
80+
6981
export class DiffFile {
7082
#oldFileResult?: File;
7183

@@ -131,13 +143,13 @@ export class DiffFile {
131143

132144
_oldFileContent: string = "";
133145

134-
_oldFileLang: string = "";
146+
_oldFileLang: DiffHighlighterLang | string = "";
135147

136148
_newFileName: string = "";
137149

138150
_newFileContent: string = "";
139151

140-
_newFileLang: string = "";
152+
_newFileLang: DiffHighlighterLang | string = "";
141153

142154
_diffList: string[] = [];
143155

@@ -159,7 +171,15 @@ export class DiffFile {
159171

160172
#clonedInstance = new Map<DiffFile, () => void>();
161173

162-
static createInstance(data: FileData, bundle?: ReturnType<DiffFile["getBundle"] | DiffFile["_getFullBundle"]>) {
174+
static createInstance(
175+
data: FileData_1,
176+
bundle?: ReturnType<DiffFile["getBundle"] | DiffFile["_getFullBundle"]>
177+
): DiffFile;
178+
static createInstance(
179+
data: FileData_2,
180+
bundle?: ReturnType<DiffFile["getBundle"] | DiffFile["_getFullBundle"]>
181+
): DiffFile;
182+
static createInstance(data: FileData_3, bundle?: ReturnType<DiffFile["getBundle"] | DiffFile["_getFullBundle"]>) {
163183
const instance = new DiffFile(
164184
data?.oldFile?.fileName || "",
165185
data?.oldFile?.content || "",
@@ -180,6 +200,16 @@ export class DiffFile {
180200
return instance;
181201
}
182202

203+
constructor(
204+
_oldFileName: string,
205+
_oldFileContent: string,
206+
_newFileName: string,
207+
_newFileContent: string,
208+
_diffList: string[],
209+
_oldFileLang?: DiffHighlighterLang,
210+
_newFileLang?: DiffHighlighterLang,
211+
uuid?: string
212+
);
183213
constructor(
184214
_oldFileName: string,
185215
_oldFileContent: string,
@@ -188,6 +218,16 @@ export class DiffFile {
188218
_diffList: string[],
189219
_oldFileLang?: string,
190220
_newFileLang?: string,
221+
uuid?: string
222+
);
223+
constructor(
224+
_oldFileName: string,
225+
_oldFileContent: string,
226+
_newFileName: string,
227+
_newFileContent: string,
228+
_diffList: string[],
229+
_oldFileLang?: DiffHighlighterLang | string,
230+
_newFileLang?: DiffHighlighterLang | string,
191231
readonly uuid?: string
192232
) {
193233
Object.defineProperty(this, "__v_skip", { value: true });

packages/core/src/file.ts

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { highlighter } from "@git-diff-view/lowlight";
22

33
import { Cache } from "./cache";
44

5-
import type { DiffAST, DiffHighlighter, SyntaxLine } from "@git-diff-view/lowlight";
5+
import type { DiffAST, DiffHighlighter, DiffHighlighterLang, SyntaxLine } from "@git-diff-view/lowlight";
66

77
const map = new Cache<string, File>();
88

@@ -73,9 +73,11 @@ export class File {
7373
return file;
7474
}
7575

76+
constructor(row: string, lang: DiffHighlighterLang, fileName?: string);
77+
constructor(row: string, lang: string, fileName?: string);
7678
constructor(
7779
readonly raw: string,
78-
readonly lang: string,
80+
readonly lang: DiffHighlighterLang | string,
7981
readonly fileName?: string
8082
) {
8183
Object.defineProperty(this, "__v_skip", { value: true });
@@ -140,15 +142,6 @@ export class File {
140142
this.rawFile[i + 1] = i < rawArray.length - 1 ? rawArray[i] + "\n" : rawArray[i];
141143
}
142144

143-
// reduce 对于大数组性能很差
144-
// this.rawFile = rawArray.reduce(
145-
// (p, item, index) => ({
146-
// ...p,
147-
// [index + 1]: index < rawArray.length - 1 ? item + "\n" : item,
148-
// }),
149-
// {}
150-
// );
151-
152145
this.hasDoRaw = true;
153146
}
154147

@@ -169,7 +162,21 @@ export class File {
169162
}
170163

171164
// TODO add highlight engine key to cache key
172-
export const getFile = (raw: string, lang: string, theme: "light" | "dark", fileName?: string, uuid?: string) => {
165+
export function getFile(
166+
raw: string,
167+
lang: DiffHighlighterLang,
168+
theme: "light" | "dark",
169+
fileName?: string,
170+
uuid?: string
171+
): File;
172+
export function getFile(raw: string, lang: string, theme: "light" | "dark", fileName?: string, uuid?: string): File;
173+
export function getFile(
174+
raw: string,
175+
lang: DiffHighlighterLang | string,
176+
theme: "light" | "dark",
177+
fileName?: string,
178+
uuid?: string
179+
) {
173180
let key = raw + "--" + __VERSION__ + "--" + theme + "--" + lang;
174181

175182
if (uuid) {
@@ -197,6 +204,6 @@ export const getFile = (raw: string, lang: string, theme: "light" | "dark", file
197204
map.set(key, file);
198205

199206
return file;
200-
};
207+
}
201208

202209
export const _cacheMap = map;

0 commit comments

Comments
 (0)