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_ranges(old_tree, new_tree) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'ext/tree_sitter/tree.c', line 82

static VALUE tree_changed_ranges(VALUE _self, VALUE old_tree, VALUE new_tree) {
  TSTree *old = unwrap(old_tree)->data;
  TSTree *new = unwrap(new_tree)->data;
  uint32_t length;
  TSRange *ranges = ts_tree_get_changed_ranges(old, new, &length);
  VALUE res = rb_ary_new_capa(length);

  for (uint32_t i = 0; i < length; i++) {
    rb_ary_push(res, new_range(&ranges[i]));
  }

  if (ranges) {
    free(ranges);
  }

  return res;
}

.finalizerObject



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'ext/tree_sitter/tree.c', line 115

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

#copyObject

Class methods

#edit(edit) ⇒ Object



76
77
78
79
80
# File 'ext/tree_sitter/tree.c', line 76

static VALUE tree_edit(VALUE self, VALUE edit) {
  TSInputEdit in = value_to_input_edit(edit);
  ts_tree_edit(SELF, &in);
  return Qnil;
}

#languageObject



72
73
74
# File 'ext/tree_sitter/tree.c', line 72

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


100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'ext/tree_sitter/tree.c', line 100

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_nodeObject



68
69
70
# File 'ext/tree_sitter/tree.c', line 68

static VALUE tree_root_node(VALUE self) {
  return new_node_by_val(ts_tree_root_node(SELF));
}