Skip to main content
Tree-sitter’s tagging system enables code navigation features like “go to definition”, symbol search, and outline views. It’s used in GitHub’s search-based code navigation and can be integrated into language servers and IDEs.

Tagging Concepts

Tagging identifies entities that can be named in a program using Tree-sitter queries. Each tag contains:
  • Role - Whether the entity is a definition or reference
  • Kind - How the entity is used (class, function, variable, etc.)
  • Name - The identifier text
  • Documentation - Optional docstring

Capture Convention

Tags use a @role.kind naming convention:

Standard Tag Vocabulary

Applications can extend this vocabulary or recognize only a subset based on their needs.

Writing Tag Queries

Tag queries are stored in queries/tags.scm:

Basic Example (Python)

Advanced Example (JavaScript)

Documentation Capture

Capture docstrings using the @doc capture:

Built-in Functions

Two special functions help process docstrings:

#strip!

Removes patterns from captured text:
This removes # and leading whitespace from Ruby comments.

#select-adjacent!

Filters text to only nodes adjacent to another capture:
This ensures only comments directly above the class are included.

TagsConfiguration Structure

The tree-sitter-tags crate provides the core implementation:

Creating a Configuration

The locals query is concatenated with the tags query to support local scope tracking.

Tag Structure

Each tag contains comprehensive location information:

Generating Tags

Use TagsContext to generate tags from source code:

Local Scope Tracking

Tags can track local scopes to distinguish between local and non-local names:
Use the (#is-not? local) predicate to exclude local variables:

Command-Line Usage

Test tag queries with the tree-sitter tags command:

Example Output

Unit Testing

Tag queries can be tested using comment assertions:
Run tests with:

Performance Considerations

Line Length Limits

The tags system limits lines to 180 characters (MAX_LINE_LEN) to prevent excessive memory usage.

Cancellation Support

Long-running operations can be cancelled:
Checks occur every 100 iterations (CANCELLATION_CHECK_INTERVAL).

Reusing Context

Reuse TagsContext instances to avoid parser reallocation:

Integration Patterns

Language Server Protocol

Error Handling

The tags system can fail with these errors:
Only captures matching @definition.*, @reference.*, @doc, @name, or @local.* are valid. Other captures will return Error::InvalidCapture.

Syntax Highlighting

Learn about Tree-sitter’s highlighting system

Query Language

Master the query language syntax