tree-sitter/tags
Patrick Thomson d5576e306c
Merge pull request #708 from tree-sitter/add-tagger-error-detection
Add ts_tags_buffer_found_parse_error capabilities for error detection during tagging.
2020-08-05 14:07:31 -04:00
..
include/tree_sitter Add parameter in the header. 2020-08-05 13:10:02 -04:00
src Merge pull request #708 from tree-sitter/add-tagger-error-detection 2020-08-05 14:07:31 -04: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);
}