xonecas.io
70160
Ce

cel-tui / commit c28d0ef

fix(components): rehighlight syntax on content changes

xonecas2026-04-12 c28d0efd0eab3e49bf767cfe7201c82a54abda11 parent 090dd66

5 files changed

docs/skill/cel-tui/SKILL.md+2−2
@@ -260,7 +260,7 @@ VStack({ flex: 1, overflow: "scroll", padding: { x: 1 } }, [
260260 SyntaxHighlight(source, "javascript", { theme: "dark-plus" });
261261 ```
262262
263-`SyntaxHighlight(content, language, props?)` renders registered lextide languages and aliases into cel-tui primitives. The component stays synchronous to call, but append-only updates reuse lextide's streaming parser state. Unknown language ids render plain text. The optional `theme` accepts the small built-in presets (`"default"`, `"dark-plus"`) or a best-effort token-color registration object.
263+`SyntaxHighlight(content, language, props?)` renders registered lextide languages and aliases into cel-tui primitives. The component stays synchronous to call and re-highlights the full snippet whenever content changes, so final output stays stable across streamed chunk boundaries. Unknown language ids render plain text. The optional `theme` accepts the small built-in presets (`"default"`, `"dark-plus"`) or a best-effort token-color registration object.
264264
265265 ## Gotchas
266266
@@ -280,7 +280,7 @@ SyntaxHighlight(source, "javascript", { theme: "dark-plus" });
280280 - **Kitty-first keyboard input** — the framework enables Kitty level 1 and gets full modifier fidelity when the host preserves it (`alt+x`, `ctrl+plus`, `shift+enter`, etc.). It also normalizes recoverable legacy encodings so common shortcuts keep working in tmux. For best results, use a Kitty-compatible terminal or `tmux` with `set -s extended-keys on`. On older legacy hosts, historically ambiguous collisions such as `ctrl+i` vs `tab` or `ctrl+m` vs `enter` cannot be recovered once the host c …
281281 - **tmux is good for keyboard-driven manual checks** — common `tmux send-keys` paths work for printable chars, `Tab`/`BTab`, `Enter`, `Escape`, arrows, and many `Ctrl+letter` shortcuts. Use exact raw-sequence injection only when you need to target a protocol-specific encoding. Mouse input remains unreliable in tmux and should be verified in a real terminal.
282282 - **Button limitations** — `Button` from `@cel-tui/components` does not forward container sizing props (`width`, `height`, `flex`, `minWidth`, etc.). It supports styling (`fgColor`, `bgColor`, `bold`, etc.), `focusStyle`, `focused`, `onFocus`, `onBlur`, `onKeyPress`, and `padding`. For full layout control, use `HStack` + `Text` directly.
283-- **SyntaxHighlight keeps streaming parser state only for append-only growth** — appended content reuses lextide's stream state, but non-append edits still reset and replay the full snippet. Unknown language ids render plain text.
283+- **SyntaxHighlight re-highlights on content changes** — the component favors deterministic final output over incremental parser reuse, so streamed chunk boundaries do not change the final highlighting. Unknown language ids render plain text.
284284
285285 ## Composing Components
286286
docs/skill/cel-tui/references/api.md+1−1
@@ -312,7 +312,7 @@ SyntaxHighlight(code, "javascript", { theme: "dark-plus" });
312312 - `props.theme` accepts the built-in presets (`"default"`, `"dark-plus"`) or a best-effort token theme registration object
313313 - Uses a terminal-friendly ANSI 16 fallback theme by default
314314 - Highlighting is synchronous at the component boundary; unsupported languages render plain text
315-- Append-only updates reuse lextide stream state, while non-append edits reset and replay the full snippet
315+- Content changes re-highlight the full snippet so final output stays stable across streamed chunk boundaries
316316
317317 ## Theme
318318
examples/syntax-highlight.ts+12−8
@@ -2,8 +2,9 @@
22 * cel-tui: SyntaxHighlight streaming demo
33 *
44 * Streams every built-in lextide language through SyntaxHighlight.
5- * The large pane renders SyntaxHighlight, while the stream board mirrors
6- * lextide's recall/stable/unstable updates for the same append-only chunks.
5+ * The large pane renders SyntaxHighlight from the growing source text, while
6+ * the stream board mirrors lextide's recall/stable/unstable updates for the
7+ * same append-only chunks.
78 *
89 * Run: bun run examples/syntax-highlight.ts
910 */
@@ -142,7 +143,7 @@ const SAMPLE_BY_LANGUAGE = {
142143 source: [
143144 "# Streaming markdown",
144145 "",
145- "`SyntaxHighlight` keeps append-only updates cheap.",
146+ "`SyntaxHighlight` keeps final output stable across chunks.",
146147 "",
147148 "- headings",
148149 "- emphasis",
@@ -700,10 +701,13 @@ cel.viewport(() => {
700701 ]),
701702 Divider({ fgColor: "color08" }),
702703 VStack({ padding: { x: 1 } }, [
703- Text("All registered lextide built-ins stream in append-only chunks.", {
704- fgColor: "color08",
705- italic: true,
706- }),
704+ Text(
705+ "All registered lextide built-ins feed SyntaxHighlight append-only chunks.",
706+ {
707+ fgColor: "color08",
708+ italic: true,
709+ },
710+ ),
707711 Text(
708712 "The large pane renders SyntaxHighlight(); the board shows the matching lowlight.stream(...) deltas.",
709713 {
@@ -735,7 +739,7 @@ cel.viewport(() => {
735739 Text("quit", { fgColor: "color08" }),
736740 ]),
737741 Text(
738- "Append-only only: restarting rebuilds every stream from empty; non-append edits would reset the streaming parser state.",
742+ "Restarting rebuilds each sample from empty so you can compare the same chunk sequence again.",
739743 {
740744 fgColor: "color08",
741745 },
packages/components/src/syntax-highlight.test.ts+45−0
@@ -58,6 +58,21 @@ function findTextContaining(node: ContainerNode, content: string): TextNode {
5858 return text;
5959 }
6060
61+function lineTokens(line: Node) {
62+ const container = asContainer(line);
63+ return container.children.map((child) => {
64+ const text = asText(child);
65+ return {
66+ content: text.content,
67+ fgColor: text.props.fgColor,
68+ bgColor: text.props.bgColor,
69+ bold: text.props.bold,
70+ italic: text.props.italic,
71+ underline: text.props.underline,
72+ };
73+ });
74+}
75+
6176 function hasStyledText(node: ContainerNode): boolean {
6277 return node.children.some((line) => {
6378 const container = asContainer(line);
@@ -213,6 +228,36 @@ describe("SyntaxHighlight", () => {
213228 expect(lineText(item(node.children, 0))).toBe('const message = "hello"');
214229 });
215230
231+ test("produces the same final highlight regardless of append chunk boundaries", () => {
232+ const firstChunk = "t";
233+ const secondChunk = "ype Foo<T extends string | number> = { value: T };\n";
234+ const finalContent = `${firstChunk}${secondChunk}const x: Foo<string> = { value: \"a\" };\n`;
235+ const streamedTheme = {
236+ name: "syntax-highlight-streamed-boundary-regression",
237+ tokenColors: [],
238+ };
239+ const directTheme = {
240+ name: "syntax-highlight-direct-boundary-regression",
241+ tokenColors: [],
242+ };
243+
244+ render(firstChunk, "typescript", { theme: streamedTheme });
245+ render(`${firstChunk}${secondChunk}`, "typescript", {
246+ theme: streamedTheme,
247+ });
248+ const streamed = render(finalContent, "typescript", {
249+ theme: streamedTheme,
250+ });
251+ const direct = render(finalContent, "typescript", { theme: directTheme });
252+
253+ expect(lineTokens(item(streamed.children, 0))).toEqual(
254+ lineTokens(item(direct.children, 0)),
255+ );
256+ expect(lineTokens(item(streamed.children, 1))).toEqual(
257+ lineTokens(item(direct.children, 1)),
258+ );
259+ });
260+
216261 test("preserves highlighting across appended multiline comment", () => {
217262 render("/* hello", "javascript");
218263 const node = render("/* hello\nworld */", "javascript");
packages/components/src/syntax-highlight.ts+6−60
@@ -5,8 +5,6 @@ import {
55 createLowlight,
66 type Element,
77 type ElementContent,
8- type HighlightStream,
9- type StreamUpdate,
108 } from "lextide";
119
1210 const lowlight = createLowlight(all);
@@ -99,7 +97,6 @@ interface SyntaxHighlightState {
9997 children: HighlightNode[];
10098 lastContent: string;
10199 lastNode: ContainerNode | null;
102- stream: HighlightStream;
103100 theme: ResolvedSyntaxHighlightTheme;
104101 }
105102
@@ -447,62 +444,20 @@ function findState(
447444 }
448445 }
449446
450- let best: SyntaxHighlightState | undefined;
451- let bestLength = -1;
452-
453- for (let i = states.length - 1; i >= 0; i--) {
454- const state = requiredAt(states, i, "syntax highlight state");
455- if (!content.startsWith(state.lastContent)) {
456- continue;
457- }
458- if (state.lastContent.length > bestLength) {
459- best = state;
460- bestLength = state.lastContent.length;
461- }
462- }
463-
464- return best;
465-}
466-
467-function createStream(language: string): HighlightStream {
468- return lowlight.stream(language, { allowRecalls: true });
447+ return undefined;
469448 }
470449
471450 function createState(
472- language: string,
473451 theme: ResolvedSyntaxHighlightTheme,
474452 ): SyntaxHighlightState {
475453 return {
476454 children: [],
477455 lastContent: "",
478456 lastNode: null,
479- stream: createStream(language),
480457 theme,
481458 };
482459 }
483460
484-function applyStreamUpdate(
485- children: HighlightNode[],
486- update: StreamUpdate,
487-): void {
488- const nextLength = Math.max(0, children.length - update.recall);
489- children.length = nextLength;
490- children.push(...update.stable, ...update.unstable);
491-}
492-
493-function resetState(
494- state: SyntaxHighlightState,
495- language: string,
496- content: string,
497-): void {
498- state.children = [];
499- state.stream = createStream(language);
500-
501- if (content.length > 0) {
502- applyStreamUpdate(state.children, state.stream.write(content));
503- }
504-}
505-
506461 function pushRun(
507462 line: HighlightRun[],
508463 content: string,
@@ -604,15 +559,7 @@ function updateState(
604559 return;
605560 }
606561
607- if (!content.startsWith(state.lastContent)) {
608- resetState(state, language, content);
609- } else {
610- const chunk = content.slice(state.lastContent.length);
611- if (chunk.length > 0) {
612- applyStreamUpdate(state.children, state.stream.write(chunk));
613- }
614- }
615-
562+ state.children = lowlight.highlight(language, content).children;
616563 state.lastContent = content;
617564 state.lastNode = null;
618565 }
@@ -658,10 +605,9 @@ function renderHighlightedState(state: SyntaxHighlightState): ContainerNode {
658605 /**
659606 * Render syntax-highlighted code as cel-tui primitives.
660607 *
661- * Uses lextide synchronously at the component boundary while keeping a
662- * streaming parser state per language/theme cache entry. Append-only updates
663- * apply lextide's recall/stable/unstable deltas, while non-append edits reset
664- * the stream and replay the full snippet.
608+ * Re-highlights the full snippet whenever the content changes so the final
609+ * output stays deterministic regardless of how streamed chunks were split.
610+ * Repeated renders with identical content reuse the cached rendered tree.
665611 *
666612 * Unknown languages render as plain text. The default theme is terminal-friendly
667613 * and maps lextide's emitted highlight.js classes onto cel's ANSI palette slots
@@ -684,7 +630,7 @@ export function SyntaxHighlight(
684630 }
685631
686632 const key = stateKey(language, theme);
687- const state = findState(key, content) ?? createState(language, theme);
633+ const state = findState(key, content) ?? createState(theme);
688634 updateState(state, language, content);
689635 touchState(key, state);
690636 return renderHighlightedState(state);