tree-sitter/highlight/README.md

113 lines
2.6 KiB
Markdown
Raw Normal View History

# Tree-sitter Highlight
2019-02-19 17:56:46 -08:00
[![crates.io badge]][crates.io]
[crates.io]: https://crates.io/crates/tree-sitter-highlight
[crates.io badge]: https://img.shields.io/crates/v/tree-sitter-highlight.svg?color=%23B48723
2019-02-19 17:56:46 -08:00
2024-02-04 02:00:42 -05:00
## Usage
2019-02-19 17:56:46 -08:00
2024-02-04 02:00:42 -05:00
Add this crate, and the language-specific crates for whichever languages you want
to parse, to your `Cargo.toml`:
2019-02-19 17:56:46 -08:00
```toml
[dependencies]
tree-sitter-highlight = "0.22.0"
tree-sitter-javascript = "0.21.3"
2019-02-19 17:56:46 -08:00
```
Define the list of highlight names that you will recognize:
2019-02-19 17:56:46 -08:00
```rust
let highlight_names = [
"attribute",
"comment",
"constant",
"constant.builtin",
"constructor",
"embedded",
"function",
"function.builtin",
"keyword",
"module",
"number",
"operator",
"property",
"property.builtin",
"punctuation",
"punctuation.bracket",
"punctuation.delimiter",
"punctuation.special",
"string",
"string.special",
"tag",
"type",
"type.builtin",
"variable",
"variable.builtin",
"variable.parameter",
2021-05-17 21:54:16 +02:00
];
2019-10-14 16:55:14 -07:00
```
2019-02-19 17:56:46 -08:00
2024-02-04 02:00:42 -05:00
Create a highlighter. You need one of these for each thread that you're using for
syntax highlighting:
2019-10-14 16:55:14 -07:00
```rust
use tree_sitter_highlight::Highlighter;
2019-10-14 16:55:14 -07:00
let mut highlighter = Highlighter::new();
2019-10-14 16:55:14 -07:00
```
Load some highlighting queries from the `queries` directory of the language repository:
2019-10-14 16:55:14 -07:00
```rust
use tree_sitter_highlight::HighlightConfiguration;
let javascript_language = tree_sitter_javascript::language();
2019-10-14 16:55:14 -07:00
let mut javascript_config = HighlightConfiguration::new(
javascript_language,
2024-02-04 02:00:42 -05:00
"javascript",
tree_sitter_javascript::HIGHLIGHT_QUERY,
tree_sitter_javascript::INJECTIONS_QUERY,
tree_sitter_javascript::LOCALS_QUERY,
).unwrap();
2019-02-19 17:56:46 -08:00
```
Configure the recognized names:
```rust
javascript_config.configure(&highlight_names);
```
2019-02-19 17:56:46 -08:00
Highlight some code:
```rust
2019-10-14 16:55:14 -07:00
use tree_sitter_highlight::HighlightEvent;
2019-02-19 17:56:46 -08:00
2019-10-14 16:55:14 -07:00
let highlights = highlighter.highlight(
&javascript_config,
2019-02-19 17:56:46 -08:00
b"const x = new Y();",
2019-10-14 16:55:14 -07:00
None,
|_| None
2019-02-19 17:56:46 -08:00
).unwrap();
for event in highlights {
match event.unwrap() {
HighlightEvent::Source {start, end} => {
eprintln!("source: {}-{}", start, end);
2019-02-19 17:56:46 -08:00
},
HighlightEvent::HighlightStart(s) => {
2019-10-14 16:55:14 -07:00
eprintln!("highlight style started: {:?}", s);
2019-02-19 17:56:46 -08:00
},
HighlightEvent::HighlightEnd => {
eprintln!("highlight style ended");
2019-02-19 17:56:46 -08:00
},
}
}
```
2024-02-04 02:00:42 -05:00
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 a `script` tag within HTML).