Skip to content

SnipMeDev/Highlights

Repository files navigation

highlights_banner_opaque

Maven Central Kotlin GitHub License

Highlights

Kotlin Multiplatform syntax highlighting engine.

Installation ⬇️

repositories {
    mavenCentral()
}
implementation("dev.snipme:highlights:1.0.0")

Features ✨

  • Code component analysis (Keyword, comment, etc.)
  • Multiple syntax languages (Java, Swift, Kotlin, C, ...)
  • Themes
  • Text bolding (emphasis)
  • Result caching and support for incremental changes
  • Written in pure Kotlin, so available for many platforms 📱 💻 🖥️
  • Sync or async mode

Support ☕

Kotlin Multiplatform is a fresh environment and developing for it is neither fast nor easy 🥲

If you feel that any of our project has saved you a time or effort, then consider supporting us via:
🧋 Buy Me A Coffee

Usage ✍️

💡 As each Highlights instance caches code analysis, it is recommended to re-use the same instance for small code changes.

To start, simply put any code snippet in the default builder

Highlights.default().apply {
    setCode("public class ExampleClass {}")
    // Keywords = [public, class], Marks = [{, }]
    getCodeStructure()
    // BoldHighlight, ColorHighlight
    getHighlights()
}

There is also a possibility to handle result asynchronously

highlights.getHighlightsAsync(
    object : DefaultHighlightsResultListener() {
        // onStart
        // onError
        // onCancel
        override fun onSuccess(result: List<CodeHighlight>) {
            emitResult(highlights)
        }
    }
)

You can also set language, theme and phrase emphasis. Language and theme has impact on the ColorHighlight and emphasis is represented by the BoldHighlight.

Highlights.Builder()
    .code("public class ExampleClass {}")
    .theme(SyntaxThemes.monokai())
    .language(SyntaxLanguage.JAVA)
    .emphasis(PhraseLocation(13, 25)) // ExampleClass
    .build()
    .run {
        getHighlights()
    }

More advance usage of this library is shown here.

Languages 🌍

C, C++, DART, JAVA, KOTLIN, RUST, C#, COFFEESCRIPT, JAVASCRIPT, PERL, PYTHON, RUBY, SHELL, SWIFT, TYPESCRIPT, GO, PHP

Themes 🖌️

The library comes with predefined syntax coloring themes available in SyntaxThemes:

Dark 🌚

Darcula Monokai Notepad Matrix Pastel Atom One
  • #EDEDED Code
  • #CC7832 Keyword
  • #6A8759 String
  • #6897BB Literal
  • #909090 Comment
  • #BBB529 Metadata
  • #629755 Multiline Comment
  • #CC7832 Punctuation
  • #EDEDED Mark
  • #F8F8F2 Code
  • #F92672 Keyword
  • #E6DB74 String
  • #AE81FF Literal
  • #FD971F Comment
  • #B8F4B8 Metadata
  • #FD971F Multiline Comment
  • #F8F8F2 Punctuation
  • #F8F8F2 Mark
  • #000080 Code
  • #0000FF Keyword
  • #808080 String
  • #FF8000 Literal
  • #008000 Comment
  • #000080 Metadata
  • #008000 Multiline Comment
  • #AA2C8C Punctuation
  • #AA2C8C Mark
  • #008500 Code
  • #008500 Keyword
  • #269926 String
  • #39E639 Literal
  • #67E667 Comment
  • #008500 Metadata
  • #67E667 Multiline Comment
  • #008500 Punctuation
  • #008500 Mark
  • #DFDEE0 Code
  • #729FCF Keyword
  • #93CF55 String
  • #8AE234 Literal
  • #888A85 Comment
  • #5DB895 Metadata
  • #888A85 Multiline Comment
  • #CB956D Punctuation
  • #CB956D Mark
  • #DFDEE0 Code
  • #729FCF Keyword
  • #93CF55 String
  • #8AE234 Literal
  • #888A85 Comment
  • #5DB895 Metadata
  • #888A85 Multiline Comment
  • #CB956D Punctuation
  • #CB956D Mark

Light 🌞

Darcula Monokai Notepad Matrix Pastel Atom One
  • #121212 Code
  • #CC7832 Keyword
  • #6A8759 String
  • #6897BB Literal
  • #909090 Comment
  • #BBB529 Metadata
  • #629755 Multiline Comment
  • #CC7832 Punctuation
  • #121212 Mark
  • #07070D Code
  • #F92672 Keyword
  • #E6DB74 String
  • #AE81FF Literal
  • #FD971F Comment
  • #B8F4B8 Metadata
  • #FD971F Multiline Comment
  • #07070D Punctuation
  • #07070D Mark
  • #000080 Code
  • #0000FF Keyword
  • #808080 String
  • #FF8000 Literal
  • #008000 Comment
  • #000080 Metadata
  • #008000 Multiline Comment
  • #AA2C8C Punctuation
  • #AA2C8C Mark
  • #008500 Code
  • #008500 Keyword
  • #269926 String
  • #39E639 Literal
  • #67E667 Comment
  • #008500 Metadata
  • #67E667 Multiline Comment
  • #008500 Punctuation
  • #008500 Mark
  • #20211F Code
  • #729FCF Keyword
  • #93CF55 String
  • #8AE234 Literal
  • #888A85 Comment
  • #5DB895 Metadata
  • #888A85 Multiline Comment
  • #CB956D Punctuation
  • #CB956D Mark
  • #DFDEE0 Code
  • #729FCF Keyword
  • #93CF55 String
  • #8AE234 Literal
  • #888A85 Comment
  • #5DB895 Metadata
  • #888A85 Multiline Comment
  • #CB956D Punctuation
  • #CB956D Mark

You can also prepare your own themes and use them. Just create the SyntaxTheme class:

SyntaxTheme(
    key = "MY_THEME",
    code = 0xEDEDED,
    keyword = 0xCC7832,
    string = 0x6A8759,
    literal = 0x6897BB,
    comment = 0x909090,
    metadata = 0xBBB529,
    multilineComment = 0x629755,
    punctuation = 0xCC7832,
    mark = 0xEDEDED
)

Popular uses 🙌

If your project uses this code, please write me or add your info

Type Name
Library KodeView
Application SnippLog
Application FlowMVI Sample

TODO 🚧

  • Migrate some lists to sets
  • Optimize code analysis
  • Add more themes and languages
  • Support italic and underline text style

Contribution 💻

Any form of support is very welcomed. Bugs, problems and new feature requests should be placed in the Issues tab with proper labeling. New feature can be also submitted via Pull Requests. Then make sure:

  • Current and added tests have passed
  • CHANGELOG and README have been updated
  • New code matches library's vision and code style

License 🖋️

Copyright 2023-2024 Tomasz Kądziołka.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.