tree-sitter/tags
2020-05-12 16:32:37 -07:00
..
include/tree_sitter tags: Avoid returning garbage pointer when length is zero 2020-03-24 14:53:08 -07:00
src Clear QueryCursor state between exec calls 2020-03-26 16:10:39 -07:00
Cargo.toml tags: 0.2.0 2020-05-12 16:32:37 -07:00
README.md Create tags readme 2020-03-20 10:29:20 -07:00

Tree-sitter Tags

Usage

Compile some languages into your app, and declare them:

extern "C" tree_sitter_python();
extern "C" tree_sitter_javascript();

Create a tag context. You need one of these for each thread that you're using for tag computation:

use tree_sitter_tags::TagsContext;

let context = TagsContext::new();

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

use tree_sitter_highlight::TagsConfiguration;

let python_language = unsafe { tree_sitter_python() };
let javascript_language = unsafe { tree_sitter_javascript() };

let python_config = HighlightConfiguration::new(
    python_language,
    &fs::read_to_string("./tree-sitter-python/queries/tags.scm").unwrap(),
    &fs::read_to_string("./tree-sitter-python/queries/locals.scm").unwrap(),
).unwrap();

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

Compute code navigation tags for some source code:

use tree_sitter_highlight::HighlightEvent;

let tags = context.generate_tags(
    &javascript_config,
    b"class A { getB() { return c(); } }",
    None,
    |_| None
);

for tag in tags {
    println!("kind: {:?}", tag.kind);
    println!("range: {:?}", tag.range);
    println!("name_range: {:?}", tag.name_range);
    println!("docs: {:?}", tag.docs);
}