Home / Market / rust tree sitter queries

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.

skill★ 508990 dlgithubCC-BY-4.0 content; MIT code; upstream exceptions applyInstall in CosmosUpstream Source

Documentation & Preview

--- name: rust-tree-sitter-queries description: "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: development risk: critical source: self source_type: self date_added: "2026-12-01" author: ShianMike tags: [rust, tree-sitter, ast, queries, syntax-analysis, utf-8]

---

Rust Tree-sitter Queries

When to Use

Prerequisites

1. Inspect the tree and express the context

  1. Parse representative source first and examine its node types and fields. Use the grammar's node-types.json and parser output to confirm that a field exists for the intended node.
  2. 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.
  3. 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

  1. Use QueryCursor::matches when captures belong together. Resolve captures by name and consume each match before advancing the streaming iterator. In the tested binding use hit.captures() and import StreamingIterator.
  2. Use node.utf8_text(source.as_bytes()) or slice the matching input by node.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.
  3. 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

  1. 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.
  2. Pass UTF-8 and CRLF examples through the same parser and query. Check byte offsets and captured text against the original bytes.
  3. 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

Safety and Side Effects

Related Skills and Documentation