Repository navigation
Expand file tree
/
Copy pathHighlightedCode.tsx
More file actions
189 lines (189 loc) · 4.58 KB
/
Copy pathHighlightedCode.tsx
File metadata and controls
189 lines (189 loc) · 4.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import { c as _c } from "react/compiler-runtime";
import * as React from 'react';
import { memo, useEffect, useMemo, useRef, useState } from 'react';
import { useSettings } from '../hooks/useSettings.js';
import { Ansi, Box, type DOMElement, measureElement, NoSelect, Text, useTheme } from '../ink.js';
import { isFullscreenEnvEnabled } from '../utils/fullscreen.js';
import sliceAnsi from '../utils/sliceAnsi.js';
import { countCharInString } from '../utils/stringUtils.js';
import { HighlightedCodeFallback } from './HighlightedCode/Fallback.js';
import { expectColorFile } from './StructuredDiff/colorDiff.js';
type Props = {
code: string;
filePath: string;
width?: number;
dim?: boolean;
};
const DEFAULT_WIDTH = 80;
export const HighlightedCode = memo(function HighlightedCode(t0: Props) {
const $ = _c(21);
const {
code,
filePath,
width,
dim: t1
} = t0;
const dim = t1 === undefined ? false : t1;
const ref = useRef(null);
const [measuredWidth, setMeasuredWidth] = useState(width || DEFAULT_WIDTH);
const [theme] = useTheme();
const settings = useSettings();
const syntaxHighlightingDisabled = settings.syntaxHighlightingDisabled ?? false;
let t2;
bb0: {
if (syntaxHighlightingDisabled) {
t2 = null;
break bb0;
}
let t3;
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
t3 = expectColorFile();
$[0] = t3;
} else {
t3 = $[0];
}
const ColorFile = t3;
if (!ColorFile) {
t2 = null;
break bb0;
}
let t4;
if ($[1] !== code || $[2] !== filePath) {
t4 = new ColorFile(code, filePath);
$[1] = code;
$[2] = filePath;
$[3] = t4;
} else {
t4 = $[3];
}
t2 = t4;
}
const colorFile = t2;
let t3;
let t4;
if ($[4] !== width) {
t3 = () => {
if (!width && ref.current) {
const {
width: elementWidth
} = measureElement(ref.current);
if (elementWidth > 0) {
setMeasuredWidth(elementWidth - 2);
}
}
};
t4 = [width];
$[4] = width;
$[5] = t3;
$[6] = t4;
} else {
t3 = $[5];
t4 = $[6];
}
useEffect(t3, t4);
let t5;
bb1: {
if (colorFile === null) {
t5 = null;
break bb1;
}
let t6;
if ($[7] !== colorFile || $[8] !== dim || $[9] !== measuredWidth || $[10] !== theme) {
t6 = colorFile.render(theme, measuredWidth, dim);
$[7] = colorFile;
$[8] = dim;
$[9] = measuredWidth;
$[10] = theme;
$[11] = t6;
} else {
t6 = $[11];
}
t5 = t6;
}
const lines = t5;
let t6;
bb2: {
if (!isFullscreenEnvEnabled()) {
t6 = 0;
break bb2;
}
const lineCount = countCharInString(code, "\n") + 1;
let t7;
if ($[12] !== lineCount) {
t7 = lineCount.toString();
$[12] = lineCount;
$[13] = t7;
} else {
t7 = $[13];
}
t6 = t7.length + 2;
}
const gutterWidth = t6;
let t7;
if ($[14] !== code || $[15] !== dim || $[16] !== filePath || $[17] !== gutterWidth || $[18] !== lines || $[19] !== syntaxHighlightingDisabled) {
t7 = <Box ref={ref}>{lines ? <Box flexDirection="column">{lines.map((line, i) => gutterWidth > 0 ? <CodeLine key={i} line={line} gutterWidth={gutterWidth} /> : <Text key={i}><Ansi>{line}</Ansi></Text>)}</Box> : <HighlightedCodeFallback code={code} filePath={filePath} dim={dim} skipColoring={syntaxHighlightingDisabled} />}</Box>;
$[14] = code;
$[15] = dim;
$[16] = filePath;
$[17] = gutterWidth;
$[18] = lines;
$[19] = syntaxHighlightingDisabled;
$[20] = t7;
} else {
t7 = $[20];
}
return t7;
});
function CodeLine(t0) {
const $ = _c(13);
const {
line,
gutterWidth
} = t0;
let t1;
if ($[0] !== gutterWidth || $[1] !== line) {
t1 = sliceAnsi(line, 0, gutterWidth);
$[0] = gutterWidth;
$[1] = line;
$[2] = t1;
} else {
t1 = $[2];
}
const gutter = t1;
let t2;
if ($[3] !== gutterWidth || $[4] !== line) {
t2 = sliceAnsi(line, gutterWidth);
$[3] = gutterWidth;
$[4] = line;
$[5] = t2;
} else {
t2 = $[5];
}
const content = t2;
let t3;
if ($[6] !== gutter) {
t3 = <NoSelect fromLeftEdge={true}><Text><Ansi>{gutter}</Ansi></Text></NoSelect>;
$[6] = gutter;
$[7] = t3;
} else {
t3 = $[7];
}
let t4;
if ($[8] !== content) {
t4 = <Text><Ansi>{content}</Ansi></Text>;
$[8] = content;
$[9] = t4;
} else {
t4 = $[9];
}
let t5;
if ($[10] !== t3 || $[11] !== t4) {
t5 = <Box flexDirection="row">{t3}{t4}</Box>;
$[10] = t3;
$[11] = t4;
$[12] = t5;
} else {
t5 = $[12];
}
return t5;
}