@@ -1,24 +1,13 @@
11 import type { Color, ContainerNode, Node, StyleProps } from "@cel-tui/types";
2-import { cel, HStack, Text, VStack } from "@cel-tui/core";
3-import type {
4- BuiltinLanguage,
5- BuiltinTheme,
6- Highlighter,
7- ThemeRegistration,
8- ThemedToken,
9-} from "shiki";
10-import type {
11- ShikiStreamTokenizer,
12- ShikiStreamTokenizerEnqueueResult,
13-} from "shiki-stream";
2+import { HStack, Text, VStack } from "@cel-tui/core";
3+import { all, createLowlight } from "lowlight";
4+
5+const lowlight = createLowlight(all);
146
157 const DEFAULT_THEME_NAME = "cel-ansi16";
8+const APPEND_REHIGHLIGHT_WINDOW = 8_192;
169 const MAX_STATES_PER_KEY = 4;
1710
18-const FONT_STYLE_ITALIC = 1;
19-const FONT_STYLE_BOLD = 2;
20-const FONT_STYLE_UNDERLINE = 4;
21-
2211 const ANSI_SLOT_HEX: ReadonlyArray<readonly [Color, string]> = [
2312 ["color00", "#000000"],
2413 ["color01", "#cd3131"],
@@ -37,182 +26,234 @@ const ANSI_SLOT_HEX: ReadonlyArray<readonly [Color, string]> = [
3726 ["color14", "#29b8db"],
3827 ["color15", "#ffffff"],
3928 ];
40-const ANSI_SLOT_HEX_BY_COLOR = new Map<Color, string>(ANSI_SLOT_HEX);
4129
42-export type SyntaxHighlightTheme = BuiltinTheme | ThemeRegistration;
30+type TokenCategory =
31+ | "addition"
32+ | "comment"
33+ | "deletion"
34+ | "emphasis"
35+ | "function"
36+ | "keyword"
37+ | "link"
38+ | "literal"
39+ | "meta"
40+ | "number"
41+ | "parameter"
42+ | "property"
43+ | "regexp"
44+ | "string"
45+ | "strong"
46+ | "tag"
47+ | "type"
48+ | "variable";
49+
50+/** A best-effort token color override entry. */
51+export interface SyntaxHighlightThemeTokenColor {
52+ /** Target scopes or categories. */
53+ scope?: string | readonly string[];
54+ /** Style settings for the matched scopes. */
55+ settings: {
56+ foreground?: string;
57+ background?: string;
58+ fontStyle?: string;
59+ };
60+}
61+
62+/** Theme registration shape accepted by {@link SyntaxHighlight}. */
63+export interface SyntaxHighlightThemeRegistration {
64+ /** Optional theme name. */
65+ name?: string;
66+ /** Optional light/dark hint. */
67+ type?: "light" | "dark";
68+ /** Optional base foreground color. */
69+ fg?: string;
70+ /** Optional base background color. */
71+ bg?: string;
72+ /** Token color overrides. */
73+ tokenColors?: readonly SyntaxHighlightThemeTokenColor[];
74+}
75+
76+/**
77+ * SyntaxHighlight theme.
78+ *
79+ * String presets are intentionally small and stable. Unknown preset names fall
80+ * back to the default terminal-friendly theme.
81+ */
82+export type SyntaxHighlightTheme = string | SyntaxHighlightThemeRegistration;
4383
4484 /** Props for the {@link SyntaxHighlight} component. */
4585 export interface SyntaxHighlightProps {
4686 /**
47- * Optional Shiki theme override.
87+ * Optional theme override.
4888 *
49- * Pass either a bundled Shiki theme name (for example `"dark-plus"`)
50- * or a custom Shiki theme registration object.
89+ * Built-in presets currently include `"default"` and `"dark-plus"`.
90+ * Theme registration objects are applied as best-effort overrides onto the
91+ * lowlight/highlight.js token categories used by this component.
5192 */
5293 theme?: SyntaxHighlightTheme;
5394 }
5495
55-type SyntaxHighlightStatus = "ready" | "loading" | "error" | "unsupported";
96+interface HighlightRun {
97+ content: string;
98+ style: StyleProps;
99+}
56100
57-interface SyntaxHighlightRuntime {
58- bundledLanguages: Record<string, unknown>;
59- createHighlighter: (typeof import("shiki"))["createHighlighter"];
60- ShikiStreamTokenizer: (typeof import("shiki-stream"))["ShikiStreamTokenizer"];
101+interface ResolvedClassStyle {
102+ resetToBase?: boolean;
103+ style: StyleProps;
61104 }
62105
63106 interface ResolvedSyntaxHighlightTheme {
107+ baseStyle: StyleProps;
64108 cacheKey: string;
65- loadableTheme: SyntaxHighlightTheme;
66- themeName: string;
67- defaultFg?: string;
68- defaultBg?: string;
69- useTerminalDefaults: boolean;
70-}
71-
72-const DEFAULT_THEME: ThemeRegistration = {
73- name: DEFAULT_THEME_NAME,
74- displayName: "cel ANSI 16",
75- type: "dark",
76- fg: ansiHex("color07"),
77- bg: ansiHex("color00"),
78- colors: {
79- foreground: ansiHex("color07"),
80- "editor.foreground": ansiHex("color07"),
81- "editor.background": ansiHex("color00"),
82- "terminal.ansiBlack": ansiHex("color00"),
83- "terminal.ansiRed": ansiHex("color01"),
84- "terminal.ansiGreen": ansiHex("color02"),
85- "terminal.ansiYellow": ansiHex("color03"),
86- "terminal.ansiBlue": ansiHex("color04"),
87- "terminal.ansiMagenta": ansiHex("color05"),
88- "terminal.ansiCyan": ansiHex("color06"),
89- "terminal.ansiWhite": ansiHex("color07"),
90- "terminal.ansiBrightBlack": ansiHex("color08"),
91- "terminal.ansiBrightRed": ansiHex("color09"),
92- "terminal.ansiBrightGreen": ansiHex("color10"),
93- "terminal.ansiBrightYellow": ansiHex("color11"),
94- "terminal.ansiBrightBlue": ansiHex("color12"),
95- "terminal.ansiBrightMagenta": ansiHex("color13"),
96- "terminal.ansiBrightCyan": ansiHex("color14"),
97- "terminal.ansiBrightWhite": ansiHex("color15"),
98- },
99- tokenColors: [
100- { settings: { foreground: ansiHex("color07") } },
101- {
102- scope: ["comment", "string.quoted.docstring.multi"],
103- settings: { foreground: ansiHex("color08"), fontStyle: "italic" },
104- },
105- {
106- scope: ["string", "markup.inline"],
107- settings: { foreground: ansiHex("color02") },
108- },
109- {
110- scope: [
111- "constant.numeric",
112- "constant.language",
113- "constant.character",
114- "constant.other.placeholder",
115- ],
116- settings: { foreground: ansiHex("color03") },
117- },
118- {
119- scope: ["keyword", "storage", "entity.name.operator"],
120- settings: { foreground: ansiHex("color05") },
121- },
122- {
123- scope: ["entity.name.function", "support.function", "meta.function-call"],
124- settings: { foreground: ansiHex("color04") },
125- },
126- {
127- scope: [
128- "entity.name.type",
129- "support.type",
130- "support.class",
131- "entity.other.inherited-class",
132- ],
133- settings: { foreground: ansiHex("color06") },
134- },
135- {
136- scope: ["variable.parameter"],
137- settings: { foreground: ansiHex("color11") },
138- },
139- {
140- scope: [
141- "entity.other.attribute-name",
142- "meta.object-literal.key",
143- "meta.property-name",
144- "support.type.property-name",
145- ],
146- settings: { foreground: ansiHex("color06") },
147- },
148- {
149- scope: ["entity.name.tag", "string.regexp", "invalid"],
150- settings: { foreground: ansiHex("color01") },
151- },
152- {
153- scope: ["markup.bold", "strong", "markup.heading"],
154- settings: { fontStyle: "bold", foreground: ansiHex("color09") },
155- },
156- {
157- scope: ["markup.italic", "emphasis"],
158- settings: { fontStyle: "italic" },
159- },
160- {
161- scope: ["markup.underline", "meta.link.inline.markdown"],
162- settings: { foreground: ansiHex("color04"), fontStyle: "underline" },
163- },
164- ],
165-};
166-
167-const DEFAULT_RESOLVED_THEME: ResolvedSyntaxHighlightTheme = {
168- cacheKey: DEFAULT_THEME_NAME,
169- loadableTheme: DEFAULT_THEME,
170- themeName: DEFAULT_THEME_NAME,
171- defaultFg: normalizeHex(DEFAULT_THEME.fg ?? ansiHex("color07")),
172- defaultBg: normalizeHex(DEFAULT_THEME.bg ?? ansiHex("color00")),
173- useTerminalDefaults: true,
174-};
175-
176-const rgbCache = new Map<string, readonly [number, number, number]>();
177-const colorSlotCache = new Map<string, Color>();
178-const loadedLanguages = new Set<string>();
179-const loadingLanguages = new Map<string, Promise<void>>();
180-const languageErrors = new Map<string, Error>();
181-const loadedThemes = new Set<string>();
182-const loadingThemes = new Map<string, Promise<void>>();
183-const themeErrors = new Map<string, Error>();
184-const statesByKey = new Map<string, SyntaxHighlightState[]>();
185-
186-let runtimePromise: Promise<SyntaxHighlightRuntime> | null = null;
187-let runtimeInstance: SyntaxHighlightRuntime | null = null;
188-let runtimeError: Error | null = null;
189-let highlighterPromise: Promise<Highlighter> | null = null;
190-let highlighterInstance: Highlighter | null = null;
191-let highlighterError: Error | null = null;
109+ classStyles: ReadonlyMap<string, ResolvedClassStyle>;
110+}
192111
193112 interface SyntaxHighlightState {
194113 lastContent: string;
195- tokenizer: ShikiStreamTokenizer;
196114 lastNode: ContainerNode | null;
115+ lines: HighlightRun[][];
197116 theme: ResolvedSyntaxHighlightTheme;
198117 }
199118
200-interface HighlightRun {
201- content: string;
202- style: StyleProps;
119+interface HighlightTextNode {
120+ type: "text";
121+ value: string;
203122 }
204123
205-function ansiHex(color: Color): string {
206- const hex = ANSI_SLOT_HEX_BY_COLOR.get(color);
207- if (!hex) {
208- throw new Error(`Unknown ANSI slot: ${color}`);
209- }
210- return hex;
124+interface HighlightElementNode {
125+ type: "element";
126+ children: HighlightNode[];
127+ properties?: {
128+ className?: string | readonly string[];
129+ };
211130 }
212131
213-function normalizeError(error: unknown): Error {
214- return error instanceof Error ? error : new Error(String(error));
215-}
132+type HighlightNode = HighlightElementNode | HighlightTextNode;
133+
134+const rgbCache = new Map<string, readonly [number, number, number]>();
135+const colorSlotCache = new Map<string, Color>();
136+const statesByKey = new Map<string, SyntaxHighlightState[]>();
137+
138+const DEFAULT_CATEGORY_STYLES: Readonly<Record<TokenCategory, StyleProps>> = {
139+ addition: { fgColor: "color02" },
140+ comment: { fgColor: "color08", italic: true },
141+ deletion: { fgColor: "color01" },
142+ emphasis: { italic: true },
143+ function: { fgColor: "color04" },
144+ keyword: { fgColor: "color05" },
145+ link: { fgColor: "color04", underline: true },
146+ literal: { fgColor: "color03" },
147+ meta: { fgColor: "color09", bold: true },
148+ number: { fgColor: "color03" },
149+ parameter: { fgColor: "color11" },
150+ property: { fgColor: "color06" },
151+ regexp: { fgColor: "color01" },
152+ string: { fgColor: "color02" },
153+ strong: { bold: true },
154+ tag: { fgColor: "color01" },
155+ type: { fgColor: "color06" },
156+ variable: {},
157+};
158+
159+const DARK_PLUS_CATEGORY_STYLES: Readonly<
160+ Partial<Record<TokenCategory, StyleProps>>
161+> = {
162+ comment: { fgColor: "color08", italic: true },
163+ function: { fgColor: "color11" },
164+ keyword: { fgColor: "color12" },
165+ meta: { fgColor: "color08" },
166+ number: { fgColor: "color10" },
167+ property: { fgColor: "color07" },
168+ regexp: { fgColor: "color09" },
169+ string: { fgColor: "color09" },
170+ tag: { fgColor: "color09" },
171+ type: { fgColor: "color06" },
172+};
173+
174+const CATEGORY_CLASSES: Readonly<Record<TokenCategory, readonly string[]>> = {
175+ addition: ["addition"],
176+ comment: ["comment", "doctag", "quote"],
177+ deletion: ["deletion"],
178+ emphasis: ["emphasis"],
179+ function: ["function_", "title"],
180+ keyword: ["keyword", "operator", "selector-pseudo"],
181+ link: ["link"],
182+ literal: ["bullet", "literal", "symbol"],
183+ meta: ["meta", "section"],
184+ number: ["number"],
185+ parameter: ["params"],
186+ property: [
187+ "attr",
188+ "attribute",
189+ "property",
190+ "selector-attr",
191+ "selector-class",
192+ "selector-id",
193+ ],
194+ regexp: ["regexp"],
195+ string: ["string"],
196+ strong: ["strong"],
197+ tag: ["name", "selector-tag", "tag"],
198+ type: ["built_in", "class_", "type"],
199+ variable: ["variable"],
200+};
201+
202+const CATEGORY_SCOPE_HINTS: Readonly<Record<TokenCategory, readonly string[]>> =
203+ {
204+ addition: ["addition", "markup.inserted"],
205+ comment: ["comment", "quote"],
206+ deletion: ["deletion", "markup.deleted"],
207+ emphasis: ["emphasis", "markup.italic"],
208+ function: [
209+ "entity.name.function",
210+ "function",
211+ "meta.function-call",
212+ "support.function",
213+ "title",
214+ ],
215+ keyword: ["entity.name.operator", "keyword", "operator", "storage"],
216+ link: ["link", "meta.link"],
217+ literal: [
218+ "bullet",
219+ "constant.character",
220+ "constant.language",
221+ "literal",
222+ "symbol",
223+ ],
224+ meta: ["markup.heading", "meta", "section"],
225+ number: ["constant.numeric", "number"],
226+ parameter: ["params", "variable.parameter"],
227+ property: [
228+ "attr",
229+ "attribute",
230+ "entity.other.attribute-name",
231+ "meta.object-literal.key",
232+ "meta.property-name",
233+ "property",
234+ "support.type.property-name",
235+ ],
236+ regexp: ["regexp", "string.regexp"],
237+ string: ["markup.inline", "string"],
238+ strong: ["markup.bold", "markup.heading", "strong"],
239+ tag: ["entity.name.tag", "name", "selector-tag", "tag"],
240+ type: [
241+ "built_in",
242+ "class",
243+ "entity.name.type",
244+ "entity.other.inherited-class",
245+ "support.class",
246+ "support.type",
247+ "type",
248+ ],
249+ variable: ["variable"],
250+ };
251+
252+const DEFAULT_RESOLVED_THEME = buildResolvedTheme(
253+ DEFAULT_THEME_NAME,
254+ {},
255+ DEFAULT_CATEGORY_STYLES,
256+);
216257
217258 function hashString(input: string): string {
218259 let hash = 2166136261;
@@ -222,184 +263,207 @@ function hashString(input: string): string {
222263 return (hash >>> 0).toString(36);
223264 }
224265
225-function resolveTheme(
226- theme?: SyntaxHighlightTheme,
227-): ResolvedSyntaxHighlightTheme {
228- if (!theme) {
229- return DEFAULT_RESOLVED_THEME;
266+function normalizeHex(color: string): string {
267+ const lower = color.toLowerCase();
268+ if (lower.length === 4) {
269+ return `#${lower[1]}${lower[1]}${lower[2]}${lower[2]}${lower[3]}${lower[3]}`;
230270 }
231-
232- if (typeof theme === "string") {
233- return {
234- cacheKey: `builtin:${theme}`,
235- loadableTheme: theme,
236- themeName: theme,
237- useTerminalDefaults: false,
238- };
271+ if (lower.length === 9) {
272+ return lower.slice(0, 7);
239273 }
274+ return lower;
275+}
240276
241- const serialized = JSON.stringify(theme);
242- const cacheHash = hashString(serialized);
243- const themeName =
244- typeof theme.name === "string" && theme.name.length > 0
245- ? theme.name
246- : `cel-syntax-${cacheHash}`;
247- const loadableTheme =
248- themeName === theme.name ? theme : { ...theme, name: themeName };
277+function parseHex(color: string): readonly [number, number, number] {
278+ const cached = rgbCache.get(color);
279+ if (cached) {
280+ return cached;
281+ }
249282
250- return {
251- cacheKey: `custom:${cacheHash}`,
252- loadableTheme,
253- themeName,
254- useTerminalDefaults: false,
255- };
283+ const normalized = normalizeHex(color);
284+ const rgb: readonly [number, number, number] = [
285+ Number.parseInt(normalized.slice(1, 3), 16),
286+ Number.parseInt(normalized.slice(3, 5), 16),
287+ Number.parseInt(normalized.slice(5, 7), 16),
288+ ];
289+ rgbCache.set(color, rgb);
290+ return rgb;
256291 }
257292
258-function stateKey(
259- language: string,
260- theme: ResolvedSyntaxHighlightTheme,
261-): string {
262- return `${language}\u0000${theme.cacheKey}`;
293+function colorDistance(
294+ a: readonly [number, number, number],
295+ b: readonly [number, number, number],
296+): number {
297+ const dr = a[0] - b[0];
298+ const dg = a[1] - b[1];
299+ const db = a[2] - b[2];
300+ return dr * dr + dg * dg + db * db;
263301 }
264302
265-function getRuntimePromise(): Promise<SyntaxHighlightRuntime> {
266- if (runtimeError) {
267- return Promise.reject(runtimeError);
303+function colorToSlot(color: string | undefined): Color | undefined {
304+ if (!color) {
305+ return undefined;
268306 }
269307
270- if (!runtimePromise) {
271- runtimePromise = Promise.all([import("shiki"), import("shiki-stream")])
272- .then(([shiki, shikiStream]) => {
273- runtimeInstance = {
274- bundledLanguages: shiki.bundledLanguages as Record<string, unknown>,
275- createHighlighter: shiki.createHighlighter,
276- ShikiStreamTokenizer: shikiStream.ShikiStreamTokenizer,
277- };
278- return runtimeInstance;
279- })
280- .catch((error) => {
281- runtimeError = normalizeError(error);
282- throw runtimeError;
283- })
284- .finally(() => {
285- cel.render();
286- });
308+ const normalized = normalizeHex(color);
309+ const cached = colorSlotCache.get(normalized);
310+ if (cached) {
311+ return cached;
287312 }
288313
289- return runtimePromise;
290-}
314+ const rgb = parseHex(normalized);
315+ let best = ANSI_SLOT_HEX[0]![0];
316+ let bestDistance = Number.POSITIVE_INFINITY;
291317
292-function getHighlighterPromise(): Promise<Highlighter> {
293- if (highlighterError) {
294- return Promise.reject(highlighterError);
318+ for (const [slot, hex] of ANSI_SLOT_HEX) {
319+ const distance = colorDistance(rgb, parseHex(hex));
320+ if (distance < bestDistance) {
321+ best = slot;
322+ bestDistance = distance;
323+ }
295324 }
296325
297- if (!highlighterPromise) {
298- highlighterPromise = getRuntimePromise()
299- .then((runtime) =>
300- runtime.createHighlighter({
301- themes: [DEFAULT_THEME],
302- langs: [],
303- }),
304- )
305- .then((highlighter) => {
306- highlighterInstance = highlighter;
307- loadedThemes.add(DEFAULT_RESOLVED_THEME.cacheKey);
308- return highlighter;
309- })
310- .catch((error) => {
311- highlighterError = normalizeError(error);
312- throw highlighterError;
313- })
314- .finally(() => {
315- cel.render();
316- });
317- }
326+ colorSlotCache.set(normalized, best);
327+ return best;
328+}
318329
319- return highlighterPromise;
330+function mergeStyles(base: StyleProps, overrides: StyleProps): StyleProps {
331+ return {
332+ fgColor: overrides.fgColor ?? base.fgColor,
333+ bgColor: overrides.bgColor ?? base.bgColor,
334+ bold: overrides.bold ?? base.bold,
335+ italic: overrides.italic ?? base.italic,
336+ underline: overrides.underline ?? base.underline,
337+ };
320338 }
321339
322-function hasBundledLanguage(
Cut short: this commit's diff is over 1000 lines.