Syntax highlighting libraries for Java
Syntax highlighting is usually done at UI level, because highlighter output must match capabilities of the media. There is however a need for reusable highlighters that are not tied to any UI platform or that at least produce output that can be filtered to remove anything platform-specific. Here I am looking for such reusable highlighters that can be invoked from Java.
There are several situations when you would want to use platform-agnostic highlighter:
- Highlighter output sometimes requires additional processing.
- Highlighted code can be cached server-side to make pages load faster.
- The app may target exotic UI platform.
- Highlighted code in the browser may be interactively changed, which interferes with highlighting.
I have searched a lot of places: syntax-highlighting and syntax-highlighter topics on GitHub, several SO questions (1, 2, 3), as well as the internet. It looks like people are mostly using platform-specific highlighters, specifically Highlight.js in the browser, RSyntaxTextArea in Swing, and CodeView on Android. None of them can be directly adapted as a reusable platform-independent highlighter.
There are several half-dead and dead pure Java highlighters:
- Highlight.java - 2021, 25 languages
- Java Prettify - 2013, 28 languages
- JHighlight - 2022 (cleanup), 2014 (language support), 5 languages
- Jygments - 2016 (cleanup), 2011 (language support), 8 languages
Since pure Java highlighters are rather poor, let's consider cross-language integration options:
- JavaScript/Node.js: Highlight.js (200+ languages)
- Python: Pygments (500+ languages, there's an obsolete jython release)
- C++: Lexilla (100+ languages)
- Rust: syntect (uses Sublime definitions, 160+ languages)
- Lua: Scintillua (120+ languages)
- CLI: pygmentize (part of Pygments, 500+ languages), bat (uses syntect, 160+ languages), clp (uses Scintillua, 120+ languages)
- pure data: Sublime definitions (format, 160+ languages)
Tradeoffs everywhere. Pick your poison. It's not that hard to write a modern syntax-highlighting Java library using Scintillua or Sublime syntax definitions. It's an interesting idea for a new opensource project, but I am busy, so I will probably just shell out to pygmentize or something. Let me know if you develop proper Java library for syntax highlighting.