← LeaderboardThe Skill Leaderboard
rust-tree-sitter-queries
Use when a native Tree-sitter query captures nested syntax unexpectedly, a grammar update breaks a pattern, or Rust capture offsets corrupt UTF-8 text.
Try your first task
A source-grounded starter task has not been verified for this skill yet.
SKILL.md
name: rust-tree-sitter-queriesdescription: "Use when a native Tree-sitter query captures nested syntax unexpectedly, a grammar update breaks a pattern, or Rust capture offsets corrupt UTF-8 text."category: developmentrisk: criticalsource: selfsource_type: selfdate_added: "2026-12-01"author: ShianMiketags: [rust, tree-sitter, ast, queries, syntax-analysis, utf-8]Rust Tree-sitter Queries
When to Use
- A native Tree-sitter query includes nested declarations that should be outside its scope.
- A syntax indexer or refactoring tool must pair a declaration with its name and byte range.
- A query succeeds on ASCII inputs but extracts the wrong text in a UTF-8 file.
Prerequisites
- Pin and record both the Tree-sitter runtime and the grammar version. The worked example uses
tree-sitter0.27.2 andtree-sitter-rust0.24.2. - Have the same source bytes available for parsing, predicate evaluation, and capture extraction.
- Include positive and negative fixtures with the grammar contexts you intend to support.
1. Inspect the tree and express the context
- Parse representative source first and examine its node types and fields. Use the grammar's
node-types.jsonand parser output to confirm that a field exists for the intended node. - Use field constraints for roles such as
name: (identifier). Surround the target with the parent pattern when the parent is part of the contract. A function pattern run on the file root can still match anywhere below that root. - Compile the query with the exact grammar at setup. Report an invalid node type, field, or capture as a query error. Handle parse recovery separately by checking
root_node().has_error()or the error status of the region your tool will use.
(source_file
(function_item
name: (identifier) @name) @definition)
2. Preserve match and source boundaries
- Use
QueryCursor::matcheswhen captures belong together. Resolve captures by name and consume each match before advancing the streaming iterator. In the tested binding usehit.captures()and importStreamingIterator. - Use
node.utf8_text(source.as_bytes())or slice the matching input bynode.byte_range(). Byte offsets are different from character counts and from UTF-16 positions used by some clients. Convert at the boundary with the original source available. - If narrowing a scan to a range, distinguish intersecting from contained matches. A byte-range restriction can still return a match with captures outside the requested interval. Use a containing range or verify capture ranges explicitly when containment is required.
3. Verify before consuming or editing captures
- Test the same construct at the file level, inside a body, and inside comments and strings. Assert the selected names and their relationship to the owning declaration.
- Pass UTF-8 and CRLF examples through the same parser and query. Check byte offsets and captured text against the original bytes.
- If results drive source edits, reject out-of-scope or ambiguous spans and handle overlaps before writing. Reparse and test the resulting source.
Example
Run the top-level capture fixture. It contrasts an unrestricted query with one matching declarations directly under the file root.
Limitations
- The query covers file-level Rust functions. Module bodies, methods, macros, name resolution, and language-specific predicate behavior need separate handling.
- Tree-sitter can return a tree for invalid source. A capture alone does not establish that the resulting program compiles.
Safety and Side Effects
- The fixture parses synthetic source in memory. Cargo writes local build output and a scratch lockfile.
- Query changes can affect downstream indexing and edits. Review the actual matches and spans before changing source files.
Related Skills and Documentation
- For the ast-grep CLI and YAML codemods, see ast-grep Codemods.
- For Rust project and Cargo setup, see Systems Programming Rust Project.
- Tree-sitter documents query syntax, the Rust query cursor, and the Rust grammar binding.