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 inqueries/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:
# and leading whitespace from Ruby comments.
#select-adjacent!
Filters text to only nodes adjacent to another capture:
TagsConfiguration Structure
Thetree-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
UseTagsContext to generate tags from source code:
Local Scope Tracking
Tags can track local scopes to distinguish between local and non-local names:(#is-not? local) predicate to exclude local variables:
Command-Line Usage
Test tag queries with thetree-sitter tags command:
Example Output
Unit Testing
Tag queries can be tested using comment assertions: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:CANCELLATION_CHECK_INTERVAL).
Reusing Context
ReuseTagsContext instances to avoid parser reallocation:
Integration Patterns
Language Server Protocol
Symbol Search
Error Handling
The tags system can fail with these errors:Related Resources
Syntax Highlighting
Learn about Tree-sitter’s highlighting system
Query Language
Master the query language syntax