
High-performance code highlighting with AST-based incremental parsing, 28+ languages, rich themes, streaming cursor animation, inline-code measurement, collapsible blocks, configurable line numbers, copy button and custom lexers.
A high-performance cross-platform code highlighting library developed based on Kotlin Multiplatform (KMP) and Compose Multiplatform. It supports consistent rendering effects on Android, iOS, Desktop (JVM), and Web (Wasm/JS) platforms.
<?php tags, keywords, strings, comments, $variable tokens, and ->/:: operatorssetState
case class, trait, and object
local variables, and require/print
library, data.frame, and common data analysis built-ins| Theme | Type | Description |
|---|---|---|
| OneDarkPro | Dark | Based on Atom's popular One Dark Pro theme |
| GithubLight | Light | Based on GitHub's code highlighting colors |
| DraculaPro | Dark | Based on the Dracula Pro color scheme |
| SolarizedLight | Light | Based on Ethan Schoonover's Solarized Light |
Add to your gradle/libs.versions.toml:
[versions]
codehigh = "2.0.0"
[libraries]
codehigh-render = { module = "io.github.zusrsoft:codehighlight-render", version.ref = "codehigh" }
codehigh-parser = { module = "io.github.zusrsoft:codehighlight-parser", version.ref = "codehigh" }Then add it in your module's build.gradle.kts:
dependencies {
implementation(libs.codehigh.render)
implementation(libs.codehigh.parser)
}If you do not use Version Catalog, you can add the dependency directly:
dependencies {
implementation("io.github.zusrsoft:codehighlight-render:2.0.0")
implementation("io.github.zusrsoft:codehighlight-parser:2.0.0")
}Use codehighlight-parser directly when you only need tokenization, language registration, or incremental parsing without Compose rendering.
In a Compose Multiplatform project, you can use the CodeBlock component directly:
import com.hrm.codehigh.renderer.CodeBlock
import com.hrm.codehigh.theme.OneDarkProTheme
@Composable
fun MyScreen() {
CodeBlock(
code = """
fun main() {
println("Hello, CodeHigh!")
}
""".trimIndent(),
language = "kotlin",
theme = OneDarkProTheme,
showLineNumbers = true,
showCopyButton = true
)
}For inline code within text, the default style follows a single InlineCodeStyle contract: light themes use a #F6F8FA background with a #D0D7DE 1dp border, dark themes use a #30363D background with a #3D444D 1dp border, and text keeps the theme plain color. The border is part of the component style contract, so rendering and measurement stay aligned as long as you reuse the same style.
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.runtime.remember
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.hrm.codehigh.renderer.InlineCode
import com.hrm.codehigh.renderer.InlineCodeDefaults
@Composable
fun MyText() {
val baseStyle = InlineCodeDefaults.style()
val customInlineCodeStyle = remember(baseStyle) {
baseStyle.copy(
textStyle = baseStyle.textStyle.copy(fontSize = 14.sp),
contentPadding = PaddingValues(horizontal = 8.dp, vertical = 3.dp),
)
}
InlineCode(text = "README.md")
InlineCode(text = "notes", style = customInlineCodeStyle)
}When you need to pre-occupy space or adjust layout, use the measurement API with the exact same InlineCodeStyle that you pass to InlineCode. The measured size already includes the style's padding and border, so you can reuse it directly for placeholders or constrained layout slots. measureInlineCodeSize returns an InlineCodeSize holding raw widthPx/heightPx values; convert them to Dp with size.width(density) / size.height(density):
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.runtime.remember
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.text.rememberTextMeasurer
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.hrm.codehigh.renderer.InlineCodeDefaults
import com.hrm.codehigh.renderer.measureInlineCodeSize
import com.hrm.codehigh.theme.OneDarkProTheme
@Composable
fun MeasureExample() {
val density = LocalDensity.current
val textMeasurer = rememberTextMeasurer()
val inlineCodeStyle = remember {
val baseStyle = InlineCodeDefaults.style(OneDarkProTheme)
baseStyle.copy(
textStyle = baseStyle.textStyle.copy(fontSize = 14.sp),
contentPadding = PaddingValues(horizontal = 8.dp, vertical = 3.dp),
)
}
val size = remember(inlineCodeStyle) {
measureInlineCodeSize(
text = "README.md",
language = "kotlin",
style = inlineCodeStyle,
density = density,
textMeasurer = textMeasurer,
)
}
Box(
Modifier
.width(size.width(density))
.height(size.height(density))
)
}Avoid wrapping InlineCode in an extra outer border unless you also account for that added decoration in your own layout. The built-in measurement API only guarantees consistency for the style contract declared in InlineCodeStyle.
For streaming scenarios (e.g., AI chatbots), enable the isStreaming flag to show a blinking cursor animation at the end:
import com.hrm.codehigh.renderer.CodeBlock
@Composable
fun StreamingCode() {
var code by remember { mutableStateOf("") }
CodeBlock(
code = code,
language = "python",
isStreaming = true // Shows blinking cursor at the end
)
}For long code blocks, use maxVisibleLines to make them collapsible (defaults to 500 lines; pass null for no limit):
CodeBlock(
code = longCode,
language = "java",
maxVisibleLines = 20 // Collapse after 20 lines
)You can create custom themes by implementing the CodeTheme interface:
import androidx.compose.ui.graphics.Color
import com.hrm.codehigh.ast.TokenType
import com.hrm.codehigh.theme.CodeTheme
object MyCustomTheme : CodeTheme {
override val background = Color(0xFF1E1E1E)
override val isDark = true
override fun colorFor(type: TokenType): Color {
return when (type) {
TokenType.KEYWORD -> Color(0xFF569CD6)
TokenType.STRING -> Color(0xFFCE9178)
TokenType.COMMENT -> Color(0xFF6A9955)
TokenType.NUMBER -> Color(0xFFB5CEA8)
// ... other token types
else -> Color(0xFFD4D4D4)
}
}
}Use CompositionLocal to provide a theme globally:
import com.hrm.codehigh.theme.LocalCodeTheme
import com.hrm.codehigh.theme.GithubLightTheme
@Composable
fun App() {
CompositionLocalProvider(LocalCodeTheme provides GithubLightTheme) {
// All CodeBlocks in this scope will use GithubLightTheme
CodeBlock(code = "...", language = "kotlin")
}
}You can register custom lexers for additional languages:
import com.hrm.codehigh.lexer.LanguageRegistry
import com.hrm.codehigh.lexer.Lexer
// Register your custom lexer
LanguageRegistry.register("my-language", MyCustomLexer)
LanguageRegistry.registerAlias("ml", "my-language") // Add alias
// Use it in CodeBlock
CodeBlock(
code = myCode,
language = "my-language" // Or "ml" via alias
):codehighlight-parser: Parser SDK module, containing tokens, lexers, language registration, and incremental parsing.:codehighlight-render: Compose render SDK module, containing renderer components, themes, and parser integration.:codehighlight-preview: Preview components and sample datasets.:composeApp: Cross-platform Demo application.:androidApp: Android Demo application.:iosApp: iOS application entry module../gradlew :androidApp:assembleDebug
./gradlew :composeApp:run
./gradlew :composeApp:wasmJsBrowserDevelopmentRun
iosApp/iosApp.xcworkspace in Xcode to run.# Library modules (JVM)
./gradlew :codehighlight-parser:jvmTest :codehighlight-render:jvmTest :codehighlight-preview:jvmTest
# Demo application (tests currently empty, reserved)
./gradlew :composeApp:jvmTestFor a detailed list of supported features, please refer to: HIGHLIGHTER_COVERAGE_ANALYSIS.md
CodeBlock internally renders lines in a regular Column rather than a LazyColumn. The default maxVisibleLines = 500 cap plus the collapse control bound the cost; full lazy rendering is left for a dedicated follow-up.codehigh.This project is licensed under the MIT License - see the LICENSE file for details.
A high-performance cross-platform code highlighting library developed based on Kotlin Multiplatform (KMP) and Compose Multiplatform. It supports consistent rendering effects on Android, iOS, Desktop (JVM), and Web (Wasm/JS) platforms.
<?php tags, keywords, strings, comments, $variable tokens, and ->/:: operatorssetState
case class, trait, and object
local variables, and require/print
library, data.frame, and common data analysis built-ins| Theme | Type | Description |
|---|---|---|
| OneDarkPro | Dark | Based on Atom's popular One Dark Pro theme |
| GithubLight | Light | Based on GitHub's code highlighting colors |
| DraculaPro | Dark | Based on the Dracula Pro color scheme |
| SolarizedLight | Light | Based on Ethan Schoonover's Solarized Light |
Add to your gradle/libs.versions.toml:
[versions]
codehigh = "2.0.0"
[libraries]
codehigh-render = { module = "io.github.zusrsoft:codehighlight-render", version.ref = "codehigh" }
codehigh-parser = { module = "io.github.zusrsoft:codehighlight-parser", version.ref = "codehigh" }Then add it in your module's build.gradle.kts:
dependencies {
implementation(libs.codehigh.render)
implementation(libs.codehigh.parser)
}If you do not use Version Catalog, you can add the dependency directly:
dependencies {
implementation("io.github.zusrsoft:codehighlight-render:2.0.0")
implementation("io.github.zusrsoft:codehighlight-parser:2.0.0")
}Use codehighlight-parser directly when you only need tokenization, language registration, or incremental parsing without Compose rendering.
In a Compose Multiplatform project, you can use the CodeBlock component directly:
import com.hrm.codehigh.renderer.CodeBlock
import com.hrm.codehigh.theme.OneDarkProTheme
@Composable
fun MyScreen() {
CodeBlock(
code = """
fun main() {
println("Hello, CodeHigh!")
}
""".trimIndent(),
language = "kotlin",
theme = OneDarkProTheme,
showLineNumbers = true,
showCopyButton = true
)
}For inline code within text, the default style follows a single InlineCodeStyle contract: light themes use a #F6F8FA background with a #D0D7DE 1dp border, dark themes use a #30363D background with a #3D444D 1dp border, and text keeps the theme plain color. The border is part of the component style contract, so rendering and measurement stay aligned as long as you reuse the same style.
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.runtime.remember
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.hrm.codehigh.renderer.InlineCode
import com.hrm.codehigh.renderer.InlineCodeDefaults
@Composable
fun MyText() {
val baseStyle = InlineCodeDefaults.style()
val customInlineCodeStyle = remember(baseStyle) {
baseStyle.copy(
textStyle = baseStyle.textStyle.copy(fontSize = 14.sp),
contentPadding = PaddingValues(horizontal = 8.dp, vertical = 3.dp),
)
}
InlineCode(text = "README.md")
InlineCode(text = "notes", style = customInlineCodeStyle)
}When you need to pre-occupy space or adjust layout, use the measurement API with the exact same InlineCodeStyle that you pass to InlineCode. The measured size already includes the style's padding and border, so you can reuse it directly for placeholders or constrained layout slots. measureInlineCodeSize returns an InlineCodeSize holding raw widthPx/heightPx values; convert them to Dp with size.width(density) / size.height(density):
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.runtime.remember
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.text.rememberTextMeasurer
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.hrm.codehigh.renderer.InlineCodeDefaults
import com.hrm.codehigh.renderer.measureInlineCodeSize
import com.hrm.codehigh.theme.OneDarkProTheme
@Composable
fun MeasureExample() {
val density = LocalDensity.current
val textMeasurer = rememberTextMeasurer()
val inlineCodeStyle = remember {
val baseStyle = InlineCodeDefaults.style(OneDarkProTheme)
baseStyle.copy(
textStyle = baseStyle.textStyle.copy(fontSize = 14.sp),
contentPadding = PaddingValues(horizontal = 8.dp, vertical = 3.dp),
)
}
val size = remember(inlineCodeStyle) {
measureInlineCodeSize(
text = "README.md",
language = "kotlin",
style = inlineCodeStyle,
density = density,
textMeasurer = textMeasurer,
)
}
Box(
Modifier
.width(size.width(density))
.height(size.height(density))
)
}Avoid wrapping InlineCode in an extra outer border unless you also account for that added decoration in your own layout. The built-in measurement API only guarantees consistency for the style contract declared in InlineCodeStyle.
For streaming scenarios (e.g., AI chatbots), enable the isStreaming flag to show a blinking cursor animation at the end:
import com.hrm.codehigh.renderer.CodeBlock
@Composable
fun StreamingCode() {
var code by remember { mutableStateOf("") }
CodeBlock(
code = code,
language = "python",
isStreaming = true // Shows blinking cursor at the end
)
}For long code blocks, use maxVisibleLines to make them collapsible (defaults to 500 lines; pass null for no limit):
CodeBlock(
code = longCode,
language = "java",
maxVisibleLines = 20 // Collapse after 20 lines
)You can create custom themes by implementing the CodeTheme interface:
import androidx.compose.ui.graphics.Color
import com.hrm.codehigh.ast.TokenType
import com.hrm.codehigh.theme.CodeTheme
object MyCustomTheme : CodeTheme {
override val background = Color(0xFF1E1E1E)
override val isDark = true
override fun colorFor(type: TokenType): Color {
return when (type) {
TokenType.KEYWORD -> Color(0xFF569CD6)
TokenType.STRING -> Color(0xFFCE9178)
TokenType.COMMENT -> Color(0xFF6A9955)
TokenType.NUMBER -> Color(0xFFB5CEA8)
// ... other token types
else -> Color(0xFFD4D4D4)
}
}
}Use CompositionLocal to provide a theme globally:
import com.hrm.codehigh.theme.LocalCodeTheme
import com.hrm.codehigh.theme.GithubLightTheme
@Composable
fun App() {
CompositionLocalProvider(LocalCodeTheme provides GithubLightTheme) {
// All CodeBlocks in this scope will use GithubLightTheme
CodeBlock(code = "...", language = "kotlin")
}
}You can register custom lexers for additional languages:
import com.hrm.codehigh.lexer.LanguageRegistry
import com.hrm.codehigh.lexer.Lexer
// Register your custom lexer
LanguageRegistry.register("my-language", MyCustomLexer)
LanguageRegistry.registerAlias("ml", "my-language") // Add alias
// Use it in CodeBlock
CodeBlock(
code = myCode,
language = "my-language" // Or "ml" via alias
):codehighlight-parser: Parser SDK module, containing tokens, lexers, language registration, and incremental parsing.:codehighlight-render: Compose render SDK module, containing renderer components, themes, and parser integration.:codehighlight-preview: Preview components and sample datasets.:composeApp: Cross-platform Demo application.:androidApp: Android Demo application.:iosApp: iOS application entry module../gradlew :androidApp:assembleDebug
./gradlew :composeApp:run
./gradlew :composeApp:wasmJsBrowserDevelopmentRun
iosApp/iosApp.xcworkspace in Xcode to run.# Library modules (JVM)
./gradlew :codehighlight-parser:jvmTest :codehighlight-render:jvmTest :codehighlight-preview:jvmTest
# Demo application (tests currently empty, reserved)
./gradlew :composeApp:jvmTestFor a detailed list of supported features, please refer to: HIGHLIGHTER_COVERAGE_ANALYSIS.md
CodeBlock internally renders lines in a regular Column rather than a LazyColumn. The default maxVisibleLines = 500 cap plus the collapse control bound the cost; full lazy rendering is left for a dedicated follow-up.codehigh.This project is licensed under the MIT License - see the LICENSE file for details.