Update tree-sitter-{highlight,tags} READMEs
We now have an easier way to get at the language-specific configuration in Rust, since we publish each language grammar as a crate with useful accessor functions and globals.
This commit is contained in:
parent
76862e281b
commit
234bd79591
2 changed files with 30 additions and 34 deletions
|
|
@ -1,5 +1,4 @@
|
|||
Tree-sitter Highlight
|
||||
=========================
|
||||
# `tree-sitter-highlight`
|
||||
|
||||
[](https://travis-ci.org/tree-sitter/tree-sitter)
|
||||
[](https://ci.appveyor.com/project/maxbrunsfeld/tree-sitter/branch/master)
|
||||
|
|
@ -7,11 +6,13 @@ Tree-sitter Highlight
|
|||
|
||||
### Usage
|
||||
|
||||
Compile some languages into your app, and declare them:
|
||||
Add this crate, and the language-specific crates for whichever languages you want to parse, to your `Cargo.toml`:
|
||||
|
||||
```rust
|
||||
extern "C" tree_sitter_html();
|
||||
extern "C" tree_sitter_javascript();
|
||||
```toml
|
||||
[dependencies]
|
||||
tree-sitter-highlight = "0.19"
|
||||
tree-sitter-html = "0.19"
|
||||
tree-sitter-javascript = "0.19"
|
||||
```
|
||||
|
||||
Define the list of highlight names that you will recognize:
|
||||
|
|
@ -60,17 +61,17 @@ let html_language = unsafe { tree_sitter_html() };
|
|||
let javascript_language = unsafe { tree_sitter_javascript() };
|
||||
|
||||
let html_config = HighlightConfiguration::new(
|
||||
html_language,
|
||||
&fs::read_to_string("./tree-sitter-html/queries/highlights.scm").unwrap(),
|
||||
&fs::read_to_string("./tree-sitter-html/queries/injections.scm").unwrap(),
|
||||
tree_sitter_html::language(),
|
||||
tree_sitter_html::HIGHLIGHTS_QUERY,
|
||||
tree_sitter_html::INJECTIONS_QUERY,
|
||||
"",
|
||||
).unwrap();
|
||||
|
||||
let javascript_config = HighlightConfiguration::new(
|
||||
javascript_language,
|
||||
&fs::read_to_string("./tree-sitter-javascript/queries/highlights.scm").unwrap(),
|
||||
&fs::read_to_string("./tree-sitter-javascript/queries/injections.scm").unwrap(),
|
||||
&fs::read_to_string("./tree-sitter-javascript/queries/locals.scm").unwrap(),
|
||||
tree_sitter_javascript::language(),
|
||||
tree_sitter_javascript::HIGHLIGHTS_QUERY,
|
||||
tree_sitter_javascript::INJECTIONS_QUERY,
|
||||
tree_sitter_javascript::LCOALS_QUERY,
|
||||
).unwrap();
|
||||
```
|
||||
|
||||
|
|
|
|||
|
|
@ -1,13 +1,14 @@
|
|||
Tree-sitter Tags
|
||||
=========================
|
||||
# `tree-sitter-tags`
|
||||
|
||||
### Usage
|
||||
|
||||
Compile some languages into your app, and declare them:
|
||||
Add this crate, and the language-specific crates for whichever languages you want to parse, to your `Cargo.toml`:
|
||||
|
||||
```rust
|
||||
extern "C" tree_sitter_python();
|
||||
extern "C" tree_sitter_javascript();
|
||||
```toml
|
||||
[dependencies]
|
||||
tree-sitter-tags = "0.19"
|
||||
tree-sitter-javascript = "0.19"
|
||||
tree-sitter-python = "0.19"
|
||||
```
|
||||
|
||||
Create a tag context. You need one of these for each thread that you're using for tag computation:
|
||||
|
|
@ -21,34 +22,28 @@ let context = TagsContext::new();
|
|||
Load some tagging queries from the `queries` directory of some language repositories:
|
||||
|
||||
```rust
|
||||
use tree_sitter_highlight::TagsConfiguration;
|
||||
use tree_sitter_tags::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(),
|
||||
let python_config = TagsConfiguration::new(
|
||||
tree_sitter_python::language(),
|
||||
tree_sitter_python::TAGGING_QUERY,
|
||||
"",
|
||||
).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(),
|
||||
let javascript_config = TagsConfiguration::new(
|
||||
tree_sitter_javascript::language(),
|
||||
tree_sitter_javascript::TAGGING_QUERY,
|
||||
tree_sitter_javascript::LOCALS_QUERY,
|
||||
).unwrap();
|
||||
```
|
||||
|
||||
Compute code navigation tags for some source code:
|
||||
|
||||
```rust
|
||||
use tree_sitter_highlight::HighlightEvent;
|
||||
|
||||
let tags = context.generate_tags(
|
||||
&javascript_config,
|
||||
b"class A { getB() { return c(); } }",
|
||||
None,
|
||||
|_| None
|
||||
);
|
||||
|
||||
for tag in tags {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue