Class: TreeSitter::Tree

Inherits:
Object
  • Object
show all
Defined in:
ext/tree_sitter/tree.c

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.changed_rangesObject

Module methods

.finalizerObject



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

#copyTree

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.

Returns:



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.

Parameters:

Returns:

  • (nil)


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_rangesArray<Range>

Get the array of included ranges that was used to parse the syntax tree.

Returns:



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;
}

#languageLanguage

Get the language that was used to parse the syntax tree.

Returns:



164
165
166
# File 'ext/tree_sitter/tree.c', line 164

static VALUE tree_language(VALUE self) {
  return new_language(ts_tree_language(SELF));
}

Write a DOT graph describing the syntax tree to the given file.

Parameters:

  • file (String)

Returns:

  • (nil)


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_nodeNode

Get the root node of the syntax tree.

Returns:



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.

Returns:



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));
}