Skip to content

Commit 95fd37e

Browse files
committed
docs: add information about the scoping of cursors
1 parent f473192 commit 95fd37e

File tree

3 files changed

+27
-6
lines changed

3 files changed

+27
-6
lines changed

lib/binding_rust/bindings.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ extern "C" {
507507
pub fn ts_node_eq(self_: TSNode, other: TSNode) -> bool;
508508
}
509509
extern "C" {
510-
#[doc = " Create a new tree cursor starting from the given node.\n\n A tree cursor allows you to walk a syntax tree more efficiently than is\n possible using the [`TSNode`] functions. It is a mutable object that is always\n on a certain syntax node, and can be moved imperatively to different nodes."]
510+
#[doc = " Create a new tree cursor starting from the given node.\n\n A tree cursor allows you to walk a syntax tree more efficiently than is\n possible using the [`TSNode`] functions. It is a mutable object that is always\n on a certain syntax node, and can be moved imperatively to different nodes.\n\n Note that the given node is considered the root of the cursor,\n and the cursor cannot walk outside this node."]
511511
pub fn ts_tree_cursor_new(node: TSNode) -> TSTreeCursor;
512512
}
513513
extern "C" {
@@ -537,15 +537,15 @@ extern "C" {
537537
pub fn ts_tree_cursor_current_field_id(self_: *const TSTreeCursor) -> TSFieldId;
538538
}
539539
extern "C" {
540-
#[doc = " Move the cursor to the parent of its current node.\n\n This returns `true` if the cursor successfully moved, and returns `false`\n if there was no parent node (the cursor was already on the root node)."]
540+
#[doc = " Move the cursor to the parent of its current node.\n\n This returns `true` if the cursor successfully moved, and returns `false`\n if there was no parent node (the cursor was already on the root node).\n\n Note that the node the cursor was constructed with is considered the root\n of the cursor, and the cursor cannot walk outside this node."]
541541
pub fn ts_tree_cursor_goto_parent(self_: *mut TSTreeCursor) -> bool;
542542
}
543543
extern "C" {
544-
#[doc = " Move the cursor to the next sibling of its current node.\n\n This returns `true` if the cursor successfully moved, and returns `false`\n if there was no next sibling node."]
544+
#[doc = " Move the cursor to the next sibling of its current node.\n\n This returns `true` if the cursor successfully moved, and returns `false`\n if there was no next sibling node.\n\n Note that the node the cursor was constructed with is considered the root\n of the cursor, and the cursor cannot walk outside this node."]
545545
pub fn ts_tree_cursor_goto_next_sibling(self_: *mut TSTreeCursor) -> bool;
546546
}
547547
extern "C" {
548-
#[doc = " Move the cursor to the previous sibling of its current node.\n\n This returns `true` if the cursor successfully moved, and returns `false` if\n there was no previous sibling node.\n\n Note, that this function may be slower than\n [`ts_tree_cursor_goto_next_sibling`] due to how node positions are stored. In\n the worst case, this will need to iterate through all the children up to the\n previous sibling node to recalculate its position."]
548+
#[doc = " Move the cursor to the previous sibling of its current node.\n\n This returns `true` if the cursor successfully moved, and returns `false` if\n there was no previous sibling node.\n\n Note, that this function may be slower than\n [`ts_tree_cursor_goto_next_sibling`] due to how node positions are stored. In\n the worst case, this will need to iterate through all the children up to the\n previous sibling node to recalculate its position. Also note that the node the cursor\n was constructed with is considered the root of the cursor, and the cursor cannot\n walk outside this node."]
549549
pub fn ts_tree_cursor_goto_previous_sibling(self_: *mut TSTreeCursor) -> bool;
550550
}
551551
extern "C" {

lib/binding_rust/lib.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2021,6 +2021,9 @@ impl<'tree> Node<'tree> {
20212021
}
20222022

20232023
/// Create a new [`TreeCursor`] starting from this node.
2024+
///
2025+
/// Note that the given node is considered the root of the cursor,
2026+
/// and the cursor cannot walk outside this node.
20242027
#[doc(alias = "ts_tree_cursor_new")]
20252028
#[must_use]
20262029
pub fn walk(&self) -> TreeCursor<'tree> {
@@ -2158,6 +2161,9 @@ impl<'cursor> TreeCursor<'cursor> {
21582161
/// This returns `true` if the cursor successfully moved, and returns
21592162
/// `false` if there was no parent node (the cursor was already on the
21602163
/// root node).
2164+
///
2165+
/// Note that the node the cursor was constructed with is considered the root
2166+
/// of the cursor, and the cursor cannot walk outside this node.
21612167
#[doc(alias = "ts_tree_cursor_goto_parent")]
21622168
pub fn goto_parent(&mut self) -> bool {
21632169
unsafe { ffi::ts_tree_cursor_goto_parent(&mut self.0) }
@@ -2167,6 +2173,9 @@ impl<'cursor> TreeCursor<'cursor> {
21672173
///
21682174
/// This returns `true` if the cursor successfully moved, and returns
21692175
/// `false` if there was no next sibling node.
2176+
///
2177+
/// Note that the node the cursor was constructed with is considered the root
2178+
/// of the cursor, and the cursor cannot walk outside this node.
21702179
#[doc(alias = "ts_tree_cursor_goto_next_sibling")]
21712180
pub fn goto_next_sibling(&mut self) -> bool {
21722181
unsafe { ffi::ts_tree_cursor_goto_next_sibling(&mut self.0) }
@@ -2189,7 +2198,8 @@ impl<'cursor> TreeCursor<'cursor> {
21892198
/// [`goto_next_sibling`](TreeCursor::goto_next_sibling) due to how node
21902199
/// positions are stored. In the worst case, this will need to iterate
21912200
/// through all the children up to the previous sibling node to recalculate
2192-
/// its position.
2201+
/// its position. Also note that the node the cursor was constructed with is
2202+
/// considered the root of the cursor, and the cursor cannot walk outside this node.
21932203
#[doc(alias = "ts_tree_cursor_goto_previous_sibling")]
21942204
pub fn goto_previous_sibling(&mut self) -> bool {
21952205
unsafe { ffi::ts_tree_cursor_goto_previous_sibling(&mut self.0) }

lib/include/tree_sitter/api.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,9 @@ bool ts_node_eq(TSNode self, TSNode other);
748748
* A tree cursor allows you to walk a syntax tree more efficiently than is
749749
* possible using the [`TSNode`] functions. It is a mutable object that is always
750750
* on a certain syntax node, and can be moved imperatively to different nodes.
751+
*
752+
* Note that the given node is considered the root of the cursor,
753+
* and the cursor cannot walk outside this node.
751754
*/
752755
TSTreeCursor ts_tree_cursor_new(TSNode node);
753756

@@ -796,6 +799,9 @@ TSFieldId ts_tree_cursor_current_field_id(const TSTreeCursor *self);
796799
*
797800
* This returns `true` if the cursor successfully moved, and returns `false`
798801
* if there was no parent node (the cursor was already on the root node).
802+
*
803+
* Note that the node the cursor was constructed with is considered the root
804+
* of the cursor, and the cursor cannot walk outside this node.
799805
*/
800806
bool ts_tree_cursor_goto_parent(TSTreeCursor *self);
801807

@@ -804,6 +810,9 @@ bool ts_tree_cursor_goto_parent(TSTreeCursor *self);
804810
*
805811
* This returns `true` if the cursor successfully moved, and returns `false`
806812
* if there was no next sibling node.
813+
*
814+
* Note that the node the cursor was constructed with is considered the root
815+
* of the cursor, and the cursor cannot walk outside this node.
807816
*/
808817
bool ts_tree_cursor_goto_next_sibling(TSTreeCursor *self);
809818

@@ -816,7 +825,9 @@ bool ts_tree_cursor_goto_next_sibling(TSTreeCursor *self);
816825
* Note, that this function may be slower than
817826
* [`ts_tree_cursor_goto_next_sibling`] due to how node positions are stored. In
818827
* the worst case, this will need to iterate through all the children up to the
819-
* previous sibling node to recalculate its position.
828+
* previous sibling node to recalculate its position. Also note that the node the cursor
829+
* was constructed with is considered the root of the cursor, and the cursor cannot
830+
* walk outside this node.
820831
*/
821832
bool ts_tree_cursor_goto_previous_sibling(TSTreeCursor *self);
822833

0 commit comments

Comments
 (0)