From f653f2b3bbd3a10dcfa90ffbeed9005b6d4361dd Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Tue, 9 Jan 2018 12:33:51 -0800 Subject: [PATCH 1/2] Add ts_node_first_{child,named_child}_for_byte methods --- include/tree_sitter/runtime.h | 2 ++ src/runtime/node.c | 33 +++++++++++++++++++ test/runtime/node_test.cc | 60 +++++++++++++++++++++++++++++++++++ 3 files changed, 95 insertions(+) diff --git a/include/tree_sitter/runtime.h b/include/tree_sitter/runtime.h index 06250a5b..9f51b549 100644 --- a/include/tree_sitter/runtime.h +++ b/include/tree_sitter/runtime.h @@ -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); diff --git a/src/runtime/node.c b/src/runtime/node.c index 0a8c0a54..cec988cf 100644 --- a/src/runtime/node.c +++ b/src/runtime/node.c @@ -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); } diff --git a/test/runtime/node_test.cc b/test/runtime/node_test.cc index 08783522..fcc7552d 100644 --- a/test/runtime/node_test.cc +++ b/test/runtime/node_test.cc @@ -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(nullptr)); + }); + }); + describe("symbols()", [&]() { it("returns an iterator that yields each of the node's symbols", [&]() { const TSLanguage *language = ts_document_language(document); From 315dff328518f5003bbbfcf6934e3edcb26e7be4 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Tue, 9 Jan 2018 12:33:51 -0800 Subject: [PATCH 2/2] Add an API for getting a node's child index --- include/tree_sitter/runtime.h | 1 + src/runtime/node.c | 19 +++++++++++++++++++ test/runtime/node_test.cc | 12 ++++++++++++ 3 files changed, 32 insertions(+) diff --git a/include/tree_sitter/runtime.h b/include/tree_sitter/runtime.h index 9f51b549..c4dde4fe 100644 --- a/include/tree_sitter/runtime.h +++ b/include/tree_sitter/runtime.h @@ -92,6 +92,7 @@ TSNode ts_node_child(TSNode, uint32_t); TSNode ts_node_named_child(TSNode, uint32_t); uint32_t ts_node_child_count(TSNode); uint32_t ts_node_named_child_count(TSNode); +uint32_t ts_node_child_index(TSNode); TSNode ts_node_next_sibling(TSNode); TSNode ts_node_next_named_sibling(TSNode); TSNode ts_node_prev_sibling(TSNode); diff --git a/src/runtime/node.c b/src/runtime/node.c index cec988cf..e4353e5b 100644 --- a/src/runtime/node.c +++ b/src/runtime/node.c @@ -329,6 +329,25 @@ TSNode ts_node_parent(TSNode self) { return result; } +uint32_t ts_node_child_index(TSNode self) { + const Tree *tree = ts_node__tree(self); + uint32_t result = 0; + + for (;;) { + const Tree *parent = tree->context.parent; + uint32_t index = tree->context.index; + if (!parent) return UINT32_MAX; + for (uint32_t i = 0; i < index; i++) { + Tree *child = parent->children[i]; + result += child->visible ? 1 : child->visible_child_count; + } + if (parent->visible) break; + tree = parent; + } + + return result; +} + TSNode ts_node_child(TSNode self, uint32_t child_index) { return ts_node__child(self, child_index, true); } diff --git a/test/runtime/node_test.cc b/test/runtime/node_test.cc index fcc7552d..bb620244 100644 --- a/test/runtime/node_test.cc +++ b/test/runtime/node_test.cc @@ -243,6 +243,18 @@ describe("Node", [&]() { }); }); + describe("child_index()", [&]() { + it("returns the index of the node within its parent", [&]() { + AssertThat(ts_node_child_index(ts_node_child(root_node, 0)), Equals(0u)); + AssertThat(ts_node_child_index(ts_node_child(root_node, 1)), Equals(1u)); + AssertThat(ts_node_child_index(ts_node_child(root_node, 2)), Equals(2u)); + AssertThat(ts_node_child_index(ts_node_child(root_node, 3)), Equals(3u)); + AssertThat(ts_node_child_index(ts_node_child(root_node, 4)), Equals(4u)); + AssertThat(ts_node_child_index(ts_node_child(root_node, 5)), Equals(5u)); + AssertThat(ts_node_child_index(ts_node_child(root_node, 6)), Equals(6u)); + }); + }); + describe("symbols()", [&]() { it("returns an iterator that yields each of the node's symbols", [&]() { const TSLanguage *language = ts_document_language(document);