Class: TreeSitter::Node
- Inherits:
-
Object
- Object
- TreeSitter::Node
- Defined in:
- lib/tree-sitter/node.rb,
ext/tree-sitter/node.c
Defined Under Namespace
Classes: Point
Instance Method Summary collapse
-
#child(child_index) ⇒ Object
Public: Return the child at the specified index.
-
#child_count ⇒ Object
Public: The number of named and unnamed children.
- #children ⇒ Object
-
#end_position ⇒ Object
Public: Get the ending position for a node.
-
#first_child ⇒ Object
Public: Return the first child.
-
#first_named_child ⇒ Object
Public: Return the first named child.
-
#last_child ⇒ Object
Public: Return the last named child.
-
#last_named_child ⇒ Object
Public: Return the last named child.
-
#named? ⇒ Boolean
Public: Does the node have a name?.
-
#named_child(child_index) ⇒ Object
Public: Return the named child at the specified index.
-
#named_child_count ⇒ Object
Public: The number of named children.
- #named_children ⇒ Object
-
#node_type ⇒ Object
Public: The node type.
-
#start_position ⇒ Object
Public: Get the starting position for a node.
- #text(input) ⇒ Object
-
#to_s ⇒ Object
Public: Render the node and its children as a string.
Instance Method Details
#child(child_index) ⇒ Object
Public: Return the child at the specified index.
Returns a TreeSitter::Node or nil.
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
# File 'ext/tree-sitter/node.c', line 213 VALUE rb_node_child(VALUE self, VALUE child_index) { Check_Type(child_index, T_FIXNUM); uint32_t i = NUM2UINT(child_index); AstNode *node; Data_Get_Struct(self, AstNode, node); uint32_t child_count = ts_node_child_count(node->ts_node); if (i > child_count) { return Qnil; } else { TSNode child = ts_node_child(node->ts_node, i); return rb_new_node(child, node->ts_document); } } |
#child_count ⇒ Object
Public: The number of named and unnamed children.
Returns an Integer.
103 104 105 106 107 108 109 110 111 112 113 |
# File 'ext/tree-sitter/node.c', line 103 VALUE rb_node_child_count(VALUE self) { AstNode *node; Data_Get_Struct(self, AstNode, node); if (node->ts_node.data) { return UINT2NUM(ts_node_child_count(node->ts_node)); } else { return UINT2NUM(0); } } |
#children ⇒ Object
5 6 7 |
# File 'lib/tree-sitter/node.rb', line 5 def children child_count.times.map { |i| child(i) } end |
#end_position ⇒ Object
Public: Get the ending position for a node.
Returns a Point.
72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'ext/tree-sitter/node.c', line 72 VALUE rb_node_end_point(VALUE self) { AstNode *node; Data_Get_Struct(self, AstNode, node); TSPoint start = ts_node_end_point(node->ts_node); Point *point = malloc(sizeof(Point)); point->ts_point = start; return Data_Wrap_Struct(rb_cPoint, NULL, rb_point_free, point); } |
#first_child ⇒ Object
Public: Return the first child.
Returns a TreeSitter::Node or nil.
137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'ext/tree-sitter/node.c', line 137 VALUE rb_node_first_child(VALUE self) { AstNode *node; Data_Get_Struct(self, AstNode, node); TSNode child = ts_node_child(node->ts_node, 0); if (child.data) { return rb_new_node(child, node->ts_document); } else { return Qnil; } } |
#first_named_child ⇒ Object
Public: Return the first named child.
Returns a TreeSitter::Node or nil.
156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'ext/tree-sitter/node.c', line 156 VALUE rb_node_first_named_child(VALUE self) { AstNode *node; Data_Get_Struct(self, AstNode, node); TSNode child = ts_node_named_child(node->ts_node, 0); if (child.data) { return rb_new_node(child, node->ts_document); } else { return Qnil; } } |
#last_child ⇒ Object
Public: Return the last named child.
Returns a TreeSitter::Node or nil.
194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'ext/tree-sitter/node.c', line 194 VALUE rb_node_last_named_child(VALUE self) { AstNode *node; Data_Get_Struct(self, AstNode, node); uint32_t child_count = ts_node_named_child_count(node->ts_node); if (child_count > 0) { TSNode child = ts_node_named_child(node->ts_node, child_count - 1); return rb_new_node(child, node->ts_document); } else { return Qnil; } } |
#last_named_child ⇒ Object
Public: Return the last named child.
Returns a TreeSitter::Node or nil.
194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'ext/tree-sitter/node.c', line 194 VALUE rb_node_last_named_child(VALUE self) { AstNode *node; Data_Get_Struct(self, AstNode, node); uint32_t child_count = ts_node_named_child_count(node->ts_node); if (child_count > 0) { TSNode child = ts_node_named_child(node->ts_node, child_count - 1); return rb_new_node(child, node->ts_document); } else { return Qnil; } } |
#named? ⇒ Boolean
Public: Does the node have a name?
Returns a Boolean.
90 91 92 93 94 95 96 |
# File 'ext/tree-sitter/node.c', line 90 VALUE rb_node_is_named(VALUE self) { AstNode *node; Data_Get_Struct(self, AstNode, node); return ts_node_is_named(node->ts_node) ? Qtrue : Qfalse; } |
#named_child(child_index) ⇒ Object
Public: Return the named child at the specified index.
Returns a TreeSitter::Node or nil.
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
# File 'ext/tree-sitter/node.c', line 236 VALUE rb_node_named_child(VALUE self, VALUE child_index) { Check_Type(child_index, T_FIXNUM); uint32_t i = NUM2UINT(child_index); AstNode *node; Data_Get_Struct(self, AstNode, node); uint32_t child_count = ts_node_named_child_count(node->ts_node); if (i > child_count) { return Qnil; } else { TSNode child = ts_node_named_child(node->ts_node, i); return rb_new_node(child, node->ts_document); } } |
#named_child_count ⇒ Object
Public: The number of named children.
Returns an Integer.
120 121 122 123 124 125 126 127 128 129 130 |
# File 'ext/tree-sitter/node.c', line 120 VALUE rb_node_named_child_count(VALUE self) { AstNode *node; Data_Get_Struct(self, AstNode, node); if (node->ts_node.data) { return UINT2NUM(ts_node_named_child_count(node->ts_node)); } else { return UINT2NUM(0); } } |
#named_children ⇒ Object
9 10 11 |
# File 'lib/tree-sitter/node.rb', line 9 def named_children named_child_count.times.map { |i| named_child(i) } end |
#node_type ⇒ Object
Public: The node type.
Returns a String.
37 38 39 40 41 42 43 |
# File 'ext/tree-sitter/node.c', line 37 VALUE rb_node_type(VALUE self) { AstNode *node; Data_Get_Struct(self, AstNode, node); return rb_str_new_cstr(ts_node_type(node->ts_node, node->ts_document)); } |
#start_position ⇒ Object
Public: Get the starting position for a node.
Returns a Point.
55 56 57 58 59 60 61 62 63 64 65 |
# File 'ext/tree-sitter/node.c', line 55 VALUE rb_node_start_point(VALUE self) { AstNode *node; Data_Get_Struct(self, AstNode, node); TSPoint start = ts_node_start_point(node->ts_node); Point *point = malloc(sizeof(Point)); point->ts_point = start; return Data_Wrap_Struct(rb_cPoint, NULL, rb_point_free, point); } |
#text(input) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/tree-sitter/node.rb', line 13 def text(input) rows = input.lines[start_position.row..end_position.row] if rows.count > 1 rows.map.with_index do |line, i| next line[start_position.column, -1] if i == 0 next line[0, end_position.column] if i == rows.count - 1 line end.join else rows[0][start_position.column..end_position.column - 1] end end |
#to_s ⇒ Object
Public: Render the node and its children as a string.
Returns a String.
24 25 26 27 28 29 30 |
# File 'ext/tree-sitter/node.c', line 24 VALUE rb_node_to_s(VALUE self) { AstNode *node; Data_Get_Struct(self, AstNode, node); return rb_str_new_cstr(ts_node_string(node->ts_node, node->ts_document)); } |