Add {next,prev}_concrete_sibling Node methods

This commit is contained in:
Max Brunsfeld 2015-09-08 21:43:37 -07:00
parent c3f3f19ea8
commit 245daffbc4
7 changed files with 208 additions and 142 deletions

View file

@ -62,69 +62,6 @@ TSNode ts_node_parent(TSNode this) {
return ts_node_make(tree, position);
}
TSNode ts_node_prev_sibling(TSNode this) {
const TSTree *tree = ts_node__tree(this);
TSLength position = ts_node__offset(this);
do {
TSTree *parent = tree->context.parent;
if (!parent)
break;
for (size_t i = tree->context.index - 1; i + 1 > 0; i--) {
const TSTree *child = parent->children[i];
position = ts_length_sub(position, ts_tree_total_size(child));
if (child->options.type == TSNodeTypeNormal)
return ts_node_make(child, position);
if (child->named_child_count > 0)
return ts_node_child(ts_node_make(child, position),
child->named_child_count - 1);
}
tree = parent;
} while (!ts_tree_is_visible(tree));
return ts_node__null();
}
TSNode ts_node_next_sibling(TSNode this) {
const TSTree *tree = ts_node__tree(this);
TSLength position = ts_node__offset(this);
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(prev_child));
if (child->options.type == TSNodeTypeNormal)
return ts_node_make(child, position);
if (child->named_child_count > 0)
return ts_node_child(ts_node_make(child, position), 0);
prev_child = child;
}
tree = parent;
} while (!ts_tree_is_visible(tree));
return ts_node__null();
}
size_t ts_node_concrete_child_count(TSNode this) {
return ts_node__tree(this)->visible_child_count;
}
bool ts_node_is_concrete(TSNode this) {
return ts_node__tree(this)->options.type == TSNodeTypeConcrete;
}
size_t ts_node_child_count(TSNode this) {
return ts_node__tree(this)->named_child_count;
}
static TSNode ts_node__child_with_type(TSNode this, size_t child_index,
TSNodeType type) {
const TSTree *tree = ts_node__tree(this);
@ -161,15 +98,63 @@ static TSNode ts_node__child_with_type(TSNode this, size_t child_index,
return ts_node__null();
}
TSNode ts_node_child(TSNode this, size_t child_index) {
return ts_node__child_with_type(this, child_index, TSNodeTypeNormal);
static TSNode ts_node__prev_sibling_with_type(TSNode this, TSNodeType type) {
const TSTree *tree = ts_node__tree(this);
TSLength position = ts_node__offset(this);
do {
size_t index = tree->context.index;
position = ts_length_sub(position, tree->context.offset);
tree = tree->context.parent;
if (!tree)
break;
for (size_t i = index - 1; i + 1 > 0; i--) {
const TSTree *child = tree->children[i];
TSLength child_position = ts_length_add(position, child->context.offset);
if (child->options.type >= type)
return ts_node_make(child, child_position);
size_t grandchild_count = (type == TSNodeTypeNormal)
? child->named_child_count
: child->visible_child_count;
if (grandchild_count > 0)
return ts_node__child_with_type(ts_node_make(child, child_position),
grandchild_count - 1, type);
}
} while (!ts_tree_is_visible(tree));
return ts_node__null();
}
TSNode ts_node_concrete_child(TSNode this, size_t child_index) {
return ts_node__child_with_type(this, child_index, TSNodeTypeConcrete);
static TSNode ts_node__next_sibling_with_type(TSNode this, TSNodeType type) {
const TSTree *tree = ts_node__tree(this);
TSLength position = ts_node__offset(this);
do {
size_t index = tree->context.index;
position = ts_length_sub(position, tree->context.offset);
tree = tree->context.parent;
if (!tree)
break;
for (size_t i = index + 1; i < tree->child_count; i++) {
const TSTree *child = tree->children[i];
TSLength child_position = ts_length_add(position, child->context.offset);
if (child->options.type >= type)
return ts_node_make(child, child_position);
size_t grandchild_count = (type == TSNodeTypeNormal)
? child->named_child_count
: child->visible_child_count;
if (grandchild_count > 0)
return ts_node__child_with_type(ts_node_make(child, child_position), 0,
type);
}
} while (!ts_tree_is_visible(tree));
return ts_node__null();
}
TSNode ts_node_find_for_range(TSNode this, size_t min, size_t max) {
static TSNode ts_node__find_for_range_with_type(TSNode this, size_t min, size_t max, TSNodeType type) {
const TSTree *tree = ts_node__tree(this), *last_visible_tree = tree;
TSLength position = ts_node__offset(this), last_visible_position = position;
@ -183,7 +168,7 @@ TSNode ts_node_find_for_range(TSNode this, size_t min, size_t max) {
break;
if (position.chars + child->padding.chars + child->size.chars > max) {
tree = child;
if (ts_tree_is_visible(child)) {
if (child->options.type >= type) {
last_visible_tree = tree;
last_visible_position = position;
}
@ -197,6 +182,50 @@ TSNode ts_node_find_for_range(TSNode this, size_t min, size_t max) {
return ts_node_make(last_visible_tree, last_visible_position);
}
TSNode ts_node_next_sibling(TSNode this) {
return ts_node__next_sibling_with_type(this, TSNodeTypeNormal);
}
TSNode ts_node_prev_sibling(TSNode this) {
return ts_node__prev_sibling_with_type(this, TSNodeTypeNormal);
}
TSNode ts_node_next_concrete_sibling(TSNode this) {
return ts_node__next_sibling_with_type(this, TSNodeTypeConcrete);
}
TSNode ts_node_prev_concrete_sibling(TSNode this) {
return ts_node__prev_sibling_with_type(this, TSNodeTypeConcrete);
}
size_t ts_node_concrete_child_count(TSNode this) {
return ts_node__tree(this)->visible_child_count;
}
bool ts_node_is_concrete(TSNode this) {
return ts_node__tree(this)->options.type == TSNodeTypeConcrete;
}
size_t ts_node_child_count(TSNode this) {
return ts_node__tree(this)->named_child_count;
}
TSNode ts_node_child(TSNode this, size_t child_index) {
return ts_node__child_with_type(this, child_index, TSNodeTypeNormal);
}
TSNode ts_node_concrete_child(TSNode this, size_t child_index) {
return ts_node__child_with_type(this, child_index, TSNodeTypeConcrete);
}
TSNode ts_node_find_for_range(TSNode this, size_t min, size_t max) {
return ts_node__find_for_range_with_type(this, min, max, TSNodeTypeNormal);
}
TSNode ts_node_find_concrete_for_range(TSNode this, size_t min, size_t max) {
return ts_node__find_for_range_with_type(this, min, max, TSNodeTypeConcrete);
}
TSNode ts_node_find_for_pos(TSNode this, size_t position) {
return ts_node_find_for_range(this, position, position);
}