8 releases

✓ Uses Rust 2018 edition

0.2.0 May 12, 2020
0.1.6 May 31, 2019
0.1.5 Mar 13, 2019
0.1.4 Feb 25, 2019

#35 in Parser tooling

Download history 39/week @ 2020-03-22 2/week @ 2020-03-29 29/week @ 2020-04-05 36/week @ 2020-04-12 5/week @ 2020-04-19 7/week @ 2020-04-26 8/week @ 2020-05-03 19/week @ 2020-05-10 41/week @ 2020-05-17 15/week @ 2020-05-24 33/week @ 2020-05-31 11/week @ 2020-06-07 11/week @ 2020-06-14 16/week @ 2020-06-21 15/week @ 2020-06-28 22/week @ 2020-07-05

71 downloads per month
Used in tree-sitter-cli

MIT license

425KB
10K SLoC

C 6.5K SLoC // 0.1% comments Rust 3.5K SLoC // 0.1% comments

Tree-sitter Highlight

Build Status Build status Crates.io

Usage

Compile some languages into your app, and declare them:

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

Define the list of highlight names that you will recognize:

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:

use tree_sitter_highlight::Highlighter;

let highlighter = Highlighter::new();

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

use tree_sitter_highlight::HighlightConfiguration;

let html_language = unsafe { tree_sitter_html() };
let javascript_language = unsafe { tree_sitter_javascript() };

let html_config = HighlightConfiguration::new(
    html_language,
    &fs::read_to_string("./tree-sitter-html/queries/highlights.scm").unwrap(),
    &fs::read_to_string("./tree-sitter-html/queries/injections.scm").unwrap(),
    "",
).unwrap();

let javascript_config = HighlightConfiguration::new(
    javascript_language,
    &fs::read_to_string("./tree-sitter-javascript/queries/highlights.scm").unwrap(),
    &fs::read_to_string("./tree-sitter-javascript/queries/injections.scm").unwrap(),
    &fs::read_to_string("./tree-sitter-javascript/queries/locals.scm").unwrap(),
).unwrap();

Configure the recognized names:

javascript_config.configure(&highlight_names);

Highlight some code:

use tree_sitter_highlight::HighlightEvent;

let highlights = highlighter.highlight(
    &javascript_config,
    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).

Dependencies

~1–1.5MB
~43K SLoC