#incremental #parsing #syntax #highlighting

tree-sitter-highlight

Library for performing syntax highlighting with Tree-sitter

6 releases

✓ Uses Rust 2018 edition

0.1.5 Mar 13, 2019
0.1.4 Feb 25, 2019

#110 in Parser tooling

Download history 43/week @ 2019-02-15 52/week @ 2019-02-22 34/week @ 2019-03-01 20/week @ 2019-03-08 15/week @ 2019-03-15 25/week @ 2019-03-22 1/week @ 2019-03-29 9/week @ 2019-04-05 5/week @ 2019-04-12 4/week @ 2019-04-19

63 downloads per month
Used in 1 crate

MIT license

45KB
1K SLoC

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

Dependencies

~5.5MB
~89K SLoC