The Query API allows you to search syntax trees using pattern matching. Queries are written in a special S-expression syntax and can capture specific nodes that match certain patterns.
Query
A Query is a set of patterns that match nodes in a syntax tree.
new
Create a new query from a string containing one or more S-expression patterns. The query is associated with a particular language, and can only be run on syntax nodes parsed with that language.
The language to associate with the query
The query source code in S-expression syntax
pattern_count
Get the number of patterns in the query.
start_byte_for_pattern
Get the byte offset where the given pattern starts in the query’s source.
end_byte_for_pattern
Get the byte offset where the given pattern ends in the query’s source.
is_pattern_rooted
Check if a given pattern within a query has a single root node.
is_pattern_non_local
Check if a given pattern is non-local (can match nodes at any depth).
Capture Management
capture_names
Get the names of the captures used in the query.
capture_index_for_name
Get the index for a given capture name.
The name of the capture (without the @ prefix)
capture_quantifiers
Get the quantifiers of the captures for a given pattern.
Predicates
property_predicates
Get the properties that are checked for the given pattern index. This includes predicates with the operators is? and is-not?.
property_settings
Get the properties that are set for the given pattern index. This includes predicates with the operator set!.
general_predicates
Get the other user-defined predicates associated with the given index. This includes predicates with operators other than match?, eq?, not-eq?, is?, is-not?, and set!.
Pattern Control
disable_capture
Disable a certain capture within a query. This prevents the capture from being returned in matches, and also avoids any resource usage associated with recording the capture.
The name of the capture to disable
disable_pattern
Disable a certain pattern within a query. This prevents the pattern from matching, and also avoids any resource usage associated with the pattern.
The index of the pattern to disable
QueryCursor
A QueryCursor is a stateful object for executing a Query on a syntax tree.
new
Create a new cursor for executing a query. The cursor stores the state that is needed to iteratively search for matches.
Execution
matches
Iterate over all of the matches in the order that they were found. Requires the StreamingIterator trait to be in scope.
The node to search within
A provider for accessing the source text
captures
Iterate over all of the individual captures in the order that they appear. This is useful if you don’t care about which pattern matched, and just want a single, ordered sequence of captures.
Range Configuration
set_byte_range
Set the range in which the query will be executed, in terms of byte offsets.
The byte range to search within
set_point_range
Set the range in which the query will be executed, in terms of rows and columns.
The point range to search within
set_containing_byte_range
Set the byte range within which all matches must be fully contained. This will restrict the query cursor to only return matches where all nodes are fully contained within the given range.
set_containing_point_range
Set the point range within which all matches must be fully contained.
set_max_start_depth
Set the maximum start depth for a query cursor. This prevents cursors from exploring children nodes at a certain depth. Set to None to remove the maximum start depth.
The maximum depth, or None to disable
Match Limits
match_limit
Return the maximum number of in-progress matches for this cursor.
set_match_limit
Set the maximum number of in-progress matches for this cursor. The limit must be greater than 0 and less than or equal to 65536.
The maximum number of in-progress matches
did_exceed_match_limit
Check if, on its last execution, this cursor exceeded its maximum number of in-progress matches.
QueryMatch
A match of a query to a particular set of nodes.
The index of the pattern that matched
The list of captures in this match
Get the unique id for this match.
remove
Remove this match from the query cursor’s results.
nodes_for_capture_index
Get all nodes for a given capture index within this match.
QueryCapture
A particular node that has been captured with a particular name within a query.
The index of the capture (use Query::capture_names() to get the name)
Supporting Types
CaptureQuantifier
Quantifiers for captures.
QueryProperty
A key-value pair associated with a particular pattern in a query.
QueryPredicate
A user-defined predicate associated with a pattern.
QueryPredicateArg
An argument to a query predicate.
Examples
Basic Query
Query with Predicates
Filtering by Range
Using Captures Iterator