Class: TreeSitter::Tree
- Inherits:
-
Object
- Object
- TreeSitter::Tree
- Defined in:
- ext/tree_sitter/tree.c
Class Method Summary collapse
-
.changed_ranges ⇒ Object
Module methods.
- .finalizer ⇒ Object
Instance Method Summary collapse
-
#copy ⇒ Tree
Create a shallow copy of the syntax tree.
-
#edit(edit) ⇒ nil
Edit the syntax tree to keep it in sync with source code that has been edited.
-
#included_ranges ⇒ Array<Range>
Get the array of included ranges that was used to parse the syntax tree.
-
#language ⇒ Language
Get the language that was used to parse the syntax tree.
-
#print_dot_graph(file) ⇒ nil
Write a DOT graph describing the syntax tree to the given file.
-
#root_node ⇒ Node
Get the root node of the syntax tree.
-
#root_node_with_offset(offset_bytes, offset_extent) ⇒ Node
Get the root node of the syntax tree, but with its position shifted forward by the given offset.
Class Method Details
.changed_ranges ⇒ Object
Module methods
.finalizer ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'ext/tree_sitter/tree.c', line 99
static VALUE tree_finalizer(VALUE _self) {
VALUE rc = rb_cv_get(cTree, "@@rc");
VALUE keys = rb_funcall(rc, rb_intern("keys"), 0);
long len = RARRAY_LEN(keys);
for (long i = 0; i < len; ++i) {
VALUE curr = RARRAY_AREF(keys, i);
unsigned int val = NUM2UINT(rb_hash_lookup(rc, curr));
if (val > 0) {
ts_tree_delete((TSTree *)NUM2ULONG(curr));
}
rb_hash_delete(rc, curr);
}
return Qnil;
}
|
Instance Method Details
#copy ⇒ Tree
Create a shallow copy of the syntax tree. This is very fast.
You need to copy a syntax tree in order to use it on more than one thread at a time, as syntax trees are not thread safe.
125 |
# File 'ext/tree_sitter/tree.c', line 125 static VALUE tree_copy(VALUE self) { return new_tree(ts_tree_copy(SELF)); } |
#edit(edit) ⇒ nil
Edit the syntax tree to keep it in sync with source code that has been edited.
You must describe the edit both in terms of byte offsets and in terms of (row, column) coordinates.
138 139 140 141 142 |
# File 'ext/tree_sitter/tree.c', line 138
static VALUE tree_edit(VALUE self, VALUE edit) {
TSInputEdit in = value_to_input_edit(edit);
ts_tree_edit(SELF, &in);
return Qnil;
}
|
#included_ranges ⇒ Array<Range>
Get the array of included ranges that was used to parse the syntax tree.
149 150 151 152 153 154 155 156 157 |
# File 'ext/tree_sitter/tree.c', line 149
static VALUE included_ranges(VALUE self) {
uint32_t length;
const TSRange *ranges = ts_tree_included_ranges(SELF, &length);
VALUE res = rb_ary_new_capa(length);
for (uint32_t i = 0; i < length; i++) {
rb_ary_push(res, new_range(&ranges[i]));
}
return res;
}
|
#language ⇒ Language
Get the language that was used to parse the syntax tree.
164 165 166 |
# File 'ext/tree_sitter/tree.c', line 164 static VALUE tree_language(VALUE self) { return new_language(ts_tree_language(SELF)); } |
#print_dot_graph(file) ⇒ nil
Write a DOT graph describing the syntax tree to the given file.
175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'ext/tree_sitter/tree.c', line 175
static VALUE tree_print_dot_graph(VALUE self, VALUE file) {
Check_Type(file, T_STRING);
char *path = StringValueCStr(file);
int fd = open(path, O_CREAT | O_WRONLY | O_TRUNC,
S_IWUSR | S_IRUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
if (fd < 0) {
rb_raise(rb_eRuntimeError, "Could not open file `%s'.\nReason:\n%s", path,
strerror(fd));
return Qnil;
}
ts_tree_print_dot_graph(SELF, fd);
close(fd);
return Qnil;
}
|
#root_node ⇒ Node
Get the root node of the syntax tree.
195 196 197 |
# File 'ext/tree_sitter/tree.c', line 195 static VALUE tree_root_node(VALUE self) { return new_node_by_val(ts_tree_root_node(SELF)); } |
#root_node_with_offset(offset_bytes, offset_extent) ⇒ Node
Get the root node of the syntax tree, but with its position shifted forward by the given offset.
205 206 207 208 209 210 |
# File 'ext/tree_sitter/tree.c', line 205
static VALUE tree_root_node_with_offset(VALUE self, VALUE offset_bytes,
VALUE offset_extent) {
uint32_t bytes = NUM2UINT(offset_bytes);
TSPoint extent = value_to_point(offset_extent);
return new_node_by_val(ts_tree_root_node_with_offset(SELF, bytes, extent));
}
|