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. Ignored when header is customized.
Optional composable slot for trailing header actions (copy, edit, share, etc.). null hides all trailing actions. Runs with a RowScope receiver and receives an onCopy action pre-wired to copy code to the system clipboard (or call onCopyClick if provided). The default renders SyntaxHighlightedCodeDefaults.CopyButton. Ignored when header is customized.
Optional coarse-grained slot that replaces the entire header row. null renders no header chrome at all. Receives an onCopy action pre-wired to copy code to the system clipboard (or call onCopyClick if provided). When customized, languageLabel and actions are ignored. The default delegates to SyntaxHighlightedCodeDefaults.Header.
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.