rememberStreamingHighlightedCode

fun rememberStreamingHighlightedCode(code: String, language: String, theme: HighlightTheme = LocalHighlightTheme.current, debounceMs: Long = StreamingSyntaxHighlightedCodeDefaults.DEBOUNCE_MS, triggerOnNewline: Boolean = true, minThrottleMs: Long = StreamingSyntaxHighlightedCodeDefaults.MIN_THROTTLE_MS, onHighlightComplete: (HighlightResult) -> Unit? = null, onError: (HighlightException) -> Unit? = null): AnnotatedString

Runs the debounce + syntax-highlight pipeline for streaming / real-time code (such as LLM responses or live terminal logs) and returns an AnnotatedString ready for rendering.

This composable is marked experimental (ExperimentalHighlightApi). Call sites must opt in with @OptIn(ExperimentalHighlightApi::class) or propagate the annotation.

Unlike rememberHighlightedCode, which cancels and resets to null on every string change, this function preserves syntax highlighting during active streaming using span transfer (applySnapshotSpans):

  1. Immediate render (0 ms latency): As new tokens arrive, the full string is returned immediately.

  2. Continuous styling: Spans from the last completed highlight run are carried forward onto the unchanged prefix of the new text, keeping existing lines colored.

  3. Newline-aware progressive backfilling: When triggerOnNewline is enabled, completed lines are progressively highlighted in the background as newlines (\n) arrive (throttled by minThrottleMs), snapping finished lines to full syntax colors while subsequent tokens continue streaming.

  4. Debounced engine calls: Idle pauses are debounced by debounceMs, coalescing rapid token emissions into a single highlight engine run when generation pauses or finishes.

Usage

HighlightThemeProvider(
lightHighlightTheme = rememberTomorrowLightTheme(),
darkHighlightTheme = rememberAtomOneDarkTheme(),
) {
val highlightedCode = rememberStreamingHighlightedCode(
code = streamingLlmText,
language = "kotlin",
)
Text(text = highlightedCode)
}

Return

An AnnotatedString with syntax highlighting applied and previous spans preserved across stream updates.

Parameters

code

The current source code string (actively growing or static).

language

Highlight.js language identifier (e.g. "kotlin", "python", "json").

theme

The highlight theme to apply. Defaults to LocalHighlightTheme.

debounceMs

Milliseconds to wait after the last text change before triggering an idle highlight call. Defaults to StreamingSyntaxHighlightedCodeDefaults.DEBOUNCE_MS (200 ms).

triggerOnNewline

Whether to trigger a background highlight run when a new newline (\n) is detected in the stream, progressively styling completed lines. Defaults to true.

minThrottleMs

Minimum interval in milliseconds between consecutive newline-triggered highlight runs to avoid engine overload. Defaults to StreamingSyntaxHighlightedCodeDefaults.MIN_THROTTLE_MS (150 ms).

onHighlightComplete

Optional callback invoked with a HighlightResult when a highlight cycle completes successfully. Fires after the snapshot is updated.

onError

Optional callback invoked with the HighlightException when highlighting fails. Previously highlighted spans are preserved on failure (no flash to plain text mid-stream) - this callback is purely observational.