Synoptic
Syntax highlighting for Rust applications
This is a pretty lightweight (only 3 main depedencies) and simple regex-based syntax highlighter for Rust.
I originally wrote this for my text editor, Ox. It needed a fast, configurable and optimised syntax highlighter that could easily integrate with existing projects. However, you can (and are encouraged) to use it for any project you have in mind.
Advantages:
- Customisable - You can highlight almost any language by adding in custom syntax highlighting rules
- Fast - Is reasonably fast, enough so that it won't slow your projects down, even with large files and many different rules
- Simple - You can get highlighting code pretty quickly (see example below)
- Incremental - As this was designed for use with a text editor, it can really quickly re-highlight code upon edit commands
- Built in language rules - Get highlighting even faster by choosing from existing syntax rules
- File Buffering - Synoptic doesn't need the whole file to perform a correct highlighting job, thus allowing file buffering
- Escaping - Will handle escaping if you need it (
"here is a quote: \" tada!") - Interpolation - Will handle interpolation if you need it (
"My name is {name}, nice to meet you!")
Disadvantages:
- Not very well established - There may be inconsistencies in the included pre-built language highlighting rules
- Lacks understanding - This will not be able to provide very detailed syntax highlighting, as no parsing is performed
- Interpolation is limited - You can't nest interpolated tokens like
"this is { "f{ "u" }n" }"
Despite its disadvantages, if you just want a simple syntax highlighter with no frills or excess baggage, synoptic might just be your crate.
Installation
Just add it to your Cargo.toml:
[]
= "2"
- Construct a
Highlighterinstance - Add regular expressions and keywords to the highlighter and assign each a name
- Use the
runmethod to generate tokens - Use the
linemethod to obtain the tokens for each line
Built-in languages
You can also use some provided syntax highlighters for various popular languages using the from_extension function.
There is highly likely to be inconsistencies in the existing rules, please do open an issue if you spot any.
Currently, synoptic includes
- Various Higher Level Languages: Python, Ruby, Lua, Perl, Java, Visual Basic, Scala
- The C Family: C, C++, C#
- Various Lower Level Languages: Rust, Go, Assembly
- Web Technologies: HTML, CSS, PHP, Javascript, JSON, TypeScript
- Mathematical Languages: MATLAB, R, Haskell, Prolog
- Moblie Development: Kotlin, Swift, Dart
- Markup Languages: Markdown, YAML, TOML, XML, CSV
- Other: SQL, Bash, Nushell
Open an issue if there is a language not yet supported, or if you notice any issues in the built-in syntax highlighting rules.
Example
Here's an example of a Rust syntax highlighter, using the lliw crate.
use ;
use Fg;
// Let's use some demonstration code
pub static CODE: &str = "\
/*
Multiline comments
Work great
*/
pub fn main() -> bool {
// Demonstrate syntax highlighting in Rust!
println!(\"Full Unicode Support: 你好\");
// Interpolation
let name = \"peter\";
println!(\"My name is {name}, nice to meet you!\");
// Bye!
return true;
}
";
That will render a result similar to this (depending on your terminal's colour scheme):

License
MIT license to ensure that you can use it in your project
you can check the LICENSE file for more info