From d70a7227a149bd0e01fd1090b01bc71a411f454c Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Fri, 14 Aug 2015 20:10:09 -0700 Subject: [PATCH] Don't return invisible nodes from ts_node_find_for_range --- spec/runtime/node_spec.cc | 12 ++++++++++++ src/runtime/node.c | 6 ++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/spec/runtime/node_spec.cc b/spec/runtime/node_spec.cc index 1aef5e8e..8e89ebb5 100644 --- a/spec/runtime/node_spec.cc +++ b/spec/runtime/node_spec.cc @@ -92,6 +92,11 @@ describe("Node", []() { AssertThat(ts_node_name(leaf, document), Equals("string")); AssertThat(ts_node_size(leaf).bytes, Equals(3)); AssertThat(ts_node_pos(leaf).bytes, Equals(16)); + + leaf = ts_node_find_for_range(root, 3, 5); + AssertThat(ts_node_name(leaf, document), Equals("number")); + AssertThat(ts_node_size(leaf).bytes, Equals(3)); + AssertThat(ts_node_pos(leaf).bytes, Equals(3)); }); }); @@ -116,6 +121,13 @@ describe("Node", []() { AssertThat(ts_node_size(node).bytes, Equals(11)); AssertThat(ts_node_pos(node).bytes, Equals(15)); }); + + it("does not return invisible nodes (repeats)", [&]() { + TSNode node = ts_node_find_for_range(root, 6, 7); + AssertThat(ts_node_name(node, document), Equals("array")); + AssertThat(ts_node_size(node).bytes, Equals(25)); + AssertThat(ts_node_pos(node).bytes, Equals(2)); + }); }); }); diff --git a/src/runtime/node.c b/src/runtime/node.c index 3e0ffdad..86f450d4 100644 --- a/src/runtime/node.c +++ b/src/runtime/node.c @@ -118,7 +118,7 @@ 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; + TSNode node = this, last_visible_node = this; bool did_descend = true; while (did_descend) { did_descend = false; @@ -131,13 +131,15 @@ 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) { node = ts_node_make(child, position); + if (ts_tree_is_visible(child)) + last_visible_node = node; did_descend = true; } position = ts_length_add(position, ts_tree_total_size(child)); } } - return node; + return last_visible_node; } TSNode ts_node_find_for_pos(TSNode this, size_t position) {