StreamingSyntaxHighlightedCode

fun StreamingSyntaxHighlightedCode(code: String, language: String, modifier: Modifier = Modifier, theme: HighlightTheme = LocalHighlightTheme.current, style: CodeBlockStyle = CodeBlockStyle.Default, showLineNumbers: Boolean = false, debounceMs: Long = StreamingSyntaxHighlightedCodeDefaults.DEBOUNCE_MS, triggerOnNewline: Boolean = true, minThrottleMs: Long = StreamingSyntaxHighlightedCodeDefaults.MIN_THROTTLE_MS, scrollState: ScrollState = rememberScrollState(), languageLabel: @Composable () -> Unit? = if (language.isNotBlank()) DefaultLanguageLabelSentinel else null, copyButton: @Composable (onClick: () -> Unit) -> Unit? = DefaultCopyButtonSentinel, onCopyClick: (String) -> Unit? = null, onHighlightComplete: (HighlightResult) -> Unit? = null, onError: (HighlightException) -> Unit? = null)

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 AnimatedContent cross-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 0 on every token.

Usage

HighlightThemeProvider(
lightHighlightTheme = rememberTomorrowLightTheme(),
darkHighlightTheme = rememberAtomOneDarkTheme(),
) {
StreamingSyntaxHighlightedCode(
code = streamingLlmResponse,
language = "kotlin",
showLineNumbers = true,
)
}

Parameters

code

The source code to display (actively growing or static).

language

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

modifier

Modifier for the outer container. Applies testTag("streaming-syntax-highlighted-code") to the outer surface.

theme

The theme to use. Defaults to LocalHighlightTheme.

style

Visual style configuration - shape, padding, line numbers, typography, etc.

showLineNumbers

Whether to show a line-number gutter on the left.

debounceMs

Delay in milliseconds to debounce highlight engine calls after token arrival. 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).

scrollState

Hoisted scroll state for horizontal scrolling.

languageLabel

Optional composable for the language badge in the header. null hides it.

copyButton

Optional composable for the copy button in the header. null hides it.

onCopyClick

Optional custom copy callback. When null, copies to the system clipboard.

onHighlightComplete

Optional callback invoked with HighlightResult when highlighting succeeds.

onError

Optional callback invoked with HighlightException when highlighting fails.