docs: note zero point unbounded behavior in query functions

This commit is contained in:
Will Lillis 2026-04-30 02:55:27 -04:00
parent b7964b9b19
commit 7c3d842519
3 changed files with 41 additions and 5 deletions

View file

@ -59,3 +59,27 @@ bool ts_query_cursor_next_match(TSQueryCursor *, TSQueryMatch *match);
This function will return `false` when there are no more matches. Otherwise, it will populate the `match` with data about
which pattern matched and which nodes were captured.
## Restricting the Query Range
You can restrict the range in which the query is executed using byte offsets or point (row, column) positions:
```c
bool ts_query_cursor_set_byte_range(TSQueryCursor *self, uint32_t start_byte, uint32_t end_byte);
bool ts_query_cursor_set_point_range(TSQueryCursor *self, TSPoint start_point, TSPoint end_point);
```
These functions return matches that *intersect* with the given range. A match may be returned even if only part of it overlaps
with the range.
There are also "containing" variants that only return matches where all captured nodes are fully within the range:
```c
bool ts_query_cursor_set_containing_byte_range(TSQueryCursor *self, uint32_t start_byte, uint32_t end_byte);
bool ts_query_cursor_set_containing_point_range(TSQueryCursor *self, TSPoint start_point, TSPoint end_point);
```
```admonish note
For all of these functions, an end value of zero is treated as unbounded (the maximum possible value).
This means passing a byte range of `(0, 0)` (or a point range of `{0, 0}, {0, 0}`) will match the entire tree, not an empty range.
```

View file

