Tree-sitter Highlight

Build Status Build status Crates.io

Usage

Compile some languages into your app, and declare them:

rust extern "C" tree_sitter_html(); extern "C" tree_sitter_javascript();

Define the list of highlight names that you will recognize:

rust let highlight_names = [ "attribute", "constant", "function.builtin", "function", "keyword", "operator", "property", "punctuation", "punctuation.bracket", "punctuation.delimiter", "string", "string.special", "tag", "type", "type.builtin", "variable", "variable.builtin", "variable.parameter", ] .iter() .cloned() .map(String::from) .collect();

Create a highlighter. You need one of these for each thread that you're using for syntax highlighting:

```rust use treesitterhighlight::Highlighter;

let highlighter = Highlighter::new(); ```

Load some highlighting queries from the queries directory of some language repositories:

```rust use treesitterhighlight::HighlightConfiguration;

let htmllanguage = unsafe { treesitterhtml() }; let javascriptlanguage = unsafe { treesitterjavascript() };

let htmlconfig = HighlightConfiguration::new( htmllanguage, &fs::readtostring("./tree-sitter-html/queries/highlights.scm").unwrap(), &fs::readtostring("./tree-sitter-html/queries/injections.scm").unwrap(), "", ).unwrap();

let javascriptconfig = HighlightConfiguration::new( javascriptlanguage, &fs::readtostring("./tree-sitter-javascript/queries/highlights.scm").unwrap(), &fs::readtostring("./tree-sitter-javascript/queries/injections.scm").unwrap(), &fs::readtostring("./tree-sitter-javascript/queries/locals.scm").unwrap(), ).unwrap(); ```

Configure the recognized names:

rust javascript_config.configure(&highlight_names);

Highlight some code:

```rust use treesitterhighlight::HighlightEvent;

let highlights = highlighter.highlight( &javascriptconfig, b"const x = new Y();", None, || None ).unwrap();

for event in highlights { match event? { HighlightEvent::Source {start, end} => { eprintln!("source: {}-{}", start, end); }, HighlightEvent::HighlightStart(s) { eprintln!("highlight style started: {:?}", s); }, HighlightEvent::HighlightEnd { eprintln!("highlight style ended"); }, } } ```

The last parameter to highlight is a language injection callback. This allows other languages to be retrieved when Tree-sitter detects an embedded document (for example, a piece of JavaScript code inside of a script tag within HTML).