fix(lib): check point, byte ranges in ts_query_cursor_set

range functions
This commit is contained in:
WillLillis 2024-11-02 00:44:58 -04:00 committed by Amaan Qureshi
parent 9d86cb2c20
commit 5b5cf5a5e5
6 changed files with 45 additions and 11 deletions

View file

@ -1996,6 +1996,7 @@ fn test_query_matches_within_byte_range() {
]
);
// An end byte of zero indicates there is no end
let matches =
cursor
.set_byte_range(12..0)

View file

@ -708,19 +708,20 @@ extern "C" {
pub fn ts_query_cursor_timeout_micros(self_: *const TSQueryCursor) -> u64;
}
extern "C" {
#[doc = " Set the range of bytes or (row, column) positions in which the query\n will be executed."]
#[doc = " Set the range of bytes in which the query will be executed.\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,
end_byte: u32,
);
) -> bool;
}
extern "C" {
#[doc = " Set the range of (row, column) positions in which the query will be executed.\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,
end_point: TSPoint,
);
) -> bool;
}
extern "C" {
#[doc = " Advance to the next match of the currently running query.\n\n If there is a match, write it to `*match` and return `true`.\n Otherwise, return `false`."]
@ -731,7 +732,7 @@ extern "C" {
pub fn ts_query_cursor_remove_match(self_: *mut TSQueryCursor, match_id: u32);
}
extern "C" {
#[doc = " Advance to the next capture of the currently running query.\n\n If there is a capture, write its match to `*match` and its index within\n the matche's capture list to `*capture_index`. Otherwise, return `false`."]
#[doc = " Advance to the next capture of the currently running query.\n\n If there is a capture, write its match to `*match` and its index within\n the match's capture list to `*capture_index`. Otherwise, return `false`."]
pub fn ts_query_cursor_next_capture(
self_: *mut TSQueryCursor,
match_: *mut TSQueryMatch,

View file

@ -1280,6 +1280,13 @@ class Query {
if (typeof matchLimit !== 'number') {
throw new Error('Arguments must be numbers');
}
if (endIndex != 0 && startIndex > endIndex) {
throw new Error('`startIndex` cannot be greater than `endIndex`');
}
if (endPosition != ZERO_POINT && (startPosition.row > endPosition.row ||
(startPosition.row == endPosition.row && startPosition.column > endPosition.row))) {
throw new Error('`startPosition` cannot be greater than `endPosition`');
}
marshalNode(node);
@ -1345,6 +1352,13 @@ class Query {
if (typeof matchLimit !== 'number') {
throw new Error('Arguments must be numbers');
}
if (endIndex != 0 && startIndex > endIndex) {
throw new Error('`startIndex` cannot be greater than `endIndex`');
}
if (endPosition != ZERO_POINT && (startPosition.row > endPosition.row ||
(startPosition.row == endPosition.row && startPosition.column > endPosition.row))) {
throw new Error('`startPosition` cannot be greater than `endPosition`');
}
marshalNode(node);

View file

@ -1089,11 +1089,20 @@ void ts_query_cursor_set_timeout_micros(TSQueryCursor *self, uint64_t timeout_mi
uint64_t ts_query_cursor_timeout_micros(const TSQueryCursor *self);
/**
* Set the range of bytes or (row, column) positions in which the query
* will be executed.
* Set the range of bytes in which the query will be executed.
*
* This will return `false` if the start byte is greater than the end byte, otherwise
* it will return `true`.
*/
void ts_query_cursor_set_byte_range(TSQueryCursor *self, uint32_t start_byte, uint32_t end_byte);
void ts_query_cursor_set_point_range(TSQueryCursor *self, TSPoint start_point, TSPoint end_point);
bool ts_query_cursor_set_byte_range(TSQueryCursor *self, uint32_t start_byte, uint32_t end_byte);
/**
* Set the range of (row, column) positions in which the query will be executed.
*
* This will return `false` if the start point is greater than the end point, otherwise
* it will return `true`.
*/
bool ts_query_cursor_set_point_range(TSQueryCursor *self, TSPoint start_point, TSPoint end_point);
/**
* Advance to the next match of the currently running query.
@ -1108,7 +1117,7 @@ void ts_query_cursor_remove_match(TSQueryCursor *self, uint32_t match_id);
* Advance to the next capture of the currently running query.
*
* If there is a capture, write its match to `*match` and its index within
* the matche's capture list to `*capture_index`. Otherwise, return `false`.
* the match's capture list to `*capture_index`. Otherwise, return `false`.
*/
bool ts_query_cursor_next_capture(
TSQueryCursor *self,

View file

@ -1,4 +1,5 @@
#include <stdbool.h>
#include "./point.h"
#include "./subtree.h"
#include "./tree.h"
#include "./language.h"

View file

@ -3103,7 +3103,7 @@ void ts_query_cursor_exec_with_options(
}
}
void ts_query_cursor_set_byte_range(
bool ts_query_cursor_set_byte_range(
TSQueryCursor *self,
uint32_t start_byte,
uint32_t end_byte
@ -3111,11 +3111,15 @@ void ts_query_cursor_set_byte_range(
if (end_byte == 0) {
end_byte = UINT32_MAX;
}
if (start_byte > end_byte) {
return false;
}
self->start_byte = start_byte;
self->end_byte = end_byte;
return true;
}
void ts_query_cursor_set_point_range(
bool ts_query_cursor_set_point_range(
TSQueryCursor *self,
TSPoint start_point,
TSPoint end_point
@ -3123,8 +3127,12 @@ void ts_query_cursor_set_point_range(
if (end_point.row == 0 && end_point.column == 0) {
end_point = POINT_MAX;
}
if (point_gt(start_point, end_point)) {
return false;
}
self->start_point = start_point;
self->end_point = end_point;
return true;
}
// Search through all of the in-progress states, and find the captured