Add ts_tree_cursor_goto_first_child_for_point function

This function (and the similar `ts_tree_cursor_goto_first_child_for_byte`)
allows you to efficiently seek the tree cursor to a given position,
exploiting the tree's internal balancing, without having to visit
all of the preceding siblings of each node.
This commit is contained in:
Max Brunsfeld 2021-05-27 12:30:17 -07:00
parent 036aceed57
commit 919e9745a6
5 changed files with 166 additions and 6 deletions

View file

@ -586,12 +586,16 @@ extern "C" {
}
extern "C" {
#[doc = " Move the cursor to the first child of its current node that extends beyond"]
#[doc = " the given byte offset."]
#[doc = " the given byte offset or point."]
#[doc = ""]
#[doc = " This returns the index of the child node if one was found, and returns -1"]
#[doc = " if no such child was found."]
pub fn ts_tree_cursor_goto_first_child_for_byte(arg1: *mut TSTreeCursor, arg2: u32) -> i64;
}
extern "C" {
pub fn ts_tree_cursor_goto_first_child_for_point(arg1: *mut TSTreeCursor, arg2: TSPoint)
-> i64;
}
extern "C" {
pub fn ts_tree_cursor_copy(arg1: *const TSTreeCursor) -> TSTreeCursor;
}

View file

@ -1172,6 +1172,21 @@ impl<'a> TreeCursor<'a> {
}
}
/// Move this cursor to the first child of its current node that extends beyond
/// the given byte offset.
///
/// This returns the index of the child node if one was found, and returns `None`
/// if no such child was found.
pub fn goto_first_child_for_point(&mut self, point: Point) -> Option<usize> {
let result =
unsafe { ffi::ts_tree_cursor_goto_first_child_for_point(&mut self.0, point.into()) };
if result < 0 {
None
} else {
Some(result as usize)
}
}
/// Re-initialize this tree cursor to start at a different node.
pub fn reset(&mut self, node: Node<'a>) {
unsafe { ffi::ts_tree_cursor_reset(&mut self.0, node.0) };