The Query API provides functions for creating queries from S-expression patterns and executing them on syntax trees to find matching nodes.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/tree-sitter/tree-sitter/llms.txt
Use this file to discover all available pages before exploring further.
Creating and deleting queries
ts_query_new
Create a new query from a string containing one or more S-expression patterns.The language for this query
The query source code
Length of the source code in bytes
Output parameter for error byte offset
Output parameter for error type
NULL if a pattern is invalid.
The query is associated with a particular language and can only be run on syntax nodes parsed with that language.
If all patterns are valid, this returns a TSQuery. If a pattern is invalid, this returns NULL and provides:
- The byte offset of the error written to
error_offset - The type of error written to
error_type
ts_query_delete
Delete a query, freeing all of the memory that it used.The query to delete
Query information
ts_query_pattern_count
Get the number of patterns in the query.The query instance
ts_query_capture_count
Get the number of captures in the query.The query instance
ts_query_string_count
Get the number of string literals in the query.The query instance
ts_query_start_byte_for_pattern
Get the byte offset where the given pattern starts in the query’s source.The query instance
The pattern index
ts_query_end_byte_for_pattern
Get the byte offset where the given pattern ends in the query’s source.The query instance
The pattern index
ts_query_predicates_for_pattern
Get all of the predicates for the given pattern in the query.The query instance
The pattern index
Output parameter for the number of steps
TSQueryPredicateStepTypeCapture- Steps representing capture names. Usets_query_capture_name_for_idto get the name.TSQueryPredicateStepTypeString- Steps representing literal strings. Usets_query_string_value_for_idto get the value.TSQueryPredicateStepTypeDone- Sentinel steps marking the end of individual predicates.
ts_query_is_pattern_rooted
Check if the given pattern in the query has a single root node.The query instance
The pattern index
true if the pattern has a single root node.
ts_query_is_pattern_non_local
Check if the given pattern in the query is non-local.The query instance
The pattern index
true if the pattern is non-local.
A non-local pattern has multiple root nodes and can match within a repeating sequence of nodes, as specified by the grammar. Non-local patterns disable certain optimizations that would otherwise be possible when executing a query on a specific range of a syntax tree.
ts_query_is_pattern_guaranteed_at_step
Check if a given pattern is guaranteed to match once a given step is reached.The query instance
The byte offset of the step in the query’s source
true if the pattern is guaranteed to match.
Captures and strings
ts_query_capture_name_for_id
Get the name and length of one of the query’s captures.The query instance
The capture ID
Output parameter for the name length
ts_query_capture_quantifier_for_id
Get the quantifier of one of the query’s captures.The query instance
The pattern index
The capture index
TSQuantifierZero, TSQuantifierZeroOrOne, TSQuantifierZeroOrMore, TSQuantifierOne, or TSQuantifierOneOrMore.
ts_query_string_value_for_id
Get the string value for one of the query’s string literals.The query instance
The string ID
Output parameter for the string length
Disabling patterns and captures
ts_query_disable_capture
Disable a certain capture within a query.The query instance
The capture name to disable
Length of the name
ts_query_disable_pattern
Disable a certain pattern within a query.The query instance
The pattern index to disable
Query cursors
ts_query_cursor_new
Create a new cursor for executing a given query.- Call
ts_query_cursor_execto start running a query on a syntax node - Repeatedly call
ts_query_cursor_next_matchto iterate over matches, or callts_query_cursor_next_captureto iterate over individual captures
ts_query_cursor_exec again to run another query.
Example:
ts_query_cursor_delete
Delete a query cursor, freeing all of the memory that it used.The cursor to delete
ts_query_cursor_exec
Start running a given query on a given node.The cursor instance
The query to execute
The node to search within
ts_query_cursor_exec_with_options
Start running a given query on a given node, with some options.The cursor instance
The query to execute
The node to search within
Options with progress callback
Match limits
ts_query_cursor_did_exceed_match_limit
Check if the query cursor exceeded its match limit.The cursor instance
true if the match limit was exceeded.
ts_query_cursor_match_limit
Get the current match limit.The cursor instance
ts_query_cursor_set_match_limit
Set the maximum number of in-progress matches allowed.The cursor instance
The new match limit
Range restrictions
ts_query_cursor_set_byte_range
Set the range of bytes in which the query will be executed.The cursor instance
The start byte offset
The end byte offset
false if start byte is greater than end byte, otherwise true.
The query cursor will return matches that intersect with the given range. This means that a match may be returned even if some of its captures fall outside the specified range, as long as at least part of the match overlaps with the range.
ts_query_cursor_set_point_range
Set the range of positions in which the query will be executed.The cursor instance
The start position
The end position
false if start point is greater than end point, otherwise true.
ts_query_cursor_set_containing_byte_range
Set the byte range within which all matches must be fully contained.The cursor instance
The start byte offset
The end byte offset
false if start byte is greater than end byte, otherwise true.
In contrast to ts_query_cursor_set_byte_range, this restricts the query cursor to only return matches where all nodes are fully contained within the given range. Both functions can be used together.
ts_query_cursor_set_containing_point_range
Set the point range within which all matches must be fully contained.The cursor instance
The start position
The end position
false if start point is greater than end point, otherwise true.
Iterating over results
ts_query_cursor_next_match
Advance to the next match of the currently running query.The cursor instance
Output parameter for the match
true if there is a match, false otherwise.
If there is a match, it is written to the match parameter. Each match contains the index of the pattern that matched, and an array of captures.
ts_query_cursor_remove_match
Remove a match from the query cursor’s list of matches.The cursor instance
The ID of the match to remove
ts_query_cursor_next_capture
Advance to the next capture of the currently running query.The cursor instance
Output parameter for the match containing the capture
Output parameter for the capture’s index within the match
true if there is a capture, false otherwise.
If there is a capture, its match is written to the match parameter and its index within the match’s capture list is written to capture_index. This is useful if you don’t care about which pattern matched, and just want a single ordered sequence of captures.
ts_query_cursor_set_max_start_depth
Set the maximum start depth for a query cursor.The cursor instance
The maximum depth, or
UINT32_MAX to remove the limit