tree-sitter/highlight/README.md

111 lines
3 KiB
Markdown
Raw Normal View History

2019-10-14 16:55:14 -07:00
Tree-sitter Highlight
2019-02-19 17:56:46 -08:00
=========================
[![Build Status](https://travis-ci.org/tree-sitter/tree-sitter.svg?branch=master)](https://travis-ci.org/tree-sitter/tree-sitter)
[![Build status](https://ci.appveyor.com/api/projects/status/vtmbd6i92e97l55w/branch/master?svg=true)](https://ci.appveyor.com/project/maxbrunsfeld/tree-sitter/branch/master)
[![Crates.io](https://img.shields.io/crates/v/tree-sitter-highlight.svg)](https://crates.io/crates/tree-sitter-highlight)
### Usage
Compile some languages into your app, and declare them:
```rust
extern "C" tree_sitter_html();
extern "C" tree_sitter_javascript();
```
Define the list of highlight names that you will recognize:
2019-02-19 17:56:46 -08:00
```rust
let highlight_names = [
"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",
]
.iter()
.cloned()
.map(String::from)
.collect();
2019-10-14 16:55:14 -07:00
```
2019-02-19 17:56:46 -08: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 highlighter = Highlighter::new();
2019-10-14 16:55:14 -07:00
```
Load some highlighting queries from the `queries` directory of some language repositories:
```rust
use tree_sitter_highlight::HighlightConfiguration;
2019-10-14 16:55:14 -07:00
let html_language = unsafe { tree_sitter_html() };
let javascript_language = unsafe { tree_sitter_javascript() };
let html_config = HighlightConfiguration::new(
2019-10-14 16:55:14 -07:00
html_language,
&fs::read_to_string("./tree-sitter-html/queries/highlights.scm").unwrap(),
&fs::read_to_string("./tree-sitter-html/queries/injections.scm").unwrap(),
"",
).unwrap();
2019-10-14 16:55:14 -07:00
let javascript_config = HighlightConfiguration::new(
2019-10-14 16:55:14 -07:00
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(),
).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 {
2019-10-14 16:55:14 -07:00
match event? {
HighlightEvent::Source {start, end} => {
eprintln!("source: {}-{}", start, end);
2019-02-19 17:56:46 -08:00
},
2019-10-14 16:55:14 -07:00
HighlightEvent::HighlightStart(s) {
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
},
}
}
```
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).