@@ -1,11 +1,17 @@
11 import type { Color, ContainerNode, Node, StyleProps } from "@cel-tui/types";
22 import { HStack, Text, VStack } from "@cel-tui/core";
3-import { all, createLowlight } from "lowlight";
3+import {
4+ all,
5+ createLowlight,
6+ type Element,
7+ type ElementContent,
8+ type HighlightStream,
9+ type StreamUpdate,
10+} from "lextide";
411
512 const lowlight = createLowlight(all);
613
714 const DEFAULT_THEME_NAME = "cel-ansi16";
8-const APPEND_REHIGHLIGHT_WINDOW = 8_192;
915 const MAX_STATES_PER_KEY = 4;
1016
1117 const ANSI_SLOT_HEX: ReadonlyArray<readonly [Color, string]> = [
@@ -88,7 +94,7 @@ export interface SyntaxHighlightProps {
8894 *
8995 * Built-in presets currently include `"default"` and `"dark-plus"`.
9096 * Theme registration objects are applied as best-effort overrides onto the
91- * lowlight/highlight.js token categories used by this component.
97+ * lextide token categories used by this component.
9298 */
9399 theme?: SyntaxHighlightTheme;
94100 }
@@ -110,26 +116,15 @@ interface ResolvedSyntaxHighlightTheme {
110116 }
111117
112118 interface SyntaxHighlightState {
119+ children: HighlightNode[];
113120 lastContent: string;
114121 lastNode: ContainerNode | null;
115- lines: HighlightRun[][];
122+ stream: HighlightStream;
116123 theme: ResolvedSyntaxHighlightTheme;
117124 }
118125
119-interface HighlightTextNode {
120- type: "text";
121- value: string;
122-}
123-
124-interface HighlightElementNode {
125- type: "element";
126- children: HighlightNode[];
127- properties?: {
128- className?: string | readonly string[];
129- };
130-}
131-
132-type HighlightNode = HighlightElementNode | HighlightTextNode;
126+type HighlightElementNode = Element;
127+type HighlightNode = ElementContent;
133128
134129 const rgbCache = new Map<string, readonly [number, number, number]>();
135130 const colorSlotCache = new Map<string, Color>();
@@ -518,34 +513,43 @@ function findState(
518513 return best;
519514 }
520515
516+function createStream(language: string): HighlightStream {
517+ return lowlight.stream(language, { allowRecalls: true });
518+}
519+
521520 function createState(
521+ language: string,
522522 theme: ResolvedSyntaxHighlightTheme,
523523 ): SyntaxHighlightState {
524524 return {
525+ children: [],
525526 lastContent: "",
526527 lastNode: null,
527- lines: [[]],
528+ stream: createStream(language),
528529 theme,
529530 };
530531 }
531532
532-function countNewlines(input: string): number {
533- let count = 0;
534- for (let i = 0; i < input.length; i++) {
535- if (input.charCodeAt(i) === 10) {
536- count++;
537- }
538- }
539- return count;
533+function applyStreamUpdate(
534+ children: HighlightNode[],
535+ update: StreamUpdate,
536+): void {
537+ const nextLength = Math.max(0, children.length - update.recall);
538+ children.length = nextLength;
539+ children.push(...update.stable, ...update.unstable);
540540 }
541541
542-function findAppendWindowStart(
542+function resetState(
543+ state: SyntaxHighlightState,
544+ language: string,
543545 content: string,
544- previousLength: number,
545-): number {
546- const target = Math.max(0, previousLength - APPEND_REHIGHLIGHT_WINDOW);
547- const newline = content.lastIndexOf("\n", target);
548- return newline === -1 ? 0 : newline + 1;
546+): void {
547+ state.children = [];
548+ state.stream = createStream(language);
549+
550+ if (content.length > 0) {
551+ applyStreamUpdate(state.children, state.stream.write(content));
552+ }
549553 }
550554
551555 function pushRun(
@@ -567,11 +571,7 @@ function pushRun(
567571 }
568572
569573 function getClassNames(node: HighlightElementNode): readonly string[] {
570- const className = node.properties?.className;
571- if (Array.isArray(className)) {
572- return className;
573- }
574- return typeof className === "string" ? [className] : [];
574+ return node.properties.className;
575575 }
576576
577577 function resolveNodeStyle(
@@ -630,21 +630,13 @@ function pushHighlightNode(
630630 }
631631 }
632632
633-function highlightContentToLines(
634- content: string,
635- language: string,
633+function highlightChildrenToLines(
634+ children: readonly HighlightNode[],
636635 theme: ResolvedSyntaxHighlightTheme,
637636 ): HighlightRun[][] {
638- if (content.length === 0) {
639- return [[]];
640- }
641-
642- const root = lowlight.highlight(language, content) as {
643- children: HighlightNode[];
644- };
645637 const lines: HighlightRun[][] = [[]];
646638
647- for (const child of root.children) {
639+ for (const child of children) {
648640 pushHighlightNode(lines, child, theme.baseStyle, theme);
649641 }
650642
@@ -661,25 +653,14 @@ function updateState(
661653 }
662654
663655 if (!content.startsWith(state.lastContent)) {
664- state.lines = highlightContentToLines(content, language, state.theme);
665- state.lastContent = content;
666- state.lastNode = null;
667- return;
656+ resetState(state, language, content);
657+ } else {
658+ const chunk = content.slice(state.lastContent.length);
659+ if (chunk.length > 0) {
660+ applyStreamUpdate(state.children, state.stream.write(chunk));
661+ }
668662 }
669663
670- const windowStart = findAppendWindowStart(content, state.lastContent.length);
671- const prefixLineCount = Math.min(
672- countNewlines(content.slice(0, windowStart)),
673- state.lines.length,
674- );
675- const suffixLines = highlightContentToLines(
676- content.slice(windowStart),
677- language,
678- state.theme,
679- );
680-
681- state.lines.length = prefixLineCount;
682- state.lines.push(...suffixLines);
683664 state.lastContent = content;
684665 state.lastNode = null;
685666 }
@@ -717,24 +698,25 @@ function renderHighlightedState(state: SyntaxHighlightState): ContainerNode {
717698 return state.lastNode;
718699 }
719700
720- state.lastNode = VStack({}, state.lines.map(lineToNode));
701+ const lines = highlightChildrenToLines(state.children, state.theme);
702+ state.lastNode = VStack({}, lines.map(lineToNode));
721703 return state.lastNode;
722704 }
723705
724706 /**
725707 * Render syntax-highlighted code as cel-tui primitives.
726708 *
727- * Uses lowlight/highlight.js synchronously with a small append-only cache. When
728- * content grows by appending new text, only a suffix window near the end is
729- * re-highlighted and merged back into the cached lines. Non-append edits fall
730- * back to a full re-highlight of the snippet.
709+ * Uses lextide synchronously at the component boundary while keeping a
710+ * streaming parser state per language/theme cache entry. Append-only updates
711+ * apply lextide's recall/stable/unstable deltas, while non-append edits reset
712+ * the stream and replay the full snippet.
731713 *
732714 * Unknown languages render as plain text. The default theme is terminal-friendly
733- * and maps lowlight token classes onto cel's ANSI palette slots while leaving
715+ * and maps lextide token classes onto cel's ANSI palette slots while leaving
734716 * base text on terminal defaults.
735717 *
736718 * @param content - Source code to render.
737- * @param language - Registered highlight.js language id or alias.
719+ * @param language - Registered lextide language id or alias.
738720 * @param props - Optional theme override.
739721 * @returns A `VStack` containing one highlighted line per child.
740722 */
@@ -750,7 +732,7 @@ export function SyntaxHighlight(
750732 }
751733
752734 const key = stateKey(language, theme);
753- const state = findState(key, content) ?? createState(theme);
735+ const state = findState(key, content) ?? createState(language, theme);
754736 updateState(state, language, content);
755737 touchState(key, state);
756738 return renderHighlightedState(state);