Expand description
§tree-sitter-bundle
A collection of tree-sitter parsers compiled into a single crate, with their highlight queries, ready for editor integration.
Each grammar is gated behind a Cargo feature, so you only pay for the languages
you enable. Enable everything with the full feature, or pick a subset:
tree-sitter-bundle = { version = "0.1", features = ["rust", "python", "json"] }§Parsing
let lang = tree_sitter_bundle::get("rust").unwrap();
let mut parser = tree_sitter::Parser::new();
parser.set_language(&lang.language()).unwrap();
let tree = parser.parse("fn main() {}", None).unwrap();
assert_eq!(tree.root_node().kind(), "source_file");§Highlighting (requires the highlight feature, on by default)
use tree_sitter_highlight::{Highlighter, HighlightEvent};
let names = ["keyword", "function", "string", "type", "variable"]
.map(String::from);
let lang = tree_sitter_bundle::get("rust").unwrap();
let config = lang.highlight_config(&names).unwrap();
let mut hl = Highlighter::new();
let src = b"fn main() {}";
for event in hl.highlight(&config, src, None, |_| None).unwrap() {
match event.unwrap() {
HighlightEvent::Source { start, end } => { /* emit text src[start..end] */ }
HighlightEvent::HighlightStart(h) => { /* open span names[h.0] */ }
HighlightEvent::HighlightEnd => { /* close span */ }
}
}Re-exports§
pub use tree_sitter;pub use tree_sitter_highlight;
Structs§
- Language
Entry - A single bundled language: its grammar plus its bundled queries.
Functions§
- count
- Number of compiled languages.
- from_
extension - Look up a language by file extension (with or without a leading dot).
- from_
path - Look up a language by file path, using its extension.
- get
- Look up a language by its identifier (case-sensitive), e.g.
get("rust"). - languages
- Iterate over every compiled language in the bundle.