2018-05-10 22:22:37 -07:00
|
|
|
#include "tree_sitter/runtime.h"
|
|
|
|
|
#include "runtime/array.h"
|
|
|
|
|
#include "runtime/get_changed_ranges.h"
|
|
|
|
|
#include "runtime/subtree.h"
|
|
|
|
|
#include "runtime/tree_cursor.h"
|
|
|
|
|
#include "runtime/tree.h"
|
|
|
|
|
|
2018-05-29 16:00:32 -07:00
|
|
|
static const unsigned PARENT_CACHE_CAPACITY = 32;
|
|
|
|
|
|
2018-09-17 13:12:13 -07:00
|
|
|
TSTree *ts_tree_new(Subtree root, const TSLanguage *language) {
|
2018-05-10 22:22:37 -07:00
|
|
|
TSTree *result = ts_malloc(sizeof(TSTree));
|
|
|
|
|
result->root = root;
|
|
|
|
|
result->language = language;
|
2018-05-29 16:00:32 -07:00
|
|
|
result->parent_cache = NULL;
|
|
|
|
|
result->parent_cache_start = 0;
|
|
|
|
|
result->parent_cache_size = 0;
|
2018-05-10 22:22:37 -07:00
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TSTree *ts_tree_copy(const TSTree *self) {
|
|
|
|
|
ts_subtree_retain(self->root);
|
|
|
|
|
return ts_tree_new(self->root, self->language);
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-11 15:06:13 -07:00
|
|
|
void ts_tree_delete(TSTree *self) {
|
2018-05-10 22:22:37 -07:00
|
|
|
SubtreePool pool = ts_subtree_pool_new(0);
|
|
|
|
|
ts_subtree_release(&pool, self->root);
|
|
|
|
|
ts_subtree_pool_delete(&pool);
|
2018-05-29 16:00:32 -07:00
|
|
|
if (self->parent_cache) ts_free(self->parent_cache);
|
2018-05-10 22:22:37 -07:00
|
|
|
ts_free(self);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TSNode ts_tree_root_node(const TSTree *self) {
|
2018-09-17 13:12:13 -07:00
|
|
|
return ts_node_new(self, &self->root, ts_subtree_padding(self->root), 0);
|
2018-05-10 22:22:37 -07:00
|
|
|
}
|
|
|
|
|
|
2018-06-21 12:54:19 -07:00
|
|
|
const TSLanguage *ts_tree_language(const TSTree *self) {
|
|
|
|
|
return self->language;
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-10 22:22:37 -07:00
|
|
|
void ts_tree_edit(TSTree *self, const TSInputEdit *edit) {
|
|
|
|
|
SubtreePool pool = ts_subtree_pool_new(0);
|
|
|
|
|
self->root = ts_subtree_edit(self->root, edit, &pool);
|
2018-07-10 14:00:31 -07:00
|
|
|
self->parent_cache_start = 0;
|
|
|
|
|
self->parent_cache_size = 0;
|
2018-05-11 16:53:47 -07:00
|
|
|
ts_subtree_pool_delete(&pool);
|
2018-05-10 22:22:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TSRange *ts_tree_get_changed_ranges(const TSTree *self, const TSTree *other, uint32_t *count) {
|
|
|
|
|
TSRange *result;
|
2018-10-21 10:39:05 -07:00
|
|
|
TreeCursor cursor1 = {array_new(), NULL};
|
|
|
|
|
TreeCursor cursor2 = {array_new(), NULL};
|
2018-06-21 12:54:04 -07:00
|
|
|
TSNode root = ts_tree_root_node(self);
|
|
|
|
|
ts_tree_cursor_init(&cursor1, root);
|
|
|
|
|
ts_tree_cursor_init(&cursor2, root);
|
2018-05-10 22:22:37 -07:00
|
|
|
*count = ts_subtree_get_changed_ranges(
|
2018-09-17 13:12:13 -07:00
|
|
|
&self->root, &other->root, &cursor1, &cursor2,
|
2018-05-10 22:22:37 -07:00
|
|
|
self->language, &result
|
|
|
|
|
);
|
|
|
|
|
array_delete(&cursor1.stack);
|
|
|
|
|
array_delete(&cursor2.stack);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ts_tree_print_dot_graph(const TSTree *self, FILE *file) {
|
|
|
|
|
ts_subtree_print_dot_graph(self->root, self->language, file);
|
|
|
|
|
}
|
2018-05-29 16:00:32 -07:00
|
|
|
|
|
|
|
|
TSNode ts_tree_get_cached_parent(const TSTree *self, const TSNode *node) {
|
|
|
|
|
for (uint32_t i = 0; i < self->parent_cache_size; i++) {
|
|
|
|
|
uint32_t index = (self->parent_cache_start + i) % PARENT_CACHE_CAPACITY;
|
|
|
|
|
ParentCacheEntry *entry = &self->parent_cache[index];
|
|
|
|
|
if (entry->child == node->id) {
|
|
|
|
|
return ts_node_new(self, entry->parent, entry->position, entry->alias_symbol);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return ts_node_new(NULL, NULL, length_zero(), 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ts_tree_set_cached_parent(const TSTree *_self, const TSNode *node, const TSNode *parent) {
|
|
|
|
|
TSTree *self = (TSTree *)_self;
|
|
|
|
|
if (!self->parent_cache) {
|
|
|
|
|
self->parent_cache = ts_calloc(PARENT_CACHE_CAPACITY, sizeof(ParentCacheEntry));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint32_t index = (self->parent_cache_start + self->parent_cache_size) % PARENT_CACHE_CAPACITY;
|
|
|
|
|
self->parent_cache[index] = (ParentCacheEntry) {
|
|
|
|
|
.child = node->id,
|
2018-09-17 13:12:13 -07:00
|
|
|
.parent = (const Subtree *)parent->id,
|
2018-05-29 16:00:32 -07:00
|
|
|
.position = {
|
|
|
|
|
parent->context[0],
|
|
|
|
|
{parent->context[1], parent->context[2]}
|
|
|
|
|
},
|
|
|
|
|
.alias_symbol = parent->context[3],
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (self->parent_cache_size == PARENT_CACHE_CAPACITY) {
|
|
|
|
|
self->parent_cache_start++;
|
|
|
|
|
} else {
|
|
|
|
|
self->parent_cache_size++;
|
|
|
|
|
}
|
|
|
|
|
}
|