Streaming Syntax Highlighted Code
Displays streaming, syntax-highlighted code in a styled block tailored for real-time token generation (e.g. LLM responses, terminal logs, and live code generators).
This composable is marked experimental (ExperimentalHighlightApi). Call sites must opt in with @OptIn(ExperimentalHighlightApi::class) or propagate the annotation.
Differences from SyntaxHighlightedCode
0 ms Streaming Latency: As incoming text grows, new characters render immediately while spans from previous lines are preserved via span-transfer (rememberStreamingHighlightedCode).
No Animation Thrash: Updates spans in-place without
AnimatedContentcross-fading, avoiding visual ghosting during active token generation.Streaming-Aware Scroll: Horizontal scroll position is preserved while new tokens are appended to the stream, rather than resetting to
0on every token.
Usage
HighlightThemeProvider(
lightHighlightTheme = rememberTomorrowLightTheme(),
darkHighlightTheme = rememberAtomOneDarkTheme(),
) {
StreamingSyntaxHighlightedCode(
code = streamingLlmResponse,
language = "kotlin",
showLineNumbers = true,
)
}Parameters
The source code to display (actively growing or static).
Highlight.js language identifier (e.g. "python", "kotlin", "json").
Modifier for the outer container. Applies testTag("streaming-syntax-highlighted-code") to the outer surface.
The theme to use. Defaults to LocalHighlightTheme.
Visual style configuration - shape, padding, line numbers, typography, etc.
Whether to show a line-number gutter on the left.
Delay in milliseconds to debounce highlight engine calls after token arrival. Defaults to StreamingSyntaxHighlightedCodeDefaults.DEBOUNCE_MS (200 ms).
Whether to trigger a background highlight run when a new newline (\n) is detected in the stream, progressively styling completed lines. Defaults to true.
Minimum interval in milliseconds between consecutive newline-triggered highlight runs to avoid engine overload. Defaults to StreamingSyntaxHighlightedCodeDefaults.MIN_THROTTLE_MS (150 ms).
Hoisted scroll state for horizontal scrolling.
Optional composable for the language badge in the header. null hides it.
Optional composable for the copy button in the header. null hides it.
Optional custom copy callback. When null, copies to the system clipboard.
Optional callback invoked with HighlightResult when highlighting succeeds.
Optional callback invoked with HighlightException when highlighting fails.