# syntect [![Crates.io](https://img.shields.io/crates/v/syntect.svg?maxAge=2591000)](https://crates.io/crates/syntect) [![Documentation](https://docs.rs/syntect/badge.svg)](https://docs.rs/syntect) [![Crates.io](https://img.shields.io/crates/l/syntect.svg?maxAge=2591000)]() [![Build Status](https://github.com/trishume/syntect/actions/workflows/CI.yml/badge.svg)](https://github.com/trishume/syntect/actions) [![codecov](https://codecov.io/gh/trishume/syntect/branch/master/graph/badge.svg)](https://codecov.io/gh/trishume/syntect) `syntect` is a syntax highlighting library for Rust that uses [Sublime Text syntax definitions](http://www.sublimetext.com/docs/3/syntax.html#include-syntax). It aims to be a good solution for any Rust project that needs syntax highlighting, including deep integration with text editors written in Rust. It's used in production by at least two companies, and by [many open source projects](#projects-using-syntect). If you are writing a text editor (or something else needing highlighting) in Rust and this library doesn't fit your needs, I consider that a bug and you should file an issue or email me. I consider this project mostly complete, I still maintain it and review PRs, but it's not under heavy development. ## Important Links - API docs with examples: - [Changelogs and upgrade notes for past releases](https://github.com/trishume/syntect/releases) ## Getting Started `syntect` is [available on crates.io](https://crates.io/crates/syntect). You can install it by adding this line to your `Cargo.toml`: ```toml syntect = "5.0" ``` After that take a look at the [documentation](https://docs.rs/syntect) and the [examples](https://github.com/trishume/syntect/tree/master/examples). If you've cloned this repository, be sure to run ```bash git submodule update --init ``` to fetch all the required dependencies for running the tests. ## Features/Goals - [x] Work with many languages (accomplished through using existing grammar formats) - [x] Highlight super quickly, faster than nearly all text editors - [x] Include easy to use API for basic cases - [x] API allows use in fancy text editors with piece tables and incremental re-highlighting and the like. - [x] Expose internals of the parsing process so text editors can do things like cache parse states and use semantic info for code intelligence - [x] High quality highlighting, supporting things like heredocs and complex syntaxes (like Rust's). - [x] Include a compressed dump of all the default syntax definitions in the library binary so users don't have to manage a folder of syntaxes. - [x] Well documented, I've tried to add a useful documentation comment to everything that isn't utterly self explanatory. - [x] Built-in output to coloured HTML `
` tags or 24-bit colour ANSI terminal escape sequences.
- [x] Nearly complete compatibility with Sublime Text 3, including lots of edge cases. Passes nearly all of Sublime's syntax tests, see [issue 59](https://github.com/trishume/syntect/issues/59).
- [x] Load up quickly, currently in around 23ms but could potentially be even faster.

## Screenshots

There's currently an example program called `syncat` that prints one of the source files using hard-coded themes and syntaxes using 24-bit terminal escape sequences supported by many newer terminals.
These screenshots don't look as good as they could for two reasons:
first the sRGB colours aren't corrected properly, and second the Rust syntax definition uses some fancy labels that these themes don't have highlighting for.

![Nested languages](http://i.imgur.com/bByxb1E.png)
![Base 16 Ocean Dark](http://i.imgur.com/CwiPOwZ.png)
![Solarized Light](http://i.imgur.com/l3zcO4J.png)
![InspiredGithub](http://i.imgur.com/a7U1r2j.png)

## Example Code

Prints highlighted lines of a string to the terminal.
See the [easy](https://docs.rs/syntect/latest/syntect/easy/index.html) and [html](https://docs.rs/syntect/latest/syntect/html/index.html) module docs for more basic use case examples.

```rust
use syntect::easy::HighlightLines;
use syntect::parsing::SyntaxSet;
use syntect::highlighting::{ThemeSet, Style};
use syntect::util::{as_24_bit_terminal_escaped, LinesWithEndings};

// Load these once at the start of your program
let ps = SyntaxSet::load_defaults_newlines();
let ts = ThemeSet::load_defaults();

let syntax = ps.find_syntax_by_extension("rs").unwrap();
let mut h = HighlightLines::new(syntax, &ts.themes["base16-ocean.dark"]);
let s = "pub struct Wow { hi: u64 }\nfn blah() -> u64 {}";
for line in LinesWithEndings::from(s) {
    let ranges: Vec<(Style, &str)> = h.highlight_line(line, &ps).unwrap();
    let escaped = as_24_bit_terminal_escaped(&ranges[..], true);
    print!("{}", escaped);
}
```

## Performance

Currently `syntect` is one of the faster syntax highlighting engines, but not the fastest. The following perf features are done:

- [x] Pre-link references between languages (e.g `