
High-performance code highlighting with AST-based incremental parsing, streaming cursor for real-time output, 28+ languages, rich themes, customizable lexers, interactive token clicks, line numbers, copy and collapsible blocks.
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 = "1.0.1"
[libraries]
codehigh = { module = "io.github.huarangmeng:codehighlight", version.ref = "codehigh" }Then add it in your module's build.gradle.kts:
dependencies {
implementation(libs.codehigh)
}If you do not use Version Catalog, you can add the dependency directly:
dependencies {
implementation("io.github.huarangmeng:codehighlight:1.0.1")
}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:
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.widthDp(density).dp)
.height(size.heightDp(density).dp)
)
}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:
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: Core SDK module, containing lexers, renderer, themes, and incremental highlighting.: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../gradlew testFor a detailed list of supported features, please refer to: HIGHLIGHTER_COVERAGE_ANALYSIS.md
codehigh.This project is licensed under the MIT License - see the LICENSE file for details.
MIT License
Copyright (c) 2026 huarangmeng
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
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 = "1.0.1"
[libraries]
codehigh = { module = "io.github.huarangmeng:codehighlight", version.ref = "codehigh" }Then add it in your module's build.gradle.kts:
dependencies {
implementation(libs.codehigh)
}If you do not use Version Catalog, you can add the dependency directly:
dependencies {
implementation("io.github.huarangmeng:codehighlight:1.0.1")
}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:
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.widthDp(density).dp)
.height(size.heightDp(density).dp)
)
}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:
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: Core SDK module, containing lexers, renderer, themes, and incremental highlighting.: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../gradlew testFor a detailed list of supported features, please refer to: HIGHLIGHTER_COVERAGE_ANALYSIS.md
codehigh.This project is licensed under the MIT License - see the LICENSE file for details.
MIT License
Copyright (c) 2026 huarangmeng
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.