Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 28 additions & 8 deletions packages/core/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ declare class Cache$1<K, V> extends Map<K, V> {
declare class File$1 {

readonly raw: string;
readonly lang: string;
readonly lang: DiffHighlighterLang | string;
readonly fileName?: string;
ast?: DiffAST;
rawFile: Record<number, string>;
Expand All @@ -22,7 +22,8 @@ declare class File$1 {
highlighterType?: DiffHighlighter["type"];
maxLineNumber: number;
static createInstance(data: File$1): File$1;
constructor(raw: string, lang: string, fileName?: string);
constructor(row: string, lang: DiffHighlighterLang, fileName?: string);
constructor(row: string, lang: string, fileName?: string);
doSyntax({ registerHighlighter, theme, }: {
registerHighlighter?: Omit<DiffHighlighter, "getHighlighterEngine">;
theme?: "light" | "dark";
Expand All @@ -49,10 +50,10 @@ export declare class DiffFile {
_version_: string;
_oldFileName: string;
_oldFileContent: string;
_oldFileLang: string;
_oldFileLang: DiffHighlighterLang | string;
_newFileName: string;
_newFileContent: string;
_newFileLang: string;
_newFileLang: DiffHighlighterLang | string;
_diffList: string[];
diffLineLength: number;
splitLineLength: number;
Expand All @@ -61,7 +62,9 @@ export declare class DiffFile {
hasExpandSplitAll: boolean;
hasExpandUnifiedAll: boolean;
hasSomeLineCollapsed: boolean;
static createInstance(data: FileData, bundle?: ReturnType<DiffFile["getBundle"] | DiffFile["_getFullBundle"]>): DiffFile;
static createInstance(data: FileData_1, bundle?: ReturnType<DiffFile["getBundle"] | DiffFile["_getFullBundle"]>): DiffFile;
static createInstance(data: FileData_2, bundle?: ReturnType<DiffFile["getBundle"] | DiffFile["_getFullBundle"]>): DiffFile;
constructor(_oldFileName: string, _oldFileContent: string, _newFileName: string, _newFileContent: string, _diffList: string[], _oldFileLang?: DiffHighlighterLang, _newFileLang?: DiffHighlighterLang, uuid?: string);
constructor(_oldFileName: string, _oldFileContent: string, _newFileName: string, _newFileContent: string, _diffList: string[], _oldFileLang?: string, _newFileLang?: string, uuid?: string);
initId(): void;
getId(): string;
Expand Down Expand Up @@ -353,7 +356,6 @@ export declare const getDiffRange: (additions: DiffLine[], deletions: DiffLine[]
getAdditionRaw: (lineNumber: number) => string;
getDeletionRaw: (lineNumber: number) => string;
}) => void;
export declare const getFile: (raw: string, lang: string, theme: "light" | "dark", fileName?: string, uuid?: string) => File$1;
export declare const getLang: (fileName: string) => string;
export declare const getSplitContentLines: (diffFile: DiffFile) => DiffSplitContentLineItem[];
export declare const getSplitLines: (diffFile: DiffFile) => DiffSplitLineItem[];
Expand Down Expand Up @@ -413,6 +415,10 @@ export declare function diffChanges(addition: DiffLine, deletion: DiffLine): {
addRange: DiffRange;
delRange: DiffRange;
};
export declare function getAst(raw: string, fileName?: string, lang?: DiffHighlighterLang, theme?: "light" | "dark"): DiffAST;
export declare function getAst(raw: string, fileName?: string, lang?: string, theme?: "light" | "dark"): DiffAST;
export declare function getFile(raw: string, lang: DiffHighlighterLang, theme: "light" | "dark", fileName?: string, uuid?: string): File$1;
export declare function getFile(raw: string, lang: string, theme: "light" | "dark", fileName?: string, uuid?: string): File$1;
/**
* Calculates whether or not a hunk header can be expanded up, down, both, or if
* the space represented by the hunk header is short and expansion there would
Expand Down Expand Up @@ -519,14 +525,15 @@ export type DiffHighlighter = {
setMaxLineToIgnoreSyntax: (v: number) => void;
ignoreSyntaxHighlightList: (string | RegExp)[];
setIgnoreSyntaxHighlightList: (v: (string | RegExp)[]) => void;
getAST: (raw: string, fileName?: string, lang?: string, theme?: "light" | "dark") => DiffAST;
getAST: typeof getAst;
processAST: (ast: DiffAST) => {
syntaxFileObject: Record<number, SyntaxLine>;
syntaxFileLineNumber: number;
};
hasRegisteredCurrentLang: (lang: string) => boolean;
getHighlighterEngine: () => typeof lowlight;
};
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";
export type DiffSplitContentLineItem = {
type: DiffFileLineType.content;
index: number;
Expand All @@ -552,7 +559,20 @@ export type DiffUnifiedLineItem = {
index: number;
lineNumber: number;
};
export type FileData = {
export type FileData_1 = {
oldFile?: {
fileName?: string | null;
fileLang?: DiffHighlighterLang | null;
content?: string | null;
};
newFile?: {
fileName?: string | null;
fileLang?: DiffHighlighterLang | null;
content?: string | null;
};
hunks?: string[];
};
export type FileData_2 = {
oldFile?: {
fileName?: string | null;
fileLang?: string | null;
Expand Down
4 changes: 2 additions & 2 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "@git-diff-view/core",
"author": "MrWangJustToDo",
"license": "MIT",
"version": "0.0.20",
"version": "0.0.21",
"main": "index.js",
"types": "index.d.ts",
"files": [
Expand Down Expand Up @@ -52,7 +52,7 @@
"diff parse"
],
"dependencies": {
"@git-diff-view/lowlight": "^0.0.20",
"@git-diff-view/lowlight": "^0.0.21",
"highlight.js": "^11.9.0",
"lowlight": "^3.1.0",
"fast-diff": "^1.3.0"
Expand Down
50 changes: 45 additions & 5 deletions packages/core/src/diff-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { getFile, File } from "./file";
import { DiffLine, DiffLineType, parseInstance, getDiffRange, getLang } from "./parse";

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

export const composeLen = 40;

Expand Down Expand Up @@ -60,12 +60,24 @@ export interface DiffHunkItem extends DiffLineItem {
unifiedInfo?: HunkLineInfo & HunkInfo;
}

type FileData = {
type FileData_1 = {
oldFile?: { fileName?: string | null; fileLang?: DiffHighlighterLang | null; content?: string | null };
newFile?: { fileName?: string | null; fileLang?: DiffHighlighterLang | null; content?: string | null };
hunks?: string[];
};

type FileData_2 = {
oldFile?: { fileName?: string | null; fileLang?: string | null; content?: string | null };
newFile?: { fileName?: string | null; fileLang?: string | null; content?: string | null };
hunks?: string[];
};

type FileData_3 = {
oldFile?: { fileName?: string | null; fileLang?: DiffHighlighterLang | string | null; content?: string | null };
newFile?: { fileName?: string | null; fileLang?: DiffHighlighterLang | string | null; content?: string | null };
hunks?: string[];
};

export class DiffFile {
#oldFileResult?: File;

Expand Down Expand Up @@ -131,13 +143,13 @@ export class DiffFile {

_oldFileContent: string = "";

_oldFileLang: string = "";
_oldFileLang: DiffHighlighterLang | string = "";

_newFileName: string = "";

_newFileContent: string = "";

_newFileLang: string = "";
_newFileLang: DiffHighlighterLang | string = "";

_diffList: string[] = [];

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

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

static createInstance(data: FileData, bundle?: ReturnType<DiffFile["getBundle"] | DiffFile["_getFullBundle"]>) {
static createInstance(
data: FileData_1,
bundle?: ReturnType<DiffFile["getBundle"] | DiffFile["_getFullBundle"]>
): DiffFile;
static createInstance(
data: FileData_2,
bundle?: ReturnType<DiffFile["getBundle"] | DiffFile["_getFullBundle"]>
): DiffFile;
static createInstance(data: FileData_3, bundle?: ReturnType<DiffFile["getBundle"] | DiffFile["_getFullBundle"]>) {
const instance = new DiffFile(
data?.oldFile?.fileName || "",
data?.oldFile?.content || "",
Expand All @@ -180,6 +200,16 @@ export class DiffFile {
return instance;
}

constructor(
_oldFileName: string,
_oldFileContent: string,
_newFileName: string,
_newFileContent: string,
_diffList: string[],
_oldFileLang?: DiffHighlighterLang,
_newFileLang?: DiffHighlighterLang,
uuid?: string
);
constructor(
_oldFileName: string,
_oldFileContent: string,
Expand All @@ -188,6 +218,16 @@ export class DiffFile {
_diffList: string[],
_oldFileLang?: string,
_newFileLang?: string,
uuid?: string
);
constructor(
_oldFileName: string,
_oldFileContent: string,
_newFileName: string,
_newFileContent: string,
_diffList: string[],
_oldFileLang?: DiffHighlighterLang | string,
_newFileLang?: DiffHighlighterLang | string,
readonly uuid?: string
) {
Object.defineProperty(this, "__v_skip", { value: true });
Expand Down
33 changes: 20 additions & 13 deletions packages/core/src/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { highlighter } from "@git-diff-view/lowlight";

import { Cache } from "./cache";

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

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

Expand Down Expand Up @@ -73,9 +73,11 @@ export class File {
return file;
}

constructor(row: string, lang: DiffHighlighterLang, fileName?: string);
constructor(row: string, lang: string, fileName?: string);
constructor(
readonly raw: string,
readonly lang: string,
readonly lang: DiffHighlighterLang | string,
readonly fileName?: string
) {
Object.defineProperty(this, "__v_skip", { value: true });
Expand Down Expand Up @@ -140,15 +142,6 @@ export class File {
this.rawFile[i + 1] = i < rawArray.length - 1 ? rawArray[i] + "\n" : rawArray[i];
}

// reduce 对于大数组性能很差
// this.rawFile = rawArray.reduce(
// (p, item, index) => ({
// ...p,
// [index + 1]: index < rawArray.length - 1 ? item + "\n" : item,
// }),
// {}
// );

this.hasDoRaw = true;
}

Expand All @@ -169,7 +162,21 @@ export class File {
}

// TODO add highlight engine key to cache key
export const getFile = (raw: string, lang: string, theme: "light" | "dark", fileName?: string, uuid?: string) => {
export function getFile(
raw: string,
lang: DiffHighlighterLang,
theme: "light" | "dark",
fileName?: string,
uuid?: string
): File;
export function getFile(raw: string, lang: string, theme: "light" | "dark", fileName?: string, uuid?: string): File;
export function getFile(
raw: string,
lang: DiffHighlighterLang | string,
theme: "light" | "dark",
fileName?: string,
uuid?: string
) {
let key = raw + "--" + __VERSION__ + "--" + theme + "--" + lang;

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

return file;
};
}

export const _cacheMap = map;
Loading