Query#

class tree_sitter.QueryCursor(query, *, match_limit=None, timeout_micros=None)#

A class for executing a Query on a syntax Tree.

Added in version 0.25.0.

Methods#

captures(node, /, predicate=None, progress_callback=None)#

Get a list of captures within the given node.

Returns:

A dict where the keys are the names of the captures and the values are lists of the captured nodes.

Hint

This method returns all of thecaptures while matches() only returns the last match.

matches(node, /, predicate=None, progress_callback=None)#

Get a list of matches within the given node.

Returns:

A list of tuples where the first element is the pattern index and the second element is a dictionary that maps capture names to nodes.

set_byte_range(start, end)#

Set the range of bytes in which the query will be executed.

Raises:

ValueError – If the start byte exceeds the end byte.

Note

The query cursor will return matches that intersect with the given byte 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 it.

set_max_start_depth(max_start_depth)#

Set the maximum start depth for the query.

set_point_range(start, end)#

Set the range of points in which the query will be executed.

Raises:

ValueError – If the start point exceeds the end point.

Note

The query cursor will return matches that intersect with the given point 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 it.

Attributes#

did_exceed_match_limit#

Check if the query exceeded its maximum number of in-progress matches during its last execution.

match_limit#

The maximum number of in-progress matches.

timeout_micros#

The maximum duration in microseconds that query execution should be allowed to take before halting.