Add ts_node_first_{child,named_child}_for_byte methods

This commit is contained in:
Max Brunsfeld 2018-01-09 12:33:51 -08:00
parent ee9df753ff
commit f653f2b3bb
3 changed files with 95 additions and 0 deletions

View file

@ -96,6 +96,8 @@ TSNode ts_node_next_sibling(TSNode);
TSNode ts_node_next_named_sibling(TSNode);
TSNode ts_node_prev_sibling(TSNode);
TSNode ts_node_prev_named_sibling(TSNode);
TSNode ts_node_first_child_for_byte(TSNode, uint32_t);
TSNode ts_node_first_named_child_for_byte(TSNode, uint32_t);
TSNode ts_node_descendant_for_byte_range(TSNode, uint32_t, uint32_t);
TSNode ts_node_named_descendant_for_byte_range(TSNode, uint32_t, uint32_t);
TSNode ts_node_descendant_for_point_range(TSNode, TSPoint, TSPoint);

View file

@ -152,6 +152,31 @@ static inline bool point_gt(TSPoint a, TSPoint b) {
return a.row > b.row || (a.row == b.row && a.column > b.column);
}
static inline TSNode ts_node__first_child_for_byte(TSNode self, uint32_t goal,
bool include_anonymous) {
TSNode node = self;
bool did_descend = true;
while (did_descend) {
did_descend = false;
for (uint32_t i = 0; i < ts_node__tree(node)->child_count; i++) {
TSNode child = ts_node__direct_child(node, i);
if (ts_node_end_byte(child) > goal) {
if (ts_node__is_relevant(child, include_anonymous)) {
return child;
} else if (ts_node_child_count(child) > 0) {
did_descend = true;
node = child;
break;
}
}
}
}
return ts_node__null();
}
static inline TSNode ts_node__descendant_for_byte_range(TSNode self, uint32_t min,
uint32_t max,
bool include_anonymous) {
@ -346,6 +371,14 @@ TSNode ts_node_prev_named_sibling(TSNode self) {
return ts_node__prev_sibling(self, false);
}
TSNode ts_node_first_child_for_byte(TSNode self, uint32_t byte) {
return ts_node__first_child_for_byte(self, byte, true);
}
TSNode ts_node_first_named_child_for_byte(TSNode self, uint32_t byte) {
return ts_node__first_child_for_byte(self, byte, false);
}
TSNode ts_node_descendant_for_byte_range(TSNode self, uint32_t min, uint32_t max) {
return ts_node__descendant_for_byte_range(self, min, max, true);
}

View file

@ -183,6 +183,66 @@ describe("Node", [&]() {
});
});
describe("first_child_for_byte(byte_offset)", [&]() {
it("returns the first child that extends beyond the given byte offset", [&]() {
TSNode child;
child = ts_node_first_child_for_byte(root_node, array_index);
AssertThat(ts_node_type(child, document), Equals("["));
child = ts_node_first_child_for_byte(root_node, number_index);
AssertThat(ts_node_type(child, document), Equals("number"));
child = ts_node_first_child_for_byte(root_node, number_end_index);
AssertThat(ts_node_type(child, document), Equals(","));
child = ts_node_first_child_for_byte(root_node, number_end_index + 1);
AssertThat(ts_node_type(child, document), Equals("false"));
child = ts_node_first_child_for_byte(root_node, false_index - 1);
AssertThat(ts_node_type(child, document), Equals("false"));
child = ts_node_first_child_for_byte(root_node, false_index);
AssertThat(ts_node_type(child, document), Equals("false"));
child = ts_node_first_child_for_byte(root_node, false_index + 1);
AssertThat(ts_node_type(child, document), Equals("false"));
child = ts_node_first_child_for_byte(root_node, false_end_index);
AssertThat(ts_node_type(child, document), Equals(","));
child = ts_node_first_child_for_byte(root_node, false_end_index);
AssertThat(ts_node_type(child, document), Equals(","));
child = ts_node_first_child_for_byte(root_node, object_index);
AssertThat(ts_node_type(child, document), Equals("object"));
child = ts_node_first_child_for_byte(root_node, object_index + 1);
AssertThat(ts_node_type(child, document), Equals("object"));
child = ts_node_first_child_for_byte(root_node, object_end_index);
AssertThat(ts_node_type(child, document), Equals("]"));
});
});
describe("first_named_child_for_byte(byte_offset)", [&]() {
it("returns the first named child that extends beyond the given byte offset", [&]() {
TSNode child;
child = ts_node_first_named_child_for_byte(root_node, array_index);
AssertThat(ts_node_type(child, document), Equals("number"));
child = ts_node_first_named_child_for_byte(root_node, number_index);
AssertThat(ts_node_type(child, document), Equals("number"));
child = ts_node_first_named_child_for_byte(root_node, number_end_index);
AssertThat(ts_node_type(child, document), Equals("false"));
child = ts_node_first_named_child_for_byte(root_node, number_end_index + 1);
AssertThat(ts_node_type(child, document), Equals("false"));
child = ts_node_first_named_child_for_byte(root_node, false_index - 1);
AssertThat(ts_node_type(child, document), Equals("false"));
child = ts_node_first_named_child_for_byte(root_node, false_index);
AssertThat(ts_node_type(child, document), Equals("false"));
child = ts_node_first_named_child_for_byte(root_node, false_index + 1);
AssertThat(ts_node_type(child, document), Equals("false"));
child = ts_node_first_named_child_for_byte(root_node, false_end_index);
AssertThat(ts_node_type(child, document), Equals("object"));
child = ts_node_first_named_child_for_byte(root_node, object_index);
AssertThat(ts_node_type(child, document), Equals("object"));
child = ts_node_first_named_child_for_byte(root_node, object_index + 1);
AssertThat(ts_node_type(child, document), Equals("object"));
child = ts_node_first_named_child_for_byte(root_node, object_end_index);
AssertThat(child.data, Equals<void *>(nullptr));
});
});
describe("symbols()", [&]() {
it("returns an iterator that yields each of the node's symbols", [&]() {
const TSLanguage *language = ts_document_language(document);