Skip to content

Commit 94aea51

Browse files
type
1 parent 027bd6d commit 94aea51

File tree

12 files changed

+286
-354
lines changed

12 files changed

+286
-354
lines changed

packages/core/index.d.ts

Lines changed: 46 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,32 @@ export declare class DiffFile {
7878
additionLength: number;
7979
deletionLength: number;
8080
hasSomeLineCollapsed: boolean;
81-
static createInstance(data: FileData_1, bundle?: ReturnType<DiffFile["getBundle"] | DiffFile["_getFullBundle"]>): DiffFile;
82-
static createInstance(data: FileData_2, bundle?: ReturnType<DiffFile["getBundle"] | DiffFile["_getFullBundle"]>): DiffFile;
81+
static createInstance(data: {
82+
oldFile?: {
83+
fileName?: string | null;
84+
fileLang?: DiffHighlighterLang | null;
85+
content?: string | null;
86+
};
87+
newFile?: {
88+
fileName?: string | null;
89+
fileLang?: DiffHighlighterLang | null;
90+
content?: string | null;
91+
};
92+
hunks?: string[];
93+
}, bundle?: ReturnType<DiffFile["getBundle"] | DiffFile["_getFullBundle"]>): DiffFile;
94+
static createInstance(data: {
95+
oldFile?: {
96+
fileName?: string | null;
97+
fileLang?: string | null;
98+
content?: string | null;
99+
};
100+
newFile?: {
101+
fileName?: string | null;
102+
fileLang?: string | null;
103+
content?: string | null;
104+
};
105+
hunks?: string[];
106+
}, bundle?: ReturnType<DiffFile["getBundle"] | DiffFile["_getFullBundle"]>): DiffFile;
83107
constructor(_oldFileName: string, _oldFileContent: string, _newFileName: string, _newFileContent: string, _diffList: string[], _oldFileLang?: DiffHighlighterLang, _newFileLang?: DiffHighlighterLang, uuid?: string);
84108
constructor(_oldFileName: string, _oldFileContent: string, _newFileName: string, _newFileContent: string, _diffList: string[], _oldFileLang?: string, _newFileLang?: string, uuid?: string);
85109
initId(): void;
@@ -117,12 +141,16 @@ export declare class DiffFile {
117141
value: string;
118142
template?: string;
119143
};
120-
getOldSyntaxLine: (lineNumber: number) => SyntaxLineWithTemplate;
144+
getOldSyntaxLine: (lineNumber: number) => SyntaxLine & {
145+
template?: string;
146+
};
121147
getNewPlainLine: (lineNumber: number) => {
122148
value: string;
123149
template?: string;
124150
};
125-
getNewSyntaxLine: (lineNumber: number) => SyntaxLineWithTemplate;
151+
getNewSyntaxLine: (lineNumber: number) => SyntaxLine & {
152+
template?: string;
153+
};
126154
subscribe: (listener: (() => void) & {
127155
isSyncExternal?: boolean;
128156
}) => () => void;
@@ -140,15 +168,19 @@ export declare class DiffFile {
140168
value: string;
141169
template?: string;
142170
}>;
143-
oldFileSyntaxLines: Record<number, SyntaxLineWithTemplate>;
171+
oldFileSyntaxLines: Record<number, SyntaxLine & {
172+
template?: string;
173+
}>;
144174
oldFilePlaceholderLines: Record<string, boolean>;
145175
newFileLines: Record<number, string>;
146176
newFileDiffLines: Record<string, DiffLineItem>;
147177
newFilePlainLines: Record<number, {
148178
value: string;
149179
template?: string;
150180
}>;
151-
newFileSyntaxLines: Record<number, SyntaxLineWithTemplate>;
181+
newFileSyntaxLines: Record<number, SyntaxLine & {
182+
template?: string;
183+
}>;
152184
newFilePlaceholderLines: Record<string, boolean>;
153185
splitLineLength: number;
154186
unifiedLineLength: number;
@@ -198,15 +230,19 @@ export declare class DiffFile {
198230
value: string;
199231
template?: string;
200232
}>;
201-
oldFileSyntaxLines: Record<number, SyntaxLineWithTemplate>;
233+
oldFileSyntaxLines: Record<number, SyntaxLine & {
234+
template?: string;
235+
}>;
202236
oldFilePlaceholderLines: Record<string, boolean>;
203237
newFileLines: Record<number, string>;
204238
newFileDiffLines: Record<string, DiffLineItem>;
205239
newFilePlainLines: Record<number, {
206240
value: string;
207241
template?: string;
208242
}>;
209-
newFileSyntaxLines: Record<number, SyntaxLineWithTemplate>;
243+
newFileSyntaxLines: Record<number, SyntaxLine & {
244+
template?: string;
245+
}>;
210246
newFilePlaceholderLines: Record<string, boolean>;
211247
splitLineLength: number;
212248
unifiedLineLength: number;
@@ -418,12 +454,6 @@ export declare class DiffParser {
418454
}
419455
/** How many new lines will be added to a diff hunk by default. */
420456
export declare const DefaultDiffExpansionStep = 40;
421-
/**
422-
* Regular expression matching invisible bidirectional Unicode characters that
423-
* may be interpreted or compiled differently than what it appears. More info:
424-
* https://github.co/hiddenchars
425-
*/
426-
export declare const HiddenBidiCharsRegex: RegExp;
427457
export declare const _cacheMap: Cache$1<string, File$1>;
428458
export declare const changeDefaultComposeLength: (compose: number) => void;
429459
export declare const checkCurrentLineIsHidden: (diffFile: DiffFile, lineNumber: number, side: SplitSide) => {
@@ -434,12 +464,6 @@ export declare const checkDiffLineIncludeChange: (diffLine?: DiffLine) => boolea
434464
export declare const defaultTransform: (content: string) => string;
435465
export declare const disableCache: () => void;
436466
export declare const getCurrentComposeLength: () => number;
437-
export declare const getDiffRange: (additions: DiffLine[], deletions: DiffLine[], { getAdditionRaw, getDeletionRaw, getAdditionSyntax, getDeletionSyntax, }: {
438-
getAdditionRaw: (lineNumber: number) => string;
439-
getDeletionRaw: (lineNumber: number) => string;
440-
getAdditionSyntax: (lineNumber: number) => SyntaxLineWithTemplate;
441-
getDeletionSyntax: (lineNumber: number) => SyntaxLineWithTemplate;
442-
}) => void;
443467
export declare const getEnableFastDiffTemplate: () => boolean;
444468
export declare const getLang: (fileName: string) => string;
445469
export declare const getPlainDiffTemplate: ({ diffLine, rawLine, operator, }: {
@@ -457,12 +481,12 @@ export declare const getSplitContentLines: (diffFile: DiffFile) => DiffSplitCont
457481
export declare const getSplitLines: (diffFile: DiffFile) => DiffSplitLineItem[];
458482
export declare const getSyntaxDiffTemplate: ({ diffLine, syntaxLine, operator, }: {
459483
diffLine: DiffLine;
460-
syntaxLine: SyntaxLineWithTemplate;
484+
syntaxLine: SyntaxLine;
461485
operator: "add" | "del";
462486
}) => void;
463487
export declare const getSyntaxDiffTemplateByFastDiff: ({ diffLine, syntaxLine, operator, }: {
464488
diffLine: DiffLine;
465-
syntaxLine: SyntaxLineWithTemplate;
489+
syntaxLine: SyntaxLine;
466490
operator: "add" | "del";
467491
}) => void;
468492
export declare const getSyntaxLineTemplate: (line: SyntaxLine) => string;
@@ -482,7 +506,6 @@ export declare const highlighter: DiffHighlighter;
482506
* ```
483507
*/
484508
export declare const isTransformEnabled: () => boolean;
485-
export declare const numIterator: <T>(num: number, cb: (index: number) => T) => T[];
486509
export declare const parseInstance: DiffParser;
487510
export declare const processAST: (ast: DiffAST) => {
488511
syntaxFileObject: Record<number, SyntaxLine>;
@@ -594,27 +617,11 @@ export declare enum SplitSide {
594617
}
595618
export declare function _getAST(raw: string, fileName?: string, lang?: DiffHighlighterLang, theme?: "light" | "dark"): DiffAST;
596619
export declare function _getAST(raw: string, fileName?: string, lang?: string, theme?: "light" | "dark"): DiffAST;
597-
export declare function assertNever(_: never, message: string): never;
598620
export declare function diffChanges(addition: DiffLine, deletion: DiffLine): {
599621
addRange: DiffRange;
600622
delRange: DiffRange;
601623
};
602624
export declare function escapeHtml(string: unknown): string;
603-
export declare function getFile(raw: string, lang: DiffHighlighterLang, theme: "light" | "dark", fileName?: string, uuid?: string): File$1;
604-
export declare function getFile(raw: string, lang: string, theme: "light" | "dark", fileName?: string, uuid?: string): File$1;
605-
/**
606-
* Calculates whether or not a hunk header can be expanded up, down, both, or if
607-
* the space represented by the hunk header is short and expansion there would
608-
* mean merging with the hunk above.
609-
*
610-
* @param hunkIndex Index of the hunk to evaluate within the whole diff.
611-
* @param hunkHeader Header of the hunk to evaluate.
612-
* @param previousHunk Hunk previous to the one to evaluate. Null if the
613-
* evaluated hunk is the first one.
614-
*/
615-
export declare function getHunkHeaderExpansionType(hunkIndex: number, hunkHeader: DiffHunkHeader, previousHunk: DiffHunk | null): DiffHunkExpansionType;
616-
/** Utility function for getting the digit count of the largest line number in an array of diff hunks */
617-
export declare function getLargestLineNumber(hunks: DiffHunk[]): number;
618625
/** Get the changed ranges in the strings, relative to each other. */
619626
export declare function relativeChanges(addition: DiffLine, deletion: DiffLine): {
620627
addRange: IRange;
@@ -744,32 +751,6 @@ export type DiffUnifiedLineItem = {
744751
index: number;
745752
lineNumber: number;
746753
};
747-
export type FileData_1 = {
748-
oldFile?: {
749-
fileName?: string | null;
750-
fileLang?: DiffHighlighterLang | null;
751-
content?: string | null;
752-
};
753-
newFile?: {
754-
fileName?: string | null;
755-
fileLang?: DiffHighlighterLang | null;
756-
content?: string | null;
757-
};
758-
hunks?: string[];
759-
};
760-
export type FileData_2 = {
761-
oldFile?: {
762-
fileName?: string | null;
763-
fileLang?: string | null;
764-
content?: string | null;
765-
};
766-
newFile?: {
767-
fileName?: string | null;
768-
fileLang?: string | null;
769-
content?: string | null;
770-
};
771-
hunks?: string[];
772-
};
773754
export type HunkInfo = {
774755
oldStartIndex: number;
775756
newStartIndex: number;

packages/core/src/diff-file.ts

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -72,24 +72,6 @@ export interface DiffHunkItem extends DiffLineItem {
7272
unifiedInfo?: HunkLineInfo & HunkInfo;
7373
}
7474

75-
type FileData_1 = {
76-
oldFile?: { fileName?: string | null; fileLang?: DiffHighlighterLang | null; content?: string | null };
77-
newFile?: { fileName?: string | null; fileLang?: DiffHighlighterLang | null; content?: string | null };
78-
hunks?: string[];
79-
};
80-
81-
type FileData_2 = {
82-
oldFile?: { fileName?: string | null; fileLang?: string | null; content?: string | null };
83-
newFile?: { fileName?: string | null; fileLang?: string | null; content?: string | null };
84-
hunks?: string[];
85-
};
86-
87-
type FileData_3 = {
88-
oldFile?: { fileName?: string | null; fileLang?: DiffHighlighterLang | string | null; content?: string | null };
89-
newFile?: { fileName?: string | null; fileLang?: DiffHighlighterLang | string | null; content?: string | null };
90-
hunks?: string[];
91-
};
92-
9375
export class DiffFile {
9476
#oldFileResult?: File;
9577

@@ -196,14 +178,29 @@ export class DiffFile {
196178
#clonedInstance = new Map<DiffFile, () => void>();
197179

198180
static createInstance(
199-
data: FileData_1,
181+
data: {
182+
oldFile?: { fileName?: string | null; fileLang?: DiffHighlighterLang | null; content?: string | null };
183+
newFile?: { fileName?: string | null; fileLang?: DiffHighlighterLang | null; content?: string | null };
184+
hunks?: string[];
185+
},
200186
bundle?: ReturnType<DiffFile["getBundle"] | DiffFile["_getFullBundle"]>
201187
): DiffFile;
202188
static createInstance(
203-
data: FileData_2,
189+
data: {
190+
oldFile?: { fileName?: string | null; fileLang?: string | null; content?: string | null };
191+
newFile?: { fileName?: string | null; fileLang?: string | null; content?: string | null };
192+
hunks?: string[];
193+
},
204194
bundle?: ReturnType<DiffFile["getBundle"] | DiffFile["_getFullBundle"]>
205195
): DiffFile;
206-
static createInstance(data: FileData_3, bundle?: ReturnType<DiffFile["getBundle"] | DiffFile["_getFullBundle"]>) {
196+
static createInstance(
197+
data: {
198+
oldFile?: { fileName?: string | null; fileLang?: DiffHighlighterLang | string | null; content?: string | null };
199+
newFile?: { fileName?: string | null; fileLang?: DiffHighlighterLang | string | null; content?: string | null };
200+
hunks?: string[];
201+
},
202+
bundle?: ReturnType<DiffFile["getBundle"] | DiffFile["_getFullBundle"]>
203+
) {
207204
const instance = new DiffFile(
208205
data?.oldFile?.fileName || "",
209206
data?.oldFile?.content || "",

packages/core/src/file.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ if (__DEV__ && typeof globalThis !== "undefined") {
2727
}
2828
}
2929

30-
export type SyntaxLineWithTemplate = SyntaxLine & {
30+
type SyntaxLineWithTemplate = SyntaxLine & {
3131
template?: string;
3232
};
3333

@@ -208,14 +208,23 @@ export class File {
208208
}
209209

210210
// TODO add highlight engine key to cache key
211+
/**
212+
* @internal
213+
*/
211214
export function getFile(
212215
raw: string,
213216
lang: DiffHighlighterLang,
214217
theme: "light" | "dark",
215218
fileName?: string,
216219
uuid?: string
217220
): File;
221+
/**
222+
* @internal
223+
*/
218224
export function getFile(raw: string, lang: string, theme: "light" | "dark", fileName?: string, uuid?: string): File;
225+
/**
226+
* @internal
227+
*/
219228
export function getFile(
220229
raw: string,
221230
lang: DiffHighlighterLang | string,

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ const diffHeaderRe = /^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@/;
2828
* may be interpreted or compiled differently than what it appears. More info:
2929
* https://github.co/hiddenchars
3030
*/
31+
/**
32+
* @internal
33+
*/
3134
export const HiddenBidiCharsRegex = /[\u202A-\u202E]|[\u2066-\u2069]/;
3235

3336
const DiffPrefixAdd = "+" as const;

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

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
import { diffChanges, relativeChanges } from "./change-range";
23
import { DiffLineType, DiffLine } from "./diff-line";
34
import { DiffHunkExpansionType } from "./raw-diff";
@@ -9,17 +10,23 @@ import {
910
getSyntaxDiffTemplateByFastDiff,
1011
} from "./template";
1112

12-
import type { SyntaxLineWithTemplate } from "../file";
1313
import type { DiffHunk, DiffHunkHeader } from "./raw-diff";
14+
import type { SyntaxLine } from "@git-diff-view/lowlight";
1415

1516
/** How many new lines will be added to a diff hunk by default. */
1617
export const DefaultDiffExpansionStep = 40;
1718

19+
/**
20+
* @internal
21+
*/
1822
export function assertNever(_: never, message: string): never {
1923
throw new Error(message);
2024
}
2125

2226
/** Utility function for getting the digit count of the largest line number in an array of diff hunks */
27+
/**
28+
* @internal
29+
*/
2330
export function getLargestLineNumber(hunks: DiffHunk[]): number {
2431
if (hunks.length === 0) {
2532
return 0;
@@ -48,7 +55,7 @@ export function getLargestLineNumber(hunks: DiffHunk[]): number {
4855
* Calculates whether or not a hunk header can be expanded up, down, both, or if
4956
* the space represented by the hunk header is short and expansion there would
5057
* mean merging with the hunk above.
51-
*
58+
* @internal
5259
* @param hunkIndex Index of the hunk to evaluate within the whole diff.
5360
* @param hunkHeader Header of the hunk to evaluate.
5461
* @param previousHunk Hunk previous to the one to evaluate. Null if the
@@ -84,6 +91,9 @@ export function getHunkHeaderExpansionType(
8491
}
8592
}
8693

94+
/**
95+
* @internal
96+
*/
8797
export const numIterator = <T>(num: number, cb: (index: number) => T): T[] => {
8898
const re = [];
8999
for (let i = 0; i < num; i++) {
@@ -98,6 +108,9 @@ export const getLang = (fileName: string) => {
98108
return extension;
99109
};
100110

111+
/**
112+
* @internal
113+
*/
101114
export const getDiffRange = (
102115
additions: DiffLine[],
103116
deletions: DiffLine[],
@@ -109,8 +122,8 @@ export const getDiffRange = (
109122
}: {
110123
getAdditionRaw: (lineNumber: number) => string;
111124
getDeletionRaw: (lineNumber: number) => string;
112-
getAdditionSyntax: (lineNumber: number) => SyntaxLineWithTemplate;
113-
getDeletionSyntax: (lineNumber: number) => SyntaxLineWithTemplate;
125+
getAdditionSyntax: (lineNumber: number) => SyntaxLine;
126+
getDeletionSyntax: (lineNumber: number) => SyntaxLine;
114127
}
115128
) => {
116129
if (additions.length === deletions.length) {

0 commit comments

Comments
 (0)