2023-08-28 21:16:02 +03:00
# Tree-sitter Highlight
2019-02-19 17:56:46 -08:00
docs: update badges; fix markdown lint complains
Linter config `.vscode/settings.json`:
```json
{
"[markdown]": {
"files.trimTrailingWhitespace": false,
},
"markdownlint.config": {
"default": true,
// "ul-style": {
// "style": "asterisk"
// },
"MD001": false,
"MD024": false,
"MD025": false,
"MD033": false,
"MD041": false,
"MD053": false,
},
}
```
2023-04-16 21:14:19 +03: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
### Usage
2021-03-31 09:56:24 -04: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
2021-03-31 09:56:24 -04:00
```toml
[dependencies]
2022-07-25 15:04:29 -05:00
tree-sitter-highlight = "^0.20"
2021-03-31 09:56:24 -04:00
tree-sitter-javascript = "0.19"
2019-02-19 17:56:46 -08:00
```
2019-10-24 12:01:27 -07:00
Define the list of highlight names that you will recognize:
2019-02-19 17:56:46 -08:00
```rust
2022-07-25 15:04:29 -05:00
let highlight_names = [
2019-10-24 12:01:27 -07:00
"attribute",
"constant",
"function.builtin",
"function",
"keyword",
"operator",
"property",
"punctuation",
"punctuation.bracket",
"punctuation.delimiter",
"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
2019-10-24 12:01:27 -07: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
2019-10-24 12:01:27 -07:00
use tree_sitter_highlight::Highlighter;
2019-10-14 16:55:14 -07:00
2022-07-25 15:04:29 -05:00
let mut highlighter = Highlighter::new();
2019-10-14 16:55:14 -07:00
```
2022-07-25 15:04:29 -05:00
Load some highlighting queries from the `queries` directory of the language repository:
2019-10-14 16:55:14 -07:00
```rust
2019-10-24 12:01:27 -07:00
use tree_sitter_highlight::HighlightConfiguration;
2022-07-25 15:04:29 -05:00
let javascript_language = tree_sitter_javascript::language();
2019-10-14 16:55:14 -07:00
2022-07-25 15:04:29 -05:00
let mut javascript_config = HighlightConfiguration::new(
javascript_language,
tree_sitter_javascript::HIGHLIGHT_QUERY,
tree_sitter_javascript::INJECTION_QUERY,
tree_sitter_javascript::LOCALS_QUERY,
2019-10-24 12:01:27 -07:00
).unwrap();
2019-02-19 17:56:46 -08:00
```
2019-12-06 16:00:27 -06: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(
2019-12-06 15:05:28 -06:00
& javascript_config,
2019-02-19 17:56:46 -08:00
b"const x = new Y();",
2019-10-14 16:55:14 -07:00
None,
2019-12-06 15:05:28 -06:00
|_| None
2019-02-19 17:56:46 -08:00
).unwrap();
for event in highlights {
2020-11-24 12:35:38 +01:00
match event.unwrap() {
2019-12-06 15:05:28 -06:00
HighlightEvent::Source {start, end} => {
eprintln!("source: {}-{}", start, end);
2019-02-19 17:56:46 -08:00
},
2020-11-24 12:35:38 +01: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
},
2020-11-24 12:35:38 +01:00
HighlightEvent::HighlightEnd => {
2019-12-06 15:05:28 -06:00
eprintln!("highlight style ended");
2019-02-19 17:56:46 -08: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 of a `script` tag within HTML).