Skip to content

huarangmeng/codehigh

Repository files navigation

CodeHigh

Kotlin Compose Multiplatform Maven Central Android API License

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.

δΈ­ζ–‡η‰ˆζœ¬

🌟 Key Features

  • Multi-platform Consistency: Uses Compose Multiplatform for consistent code highlighting on Android, iOS, Desktop (JVM), and Web (Wasm/JS).
  • 28+ Programming Languages: Built-in support for Kotlin, Java, Swift, Python, JavaScript, TypeScript, Ruby, PHP, Dart, Scala, Go, Rust, C, C++, R, SQL, JSON, YAML, TOML, Dockerfile, Bash, Lua, Haskell, Elixir, XML, HTML, CSS, Diff, and more.
  • Incremental Highlighting: AST-based incremental parsing and highlighting for efficient updates in streaming scenarios (e.g., AI chat, real-time editing).
  • Rich Themes: 4 built-in themes including One Dark Pro, GitHub Light, Dracula Pro, and Solarized Light, with custom theme support.
  • Code Block Components: Complete code block rendering with line numbers, configurable start lines, highlighted lines, diff mode, copy button, language label, collapsible/expandable, and streaming cursor animation.
  • Custom Lexer Support: Extensible lexer API for adding support for additional programming languages.
  • Token Click Interaction: Click callback for individual code tokens for interactive features.
  • Light/Dark Themes: Built-in light and dark themes with explicit selection and custom theme support.

πŸ“š Supported Languages (28+)

System Languages β€” Kotlin, Java, Swift
  • Kotlin: keywords, strings (including triple-quoted and template strings), comments, annotations, built-in types, numbers
  • Java: keywords, strings, comments, annotations, built-in types, numbers
  • Swift: keywords, strings, comments, built-in types, numbers
Scripting Languages β€” Python, JavaScript, TypeScript, Ruby, PHP, Dart, Scala, Lua, Haskell, Elixir
  • Python: keywords, strings (single/double/triple quotes), comments, decorators, built-in functions
  • JavaScript: keywords, strings, comments, regular expressions, template literals
  • TypeScript: all JavaScript features plus TypeScript-specific syntax (types, interfaces, generics, etc.)
  • Ruby: keywords, strings, comments, instance variables, and common Rails-oriented built-ins
  • PHP: <?php tags, keywords, strings, comments, $variable tokens, and ->/:: operators
  • Dart: keywords, strings, annotations, common Flutter types, and setState
  • Scala: keywords, strings, annotations, case class, trait, and object
  • Lua: keywords, strings, comments, local variables, and require/print
  • Haskell: keywords, strings, comments, type signatures, and algebraic data types
  • Elixir: keywords, strings, pipe operators, module definitions, and common built-in calls
System/Systems Languages β€” Go, Rust, C, C++
  • Go: keywords, strings, comments, built-in types
  • Rust: keywords, strings, comments, attributes, macros
  • C/C++: keywords, strings, comments, preprocessor directives
Data/Configuration Languages β€” R, SQL, JSON, YAML, TOML, Dockerfile
  • R: assignment operators, function definitions, library, data.frame, and common data analysis built-ins
  • SQL: keywords, strings, comments, identifiers
  • JSON: strings, numbers, booleans, null, objects, arrays
  • YAML: keys, values, strings, comments, lists, dictionaries
  • TOML: sections, array sections, key-value pairs, strings, and booleans
  • Dockerfile: build instructions, environment variables, strings, and comments
Markup/Style/Diff Languages β€” Bash, XML, HTML, CSS, Diff
  • Bash/Shell: keywords, strings, comments, variables, commands
  • XML/HTML: tags, attributes, strings, comments, entities
  • CSS: selectors, properties, values, comments, at-rules
  • Diff/Patch: file headers, hunk markers, added/removed lines, and patch metadata

🎨 Built-in Themes (4)

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

πŸ“¦ Installation

Add Dependency

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")
}

πŸ› οΈ Usage

Basic Code Block

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
    )
}

Inline Code

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)
}

Measuring Inline Code Size

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.

Streaming Mode (For AI Chat/Real-time Output)

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
    )
}

Collapsible Code Blocks

For long code blocks, use maxVisibleLines to make them collapsible:

CodeBlock(
    code = longCode,
    language = "java",
    maxVisibleLines = 20 // Collapse after 20 lines
)

Custom Theme

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)
        }
    }
}

Using LocalCodeTheme for Global Theming

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")
    }
}

Registering Custom Lexers

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
)

πŸ—οΈ Project Structure

  • :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.

πŸš€ Quick Start

Running the Demo App

  • Android: ./gradlew :androidApp:assembleDebug
  • Desktop: ./gradlew :composeApp:run
  • Web (Wasm): ./gradlew :composeApp:wasmJsBrowserDevelopmentRun
  • iOS: Open iosApp/iosApp.xcworkspace in Xcode to run.

Running Tests

./gradlew test

πŸ“Š Roadmap & Coverage

For a detailed list of supported features, please refer to: HIGHLIGHTER_COVERAGE_ANALYSIS.md

πŸ’‘ Recommended

  • Markdown β€” A dedicated Kotlin Multiplatform Markdown parsing and rendering library by the same author. If you need Markdown parsing or rendering, use it alongside codehigh.
  • LaTeX β€” A Kotlin Multiplatform LaTeX parsing and rendering library by the same author. If you need both code highlighting and LaTeX rendering in your project, check it out!

πŸ“„ License

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.

Packages

 
 
 

Contributors

Languages