Class: TreeSitter::TreeCursor
- Inherits:
-
Object
- Object
- TreeSitter::TreeCursor
- Defined in:
- ext/tree_sitter/tree_cursor.c
Instance Method Summary collapse
-
#copy ⇒ Object
Class methods.
-
#current_depth ⇒ Integer
Get the depth of the cursor’s current node relative to the original node that the cursor was constructed with.
-
#current_descendant_index ⇒ Integer
Get the index of the cursor’s current node out of all of the descendants of the original node that the cursor was constructed with.
-
#current_field_id ⇒ Integer
Get the field id of the tree cursor’s current node.
-
#current_field_name ⇒ String
Get the field name of the tree cursor’s current node.
-
#current_node ⇒ Node
Get the tree cursor’s current node.
-
#goto_descendant(descendant_idx) ⇒ nil
Move the cursor to the node that is the nth descendant of the original node that the cursor was constructed with, where zero represents the original node itself.
-
#goto_first_child ⇒ Boolean
Move the cursor to the first child of its current node.
-
#goto_first_child_for_byte(byte) ⇒ Integer
Move the cursor to the first child of its current node that extends beyond the given byte offset.
-
#goto_first_child_for_point(point) ⇒ Integer
Move the cursor to the first child of its current node that extends beyond the given or point.
-
#goto_last_child ⇒ Object
Move the cursor to the last child of its current node.
-
#goto_next_sibling ⇒ Object
Move the cursor to the next sibling of its current node.
-
#goto_parent ⇒ Boolean
Move the cursor to the parent of its current node.
-
#goto_previous_sibling ⇒ Boolean
Move the cursor to the previous sibling of its current node.
-
#initialize(node) ⇒ TreeCursor
constructor
Create a new tree cursor starting from the given node.
-
#reset(node) ⇒ nil
Re-initialize a tree cursor to start at a different node.
-
#reset_to(src) ⇒ nil
Re-initialize a tree cursor to the same position as another cursor.
Constructor Details
#initialize(node) ⇒ TreeCursor
Create a new tree cursor starting from the given node.
A tree cursor allows you to walk a syntax tree more efficiently than is possible using the Node functions. It is a mutable object that is always on a certain syntax node, and can be moved imperatively to different nodes.
206 207 208 209 210 211 |
# File 'ext/tree_sitter/tree_cursor.c', line 206
static VALUE tree_cursor_initialize(VALUE self, VALUE node) {
TSNode n = value_to_node(node);
tree_cursor_t *ptr = unwrap(self);
ptr->data = ts_tree_cursor_new(n);
return self;
}
|
Instance Method Details
#copy ⇒ Object
Class methods
#current_depth ⇒ Integer
Get the depth of the cursor’s current node relative to the original node that the cursor was constructed with.
38 39 40 |
# File 'ext/tree_sitter/tree_cursor.c', line 38 static VALUE tree_cursor_current_depth(VALUE self) { return UINT2NUM(ts_tree_cursor_current_depth(&SELF)); } |
#current_descendant_index ⇒ Integer
Get the index of the cursor’s current node out of all of the descendants of the original node that the cursor was constructed with.
48 49 50 |
# File 'ext/tree_sitter/tree_cursor.c', line 48 static VALUE tree_cursor_current_descendant_index(VALUE self) { return UINT2NUM(ts_tree_cursor_current_descendant_index(&SELF)); } |
#current_field_id ⇒ Integer
Get the field id of the tree cursor’s current node.
This returns zero if the current node doesn’t have a field.
62 63 64 |
# File 'ext/tree_sitter/tree_cursor.c', line 62 static VALUE tree_cursor_current_field_id(VALUE self) { return UINT2NUM(ts_tree_cursor_current_field_id(&SELF)); } |
#current_field_name ⇒ String
Get the field name of the tree cursor’s current node.
This returns nil if the current node doesn’t have a field.
75 76 77 |
# File 'ext/tree_sitter/tree_cursor.c', line 75 static VALUE tree_cursor_current_field_name(VALUE self) { return safe_str(ts_tree_cursor_current_field_name(&SELF)); } |
#current_node ⇒ Node
Get the tree cursor’s current node.
84 85 86 87 |
# File 'ext/tree_sitter/tree_cursor.c', line 84 static VALUE tree_cursor_current_node(VALUE self) { TSNode node = ts_tree_cursor_current_node(&SELF); return new_node(&node); } |
#goto_descendant(descendant_idx) ⇒ nil
Move the cursor to the node that is the nth descendant of the original node that the cursor was constructed with, where zero represents the original node itself.
96 97 98 99 100 |
# File 'ext/tree_sitter/tree_cursor.c', line 96
static VALUE tree_cursor_goto_descendant(VALUE self, VALUE descendant_idx) {
uint32_t idx = NUM2UINT(descendant_idx);
ts_tree_cursor_goto_descendant(&SELF, idx);
return Qnil;
}
|
#goto_first_child ⇒ Boolean
Move the cursor to the first child of its current node.
This returns true if the cursor successfully moved, and returns false if there were no children.
110 111 112 |
# File 'ext/tree_sitter/tree_cursor.c', line 110 static VALUE tree_cursor_goto_first_child(VALUE self) { return ts_tree_cursor_goto_first_child(&SELF) ? Qtrue : Qfalse; } |
#goto_first_child_for_byte(byte) ⇒ Integer
Move the 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 -1 if no such child was found.
123 124 125 126 |
# File 'ext/tree_sitter/tree_cursor.c', line 123
static VALUE tree_cursor_goto_first_child_for_byte(VALUE self, VALUE byte) {
return LL2NUM(
ts_tree_cursor_goto_first_child_for_byte(&SELF, NUM2UINT(byte)));
}
|
#goto_first_child_for_point(point) ⇒ Integer
Move the cursor to the first child of its current node that extends beyond the given or point.
This returns the index of the child node if one was found, and returns -1 if no such child was found.
137 138 139 140 |
# File 'ext/tree_sitter/tree_cursor.c', line 137
static VALUE tree_cursor_goto_first_child_for_point(VALUE self, VALUE point) {
return LL2NUM(
ts_tree_cursor_goto_first_child_for_point(&SELF, value_to_point(point)));
}
|
#goto_last_child ⇒ Object
Move the cursor to the last child of its current node.
This returns true if the cursor successfully moved, and returns false if there were no children.
Note that this function may be slower than #goto_first_child because it needs to iterate through all the children to compute the child’s position.
152 153 154 |
# File 'ext/tree_sitter/tree_cursor.c', line 152 static VALUE tree_cursor_goto_last_child(VALUE self) { return ts_tree_cursor_goto_last_child(&SELF) ? Qtrue : Qfalse; } |
#goto_next_sibling ⇒ Object
Move the cursor to the next sibling of its current node.
This returns true if the cursor successfully moved, and returns false if there was no next sibling node.
164 165 166 |
# File 'ext/tree_sitter/tree_cursor.c', line 164 static VALUE tree_cursor_goto_next_sibling(VALUE self) { return ts_tree_cursor_goto_next_sibling(&SELF) ? Qtrue : Qfalse; } |
#goto_parent ⇒ Boolean
Move the cursor to the parent of its current node.
This returns true if the cursor successfully moved, and returns false if there was no parent node (the cursor was already on the root node).
176 177 178 |
# File 'ext/tree_sitter/tree_cursor.c', line 176 static VALUE tree_cursor_goto_parent(VALUE self) { return ts_tree_cursor_goto_parent(&SELF) ? Qtrue : Qfalse; } |
#goto_previous_sibling ⇒ Boolean
Move the cursor to the previous sibling of its current node.
This returns true if the cursor successfully moved, and returns false if there was no previous sibling node.
Note, that this function may be slower than #goto_next_sibling due to how node positions are stored. In the worst case, this will need to iterate through all the children upto the previous sibling node to recalculate its position.
193 194 195 |
# File 'ext/tree_sitter/tree_cursor.c', line 193 static VALUE tree_cursor_goto_previous_sibling(VALUE self) { return ts_tree_cursor_goto_previous_sibling(&SELF) ? Qtrue : Qfalse; } |
#reset(node) ⇒ nil
Re-initialize a tree cursor to start at a different node.
218 219 220 221 |
# File 'ext/tree_sitter/tree_cursor.c', line 218
static VALUE tree_cursor_reset(VALUE self, VALUE node) {
ts_tree_cursor_reset(&SELF, value_to_node(node));
return Qnil;
}
|
#reset_to(src) ⇒ nil
Re-initialize a tree cursor to the same position as another cursor.
Unlike #reset, this will not lose parent information and allows reusing already created cursors.
231 232 233 234 |
# File 'ext/tree_sitter/tree_cursor.c', line 231
VALUE tree_cursor_reset_to(VALUE self, VALUE src) {
ts_tree_cursor_reset_to(&SELF, &unwrap(src)->data);
return Qnil;
}
|