@ -698,7 +698,7 @@ extern "C" {
pub fn ts_query_cursor_set_match_limit(self_: *mut TSQueryCursor, limit: u32);
}
extern "C" {
#[doc = " Set the range of bytes in which the query will be executed.\n\n The query cursor will return matches that intersect with the given point range.\n This means that a match may be returned even if some of its captures fall\n outside the specified range, as long as at least part of the match\n overlaps with the range.\n\n For example, if a query pattern matches a node that spans a larger area\n than the specified range, but part of that node intersects with the range,\n the entire match will be returned.\n\n This will return `false` if the start byte is greater than the end byte, otherwise\n it will return `true`."]
#[doc = " Set the range of bytes in which the query will be executed.\n\n The query cursor will return matches that intersect with the given byte range.\n This means that a match may be returned even if some of its captures fall\n outside the specified range, as long as at least part of the match\n overlaps with the range.\n\n For example, if a query pattern matches a node that spans a larger area\n than the specified range, but part of that node intersects with the range,\n the entire match will be returned.\n\n NOTE: An `end_byte` of zero is interpreted as `UINT32_MAX`, making the range\n unbounded.\n\n This will return `false` if the start byte is greater than the end byte, otherwise\n it will return `true`."]
pub fn ts_query_cursor_set_byte_range(
self_: *mut TSQueryCursor,
start_byte: u32,
@ -706,7 +706,7 @@ extern "C" {
) -> bool;
}
extern "C" {
#[doc = " Set the range of (row, column) positions in which the query will be executed.\n\n The query cursor will return matches that intersect with the given point range.\n This means that a match may be returned even if some of its captures fall\n outside the specified range, as long as at least part of the match\n overlaps with the range.\n\n For example, if a query pattern matches a node that spans a larger area\n than the specified range, but part of that node intersects with the range,\n the entire match will be returned.\n\n This will return `false` if the start point is greater than the end point, otherwise\n it will return `true`."]
#[doc = " Set the range of (row, column) positions in which the query will be executed.\n\n The query cursor will return matches that intersect with the given point range.\n This means that a match may be returned even if some of its captures fall\n outside the specified range, as long as at least part of the match\n overlaps with the range.\n\n For example, if a query pattern matches a node that spans a larger area\n than the specified range, but part of that node intersects with the range,\n the entire match will be returned.\n\n NOTE: An `end_point` of `(0, 0)` is interpreted as `POINT_MAX`, making the\n range unbounded.\n\n This will return `false` if the start point is greater than the end point, otherwise\n it will return `true`."]
pub fn ts_query_cursor_set_point_range(
self_: *mut TSQueryCursor,
start_point: TSPoint,
@ -714,7 +714,7 @@ extern "C" {
) -> bool;
}
extern "C" {
#[doc = " Set the byte range within which all matches must be fully contained.\n\n Set the range of bytes in which matches will be searched for. In contrast to\n `ts_query_cursor_set_byte_range`, this will restrict the query cursor to only return\n matches where _all_ nodes are _fully_ contained within the given range. Both functions\n can be used together, e.g. to search for any matches that intersect line 5000, as\n long as they are fully contained within lines 4500-5500"]
#[doc = " Set the byte range within which all matches must be fully contained.\n\n Set the range of bytes in which matches will be searched for. In contrast to\n `ts_query_cursor_set_byte_range`, this will restrict the query cursor to only return\n matches where _all_ nodes are _fully_ contained within the given range. Both functions\n can be used together, e.g. to search for any matches that intersect line 5000, as\n long as they are fully contained within lines 4500-5500\n\n NOTE: An `end_byte` of zero is interpreted as `UINT32_MAX`, making the range\n unbounded."]
pub fn ts_query_cursor_set_containing_byte_range(
self_: *mut TSQueryCursor,
start_byte: u32,
@ -722,7 +722,7 @@ extern "C" {
) -> bool;
}
extern "C" {
#[doc = " Set the point range within which all matches must be fully contained.\n\n Set the range of bytes in which matches will be searched for. In contrast to\n `ts_query_cursor_set_point_range`, this will restrict the query cursor to only return\n matches where _all_ nodes are _fully_ contained within the given range. Both functions\n can be used together, e.g. to search for any matches that intersect line 5000, as\n long as they are fully contained within lines 4500-5500"]
#[doc = " Set the point range within which all matches must be fully contained.\n\n Set the range of bytes in which matches will be searched for. In contrast to\n `ts_query_cursor_set_point_range`, this will restrict the query cursor to only return\n matches where _all_ nodes are _fully_ contained within the given range. Both functions\n can be used together, e.g. to search for any matches that intersect line 5000, as\n long as they are fully contained within lines 4500-5500\n\n NOTE: An `end_point` of `(0, 0)` is interpreted as `POINT_MAX`, making the\n range unbounded."]
pub fn ts_query_cursor_set_containing_point_range(
self_: *mut TSQueryCursor,
start_point: TSPoint,

View file

@ -1089,7 +1089,7 @@ void ts_query_cursor_set_match_limit(TSQueryCursor *self, uint32_t limit);
/**
* Set the range of bytes in which the query will be executed.
*
* The query cursor will return matches that intersect with the given point range.
* 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 the range.
@ -1098,6 +1098,9 @@ void ts_query_cursor_set_match_limit(TSQueryCursor *self, uint32_t limit);
* than the specified range, but part of that node intersects with the range,
* the entire match will be returned.
*
* NOTE: An `end_byte` of zero is interpreted as `UINT32_MAX`, making the range
* unbounded.
*
* This will return `false` if the start byte is greater than the end byte, otherwise
* it will return `true`.
*/
@ -1115,6 +1118,9 @@ bool ts_query_cursor_set_byte_range(TSQueryCursor *self, uint32_t start_byte, ui
* than the specified range, but part of that node intersects with the range,
* the entire match will be returned.
*
* NOTE: An `end_point` of `(0, 0)` is interpreted as `POINT_MAX`, making the
* range unbounded.
*
* This will return `false` if the start point is greater than the end point, otherwise
* it will return `true`.
*/
@ -1128,6 +1134,9 @@ bool ts_query_cursor_set_point_range(TSQueryCursor *self, TSPoint start_point, T
* matches where _all_ nodes are _fully_ contained within the given range. Both functions
* can be used together, e.g. to search for any matches that intersect line 5000, as
* long as they are fully contained within lines 4500-5500
*
* NOTE: An `end_byte` of zero is interpreted as `UINT32_MAX`, making the range
* unbounded.
*/
bool ts_query_cursor_set_containing_byte_range(TSQueryCursor *self, uint32_t start_byte, uint32_t end_byte);
@ -1139,6 +1148,9 @@ bool ts_query_cursor_set_containing_byte_range(TSQueryCursor *self, uint32_t sta
* matches where _all_ nodes are _fully_ contained within the given range. Both functions
* can be used together, e.g. to search for any matches that intersect line 5000, as
* long as they are fully contained within lines 4500-5500
*
* NOTE: An `end_point` of `(0, 0)` is interpreted as `POINT_MAX`, making the
* range unbounded.
*/
bool ts_query_cursor_set_containing_point_range(TSQueryCursor *self, TSPoint start_point, TSPoint end_point);