tree-sitter/highlight
Max Brunsfeld e239aa8229 highlight: don't include scope in ScopeEnd events
When there are embedded documents, multiple scopes can start or
end at the same position. Previously, there was no guarantee that
the ScopeEnd events would always occur in the reverse order of the
ScopeStart events. The easiest way to avoid exposing inconsistency
is to not surface the scopes being ended.
2019-02-20 16:45:51 -08:00
..
src highlight: don't include scope in ScopeEnd events 2019-02-20 16:45:51 -08:00
Cargo.toml highlight: 0.1.1 2019-02-20 10:42:56 -08:00
README.md highlight: Add a README 2019-02-19 17:56:46 -08:00

Tree-sitter Highlighting

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();

Load some property sheets:

use tree_sitter_highlight::load_property_sheet;

let javascript_property_sheet = load_property_sheet(
  fs::read_to_string("./tree-sitter-javascript/src/highlights.json").unwrap()
).unwrap();

let html_property_sheet = load_property_sheet(
  fs::read_to_string("./tree-sitter-html/src/highlights.json").unwrap()
).unwrap();

Highlight some code:

use tree_sitter_highlight::{highlight, HighlightEvent};

let highlights = highlight(
    b"const x = new Y();",
    unsafe { tree_sitter_javascript() },
    &javascript_property_sheet,
    &|_| None
).unwrap();

for event in highlights {
    match event {
        HighlightEvent::Source(s) {
            eprintln!("source: {:?}", s);
        },
        HighlightEvent::ScopeStart(s) {
            eprintln!("scope started: {:?}", s);
        },
        HighlightEvent::ScopeEnd(s) {
            eprintln!("scope ended: {:?}", s);
        },
    }
}

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).