| CARVIEW |
Select Language
HTTP/2 302
server: nginx/1.14.0 (Ubuntu)
date: Fri, 16 Jan 2026 14:13:28 GMT
content-security-policy: default-src 'none'; base-uri 'none'; img-src 'self' https:
location: /tree-sitter-highlight/latest/tree_sitter_highlight/
cache-control: max-age=0
x-cache: MISS
x-cache-hits: 0
age: 2
accept-ranges: bytes
strict-transport-security: max-age=31557600
x-served-by: cache-bom-vanm7210080-BOM
content-length: 0
HTTP/2 200
server: nginx/1.14.0 (Ubuntu)
date: Fri, 16 Jan 2026 14:13:29 GMT
content-type: text/html; charset=utf-8
content-encoding: gzip
x-cache-hits: 0
vary: Accept-Encoding
etag: W/"42af28c6e879e0f85a4999f3c8119a2a"
cache-control: max-age=0
x-cache: MISS
age: 1
accept-ranges: bytes
strict-transport-security: max-age=31557600
x-served-by: cache-bom-vanm7210045-BOM
tree_sitter_highlight - Rust
Crate tree_sitter_highlight
Expand description
§Tree-sitter Highlight
§Usage
Add this crate, and the language-specific crates for whichever languages you want
to parse, to your Cargo.toml:
[dependencies]
tree-sitter-highlight = "0.25.4"
tree-sitter-javascript = "0.23.1"Define the list of highlight names that you will recognize:
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",
];Create a highlighter. You need one of these for each thread that you’re using for syntax highlighting:
use tree_sitter_highlight::Highlighter;
let mut highlighter = Highlighter::new();Load some highlighting queries from the queries directory of the language repository:
use tree_sitter_highlight::HighlightConfiguration;
let javascript_language = tree_sitter_javascript::LANGUAGE.into();
let mut javascript_config = HighlightConfiguration::new(
javascript_language,
"javascript",
tree_sitter_javascript::HIGHLIGHT_QUERY,
tree_sitter_javascript::INJECTIONS_QUERY,
tree_sitter_javascript::LOCALS_QUERY,
).unwrap();Configure the recognized names:
javascript_config.configure(&highlight_names);Highlight some code:
use tree_sitter_highlight::HighlightEvent;
let highlights = highlighter.highlight(
&javascript_config,
b"const x = new Y();",
None,
|_| None
).unwrap();
for event in highlights {
match event.unwrap() {
HighlightEvent::Source {start, end} => {
eprintln!("source: {start}-{end}");
},
HighlightEvent::HighlightStart(s) => {
eprintln!("highlight style started: {s:?}");
},
HighlightEvent::HighlightEnd => {
eprintln!("highlight style ended");
},
}
}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).
Re-exports§
pub use c_lib as c;
Modules§
Structs§
- Highlight
- Indicates which highlight should be applied to a region of source code.
- Highlight
Configuration - Contains the data needed to highlight code written in a particular language.
- Highlighter
- Performs syntax highlighting, recognizing a given list of highlight names.
- Html
Renderer - Converts a general-purpose syntax highlighting iterator into a sequence of lines of HTML.
- _Query
Captures
Enums§
- Error
- Represents the reason why syntax highlighting failed.
- Highlight
Event - Represents a single step in rendering a syntax-highlighted document.