Skip to content

Commit 038fe90

Browse files
Merge pull request #55 from MrWangJustToDo/feat/performance
update
2 parents 40c3e2a + 0dcf238 commit 038fe90

20 files changed

Lines changed: 449 additions & 981 deletions

File tree

packages/cli/src/components/DiffUnifiedContentLine.tsx

Lines changed: 131 additions & 148 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { type DiffFile, type DiffLine, checkDiffLineIncludeChange, type File } f
22
import { NewLineSymbol } from "@git-diff-view/utils";
33
import { Box, Text } from "ink";
44
import * as React from "react";
5-
import { memo } from "react";
65

76
import {
87
diffAddLineNumber,
@@ -161,171 +160,155 @@ const DiffUnifiedNewLine = ({
161160
);
162161
};
163162

164-
const _DiffUnifiedLine = memo(
165-
({
166-
index,
167-
width,
168-
theme,
169-
columns,
170-
diffFile,
171-
lineNumber,
172-
enableHighlight,
173-
}: {
174-
index: number;
175-
width: number;
176-
theme: "light" | "dark";
177-
columns: number;
178-
diffFile: DiffFile;
179-
lineNumber: number;
180-
enableHighlight: boolean;
181-
}) => {
182-
const unifiedLine = diffFile.getUnifiedLine(index);
163+
const InternalDiffUnifiedLine = ({
164+
index,
165+
width,
166+
theme,
167+
columns,
168+
diffFile,
169+
lineNumber,
170+
enableHighlight,
171+
}: {
172+
index: number;
173+
width: number;
174+
theme: "light" | "dark";
175+
columns: number;
176+
diffFile: DiffFile;
177+
lineNumber: number;
178+
enableHighlight: boolean;
179+
}) => {
180+
const unifiedLine = diffFile.getUnifiedLine(index);
183181

184-
const hasDiff = unifiedLine.diff;
182+
const hasDiff = unifiedLine.diff;
185183

186-
const hasChange = checkDiffLineIncludeChange(unifiedLine.diff);
184+
const hasChange = checkDiffLineIncludeChange(unifiedLine.diff);
187185

188-
const rawLine = unifiedLine.value || "";
186+
const rawLine = unifiedLine.value || "";
189187

190-
const diffLine = unifiedLine.diff;
188+
const diffLine = unifiedLine.diff;
191189

192-
const newLineNumber = unifiedLine.newLineNumber;
190+
const newLineNumber = unifiedLine.newLineNumber;
193191

194-
const oldLinenumber = unifiedLine.oldLineNumber;
192+
const oldLinenumber = unifiedLine.oldLineNumber;
195193

196-
const syntaxLine = newLineNumber
197-
? diffFile.getNewSyntaxLine(newLineNumber)
198-
: oldLinenumber
199-
? diffFile.getOldSyntaxLine(oldLinenumber)
200-
: undefined;
194+
const syntaxLine = newLineNumber
195+
? diffFile.getNewSyntaxLine(newLineNumber)
196+
: oldLinenumber
197+
? diffFile.getOldSyntaxLine(oldLinenumber)
198+
: undefined;
201199

202-
const plainLine = newLineNumber
203-
? diffFile.getNewPlainLine(newLineNumber)
204-
: oldLinenumber
205-
? diffFile.getOldPlainLine(oldLinenumber)
206-
: undefined;
200+
const plainLine = newLineNumber
201+
? diffFile.getNewPlainLine(newLineNumber)
202+
: oldLinenumber
203+
? diffFile.getOldPlainLine(oldLinenumber)
204+
: undefined;
207205

208-
const contentWidth = columns - (width + 1) * 2;
206+
const contentWidth = columns - (width + 1) * 2;
209207

210-
let row = getCurrentLineRow({ content: rawLine, width: contentWidth });
208+
let row = getCurrentLineRow({ content: rawLine, width: contentWidth });
211209

212-
row = diffLine?.changes?.hasLineChange && diffLine.changes.newLineSymbol === NewLineSymbol.NEWLINE ? row + 1 : row;
210+
row = diffLine?.changes?.hasLineChange && diffLine.changes.newLineSymbol === NewLineSymbol.NEWLINE ? row + 1 : row;
213211

214-
const color = hasDiff
215-
? theme === "light"
216-
? diffPlainLineNumberColor.light
217-
: diffPlainLineNumberColor.dark
218-
: theme === "light"
219-
? diffExpandLineNumberColor.light
220-
: diffExpandLineNumberColor.dark;
212+
const color = hasDiff
213+
? theme === "light"
214+
? diffPlainLineNumberColor.light
215+
: diffPlainLineNumberColor.dark
216+
: theme === "light"
217+
? diffExpandLineNumberColor.light
218+
: diffExpandLineNumberColor.dark;
221219

222-
const bg = hasDiff
223-
? theme === "light"
224-
? diffPlainLineNumber.light
225-
: diffPlainLineNumber.dark
226-
: theme === "light"
227-
? diffExpandLineNumber.light
228-
: diffExpandLineNumber.dark;
220+
const bg = hasDiff
221+
? theme === "light"
222+
? diffPlainLineNumber.light
223+
: diffPlainLineNumber.dark
224+
: theme === "light"
225+
? diffExpandLineNumber.light
226+
: diffExpandLineNumber.dark;
229227

230-
if (hasChange) {
231-
if (unifiedLine.oldLineNumber) {
232-
return (
233-
<DiffUnifiedOldLine
234-
theme={theme}
235-
width={width}
236-
height={row}
237-
columns={columns}
238-
rawLine={rawLine}
239-
diffFile={diffFile}
240-
index={lineNumber}
241-
diffLine={diffLine}
242-
plainLine={plainLine}
243-
syntaxLine={syntaxLine}
244-
contentWidth={contentWidth}
245-
enableHighlight={enableHighlight}
246-
lineNumber={unifiedLine.oldLineNumber}
247-
/>
248-
);
249-
} else {
250-
return (
251-
<DiffUnifiedNewLine
252-
theme={theme}
253-
width={width}
254-
height={row}
255-
columns={columns}
256-
rawLine={rawLine}
257-
index={lineNumber}
258-
diffLine={diffLine}
259-
diffFile={diffFile}
260-
plainLine={plainLine}
261-
syntaxLine={syntaxLine}
262-
contentWidth={contentWidth}
263-
enableHighlight={enableHighlight}
264-
lineNumber={unifiedLine.newLineNumber!}
265-
/>
266-
);
267-
}
228+
if (hasChange) {
229+
if (unifiedLine.oldLineNumber) {
230+
return (
231+
<DiffUnifiedOldLine
232+
theme={theme}
233+
width={width}
234+
height={row}
235+
columns={columns}
236+
rawLine={rawLine}
237+
diffFile={diffFile}
238+
index={lineNumber}
239+
diffLine={diffLine}
240+
plainLine={plainLine}
241+
syntaxLine={syntaxLine}
242+
contentWidth={contentWidth}
243+
enableHighlight={enableHighlight}
244+
lineNumber={unifiedLine.oldLineNumber}
245+
/>
246+
);
268247
} else {
269248
return (
270-
<Box data-line={lineNumber} data-state={unifiedLine.diff ? "diff" : "plain"} height={row} width={columns}>
271-
<Box width={width * 2 + 2} flexShrink={0}>
272-
<Box width={width} justifyContent="flex-end">
273-
<Text
274-
dimColor
275-
wrap="wrap"
276-
color={color}
277-
backgroundColor={bg}
278-
data-line-old-num={unifiedLine.oldLineNumber}
279-
>
280-
{unifiedLine.oldLineNumber
281-
.toString()
282-
.padStart(width)
283-
.padEnd(width * row)}
284-
</Text>
285-
</Box>
286-
<Box width={1}>
287-
<Text backgroundColor={bg} wrap="wrap">
288-
{" ".padEnd(row)}
289-
</Text>
290-
</Box>
291-
<Box width={width} justifyContent="flex-end">
292-
<Text
293-
dimColor
294-
wrap="wrap"
295-
color={color}
296-
backgroundColor={bg}
297-
data-line-new-num={unifiedLine.newLineNumber}
298-
>
299-
{unifiedLine.newLineNumber
300-
.toString()
301-
.padStart(width)
302-
.padEnd(width * row)}
303-
</Text>
304-
</Box>
305-
<Box width={1}>
306-
<Text backgroundColor={bg} wrap="wrap">
307-
{" ".padEnd(row)}
308-
</Text>
309-
</Box>
310-
</Box>
311-
<DiffContent
312-
theme={theme}
313-
height={row}
314-
rawLine={rawLine}
315-
diffFile={diffFile}
316-
diffLine={diffLine}
317-
width={contentWidth}
318-
plainLine={plainLine}
319-
syntaxLine={syntaxLine}
320-
enableHighlight={enableHighlight}
321-
/>
322-
</Box>
249+
<DiffUnifiedNewLine
250+
theme={theme}
251+
width={width}
252+
height={row}
253+
columns={columns}
254+
rawLine={rawLine}
255+
index={lineNumber}
256+
diffLine={diffLine}
257+
diffFile={diffFile}
258+
plainLine={plainLine}
259+
syntaxLine={syntaxLine}
260+
contentWidth={contentWidth}
261+
enableHighlight={enableHighlight}
262+
lineNumber={unifiedLine.newLineNumber!}
263+
/>
323264
);
324265
}
266+
} else {
267+
return (
268+
<Box data-line={lineNumber} data-state={unifiedLine.diff ? "diff" : "plain"} height={row} width={columns}>
269+
<Box width={width * 2 + 2} flexShrink={0}>
270+
<Box width={width} justifyContent="flex-end">
271+
<Text dimColor wrap="wrap" color={color} backgroundColor={bg} data-line-old-num={unifiedLine.oldLineNumber}>
272+
{unifiedLine.oldLineNumber
273+
.toString()
274+
.padStart(width)
275+
.padEnd(width * row)}
276+
</Text>
277+
</Box>
278+
<Box width={1}>
279+
<Text backgroundColor={bg} wrap="wrap">
280+
{" ".padEnd(row)}
281+
</Text>
282+
</Box>
283+
<Box width={width} justifyContent="flex-end">
284+
<Text dimColor wrap="wrap" color={color} backgroundColor={bg} data-line-new-num={unifiedLine.newLineNumber}>
285+
{unifiedLine.newLineNumber
286+
.toString()
287+
.padStart(width)
288+
.padEnd(width * row)}
289+
</Text>
290+
</Box>
291+
<Box width={1}>
292+
<Text backgroundColor={bg} wrap="wrap">
293+
{" ".padEnd(row)}
294+
</Text>
295+
</Box>
296+
</Box>
297+
<DiffContent
298+
theme={theme}
299+
height={row}
300+
rawLine={rawLine}
301+
diffFile={diffFile}
302+
diffLine={diffLine}
303+
width={contentWidth}
304+
plainLine={plainLine}
305+
syntaxLine={syntaxLine}
306+
enableHighlight={enableHighlight}
307+
/>
308+
</Box>
309+
);
325310
}
326-
);
327-
328-
_DiffUnifiedLine.displayName = "_DiffUnifiedLine";
311+
};
329312

330313
export const DiffUnifiedContentLine = ({
331314
index,
@@ -349,7 +332,7 @@ export const DiffUnifiedContentLine = ({
349332
if (unifiedLine?.isHidden) return null;
350333

351334
return (
352-
<_DiffUnifiedLine
335+
<InternalDiffUnifiedLine
353336
index={index}
354337
width={width}
355338
theme={theme}

packages/core/index.d.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ declare class File$1 {
2929
highlighterName?: DiffHighlighter["name"];
3030
highlighterType?: DiffHighlighter["type"];
3131
maxLineNumber: number;
32+
/**
33+
* @deprecated
34+
*/
3235
enableTemplate: boolean;
3336
static createInstance(data: File$1): File$1;
3437
constructor(row: string, lang: DiffHighlighterLang, fileName?: string);
@@ -113,12 +116,9 @@ export declare class DiffFile {
113116
initTheme(theme?: "light" | "dark"): void;
114117
initRaw(): void;
115118
initSyntax({ registerHighlighter }?: {
116-
registerHighlighter?: Omit<DiffHighlighter, "getHighlighterEngine">;
119+
registerHighlighter?: DiffFileHighlighter;
117120
}): void;
118121
init(): void;
119-
enableTemplate(): void;
120-
disableTemplate(): void;
121-
getIsEnableTemplate(): boolean;
122122
buildSplitDiffLines(): void;
123123
buildUnifiedDiffLines(): void;
124124
getSplitLeftLine: (index: number) => SplitLineItem;
@@ -205,11 +205,11 @@ export declare class DiffFile {
205205
};
206206
version: string;
207207
theme: "light" | "dark";
208-
enableTemplate: boolean;
209208
isFullMerge: boolean;
210209
};
211210
mergeBundle: (data: ReturnType<DiffFile["getBundle"]>, notifyUpdate?: boolean) => void;
212211
_getHighlighterName: () => string;
212+
_getHighlighterType: () => string;
213213
_getIsPureDiffRender: () => boolean;
214214
_getTheme: () => "light" | "dark";
215215
_addClonedInstance: (instance: DiffFile) => void;
@@ -267,7 +267,6 @@ export declare class DiffFile {
267267
};
268268
version: string;
269269
theme: "light" | "dark";
270-
enableTemplate: boolean;
271270
};
272271
_mergeFullBundle: (data: ReturnType<DiffFile["_getFullBundle"]>, notifyUpdate?: boolean) => void;
273272
_destroy: () => void;
@@ -718,6 +717,7 @@ export interface UnifiedLineItem {
718717
_isHidden?: boolean;
719718
}
720719
export type DiffAST = Root;
720+
export type DiffFileHighlighter = Omit<DiffHighlighter, "getHighlighterEngine">;
721721
export type DiffHighlighter = {
722722
name: string;
723723
type: "class" | "style" | string;

0 commit comments

Comments
 (0)