🎨 node.c

This commit is contained in:
Max Brunsfeld 2015-08-16 10:27:26 -07:00
parent a5d73ed676
commit b853e0f2eb
2 changed files with 25 additions and 18 deletions

View file

@ -34,18 +34,16 @@ const char *ts_node_string(TSNode this, const TSDocument *document) {
}
TSNode ts_node_parent(TSNode this) {
TSLength position = this.position;
const TSTree *tree = get_tree(this);
TSLength position = this.position;
do {
TSTree *parent = tree->context.parent;
if (!parent)
return ts_node_null();
for (size_t i = 0; i < parent->child_count; i++) {
for (size_t i = 0; i < tree->context.index; i++) {
TSTree *child = parent->children[i];
if (child == tree)
break;
position = ts_length_sub(position, ts_tree_total_size(child));
}
@ -58,6 +56,7 @@ TSNode ts_node_parent(TSNode this) {
TSNode ts_node_prev_sibling(TSNode this) {
const TSTree *tree = get_tree(this);
TSLength position = this.position;
do {
TSTree *parent = tree->context.parent;
if (!parent)
@ -69,7 +68,8 @@ TSNode ts_node_prev_sibling(TSNode this) {
if (ts_tree_is_visible(child))
return ts_node_make(child, position);
if (child->visible_child_count > 0)
return ts_node_child(ts_node_make(child, position), child->visible_child_count - 1);
return ts_node_child(ts_node_make(child, position),
child->visible_child_count - 1);
}
tree = parent;
@ -81,18 +81,21 @@ TSNode ts_node_prev_sibling(TSNode this) {
TSNode ts_node_next_sibling(TSNode this) {
const TSTree *tree = get_tree(this);
TSLength position = this.position;
do {
TSTree *parent = tree->context.parent;
if (!parent)
break;
const TSTree *prev_child = tree;
for (size_t i = tree->context.index + 1; i < parent->child_count; i++) {
const TSTree *child = parent->children[i];
position = ts_length_add(position, ts_tree_total_size(parent->children[i - 1]));
position = ts_length_add(position, ts_tree_total_size(prev_child));
if (ts_tree_is_visible(child))
return ts_node_make(child, position);
if (child->visible_child_count > 0)
return ts_node_child(ts_node_make(child, position), 0);
prev_child = child;
}
tree = parent;
@ -106,12 +109,12 @@ size_t ts_node_child_count(TSNode this) {
}
TSNode ts_node_child(TSNode this, size_t child_index) {
TSNode node = this;
const TSTree *tree = get_tree(this);
TSLength position = this.position;
bool did_descend = true;
while (did_descend) {
did_descend = false;
const TSTree *tree = get_tree(node);
TSLength position = node.position;
size_t index = 0;
for (size_t i = 0; i < tree->child_count; i++) {
@ -124,8 +127,9 @@ TSNode ts_node_child(TSNode this, size_t child_index) {
size_t grandchild_index = child_index - index;
if (grandchild_index < child->visible_child_count) {
did_descend = true;
node = ts_node_make(child, position);
tree = child;
child_index = grandchild_index;
break;
}
index += child->visible_child_count;
}
@ -137,28 +141,31 @@ TSNode ts_node_child(TSNode this, size_t child_index) {
}
TSNode ts_node_find_for_range(TSNode this, size_t min, size_t max) {
TSNode node = this, last_visible_node = this;
const TSTree *tree = get_tree(this), *last_visible_tree = tree;
TSLength position = this.position, last_visible_position = position;
bool did_descend = true;
while (did_descend) {
did_descend = false;
const TSTree *tree = get_tree(node);
TSLength position = node.position;
for (size_t i = 0; i < tree->child_count; i++) {
const TSTree *child = tree->children[i];
if (position.chars + child->padding.chars > min)
break;
if (position.chars + child->padding.chars + child->size.chars > max) {
node = ts_node_make(child, position);
if (ts_tree_is_visible(child))
last_visible_node = node;
tree = child;
if (ts_tree_is_visible(child)) {
last_visible_tree = tree;
last_visible_position = position;
}
did_descend = true;
break;
}
position = ts_length_add(position, ts_tree_total_size(child));
}
}
return last_visible_node;
return ts_node_make(last_visible_tree, last_visible_position);
}
TSNode ts_node_find_for_pos(TSNode this, size_t position) {

View file

@ -79,7 +79,7 @@ TSTree *ts_tree_make_node(TSSymbol symbol, size_t child_count,
}
*result = (TSTree){.ref_count = 1,
.context = {.parent = NULL, .index = 0},
.context = {.parent = NULL, .index = 0 },
.symbol = symbol,
.children = children,
.child_count = child_count,