Tree-sitter includes a powerful syntax highlighting system via theDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/tree-sitter/tree-sitter/llms.txt
Use this file to discover all available pages before exploring further.
tree-sitter-highlight library, which is used on GitHub.com for highlighting code in multiple languages.
Overview
The syntax highlighting system requires three types of files:- Per-user configuration -
~/.config/tree-sitter/config.json - Language configuration -
tree-sitter.jsonin grammar repositories - Query files - Located in the
queries/folder
All configuration files use the
.scm extension, which stands for Scheme, reflecting the Lisp-like syntax used in Tree-sitter queries.Language Configuration
Thetree-sitter.json file contains metadata about the parser and language:
Basic Information
scope(required) - Language identifier like"source.js"matching TextMate grammar conventionspath(optional) - Relative path to the parser’ssrc/directory (default:".")external-files(optional) - Files to check for modifications during recompilation
Language Detection
file-types- Array of filename suffixes (e.g.,[".js", ".jsx"])first-line-regex- Regex tested against the first line of a filecontent-regex- Regex tested against file contents for disambiguationinjection-regex- Regex for matching language injection sites
Query Paths
highlights- Path to highlight query (default:queries/highlights.scm)locals- Path to local variable query (default:queries/locals.scm)injections- Path to injection query (default:queries/injections.scm)
Highlight Queries
Highlight queries assign highlight names to syntax nodes using captures:Standard Highlight Names
Common highlight names include:keyword- Language keywordsfunction/function.builtin- Function namestype/type.builtin- Type identifiersproperty/property.builtin- Object propertiesstring/string.escape- String literalsnumber- Numeric literalscomment/comment.documentation- Commentsvariable/variable.parameter- Variablesoperator- Operatorspunctuation.bracket/punctuation.delimiter- Punctuation
Theme Configuration
Map highlight names to colors in your config:Local Variables
The local variables query tracks scopes and variables to ensure consistent coloring:Special Capture Names
@local.scope- Indicates a node that introduces a new local scope@local.definition- Marks the name of a definition in the current scope@local.reference- Marks a name that may refer to an earlier definition
Excluding Local Variables
Use the(#is-not? local) predicate to disable highlighting for local variables:
Ignoring Nodes
Use@ignore to exclude specific nodes from tagging:
Language Injection
Language injection allows parsing code in multiple languages within a single file.Injection Captures
@injection.content- Node whose contents should be re-parsed in another language@injection.language- Node containing the language name to use
Injection Properties
injection.language- Hard-code a specific language nameinjection.combined- Parse all matching nodes as one nested documentinjection.include-children- Include child nodes’ text in the injectioninjection.self- Parse using the same language as the nodeinjection.parent- Parse using the parent language
Example: Ruby Heredoc
Implementation Details
Thetree-sitter-highlight crate provides the core functionality:
Key Types
HighlightConfiguration- Immutable, thread-safe language configurationHighlighter- Reusable highlighter instance with parser and query cursorsHighlightEvent- Represents a highlighting step:Source { start, end }- Plain source code rangeHighlightStart(Highlight)- Begin highlighted regionHighlightEnd- End highlighted region
Performance Optimizations
Cancellation Support
Highlighting can be cancelled using an atomic flag:CANCELLATION_CHECK_INTERVAL).
Unit Testing
Tree-sitter provides a built-in testing system for highlight queries:Test Syntax
- Caret (
^) - Tests the column at the caret position on the previous non-test line - Arrow (
<-) - Tests at the same column as the comment character - Negation (
!) - Asserts the scope does NOT match (e.g.,!keyword)
HTML Rendering
TheHtmlRenderer converts highlight events to HTML:
The HTML buffer pre-allocates 10KB (
BUFFER_HTML_RESERVE_CAPACITY) and the lines buffer pre-allocates space for 1000 lines (BUFFER_LINES_RESERVE_CAPACITY) to minimize reallocations.Command-Line Usage
Related Resources
Code Navigation
Learn about tagging and code navigation
Query Syntax
Deep dive into Tree-sitter query language