From 80630ce50404feaf4662558aea5ed68fbf6df263 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Tue, 22 May 2018 08:50:04 -0700 Subject: [PATCH] Store nodes' public-facing positions, not pre-padding positions --- src/runtime/node.c | 37 +++++++++++++------------------------ src/runtime/tree.c | 2 +- src/runtime/tree_cursor.c | 7 ++++++- 3 files changed, 20 insertions(+), 26 deletions(-) diff --git a/src/runtime/node.c b/src/runtime/node.c index 31843e2a..15a1572a 100644 --- a/src/runtime/node.c +++ b/src/runtime/node.c @@ -28,12 +28,12 @@ static inline TSNode ts_node__null() { // TSNode - accessors -static inline uint32_t ts_node__byte(const TSNode *self) { - return self->context[0]; +uint32_t ts_node_start_byte(const TSNode self) { + return self.context[0]; } -static inline TSPoint ts_node__position(const TSNode *self) { - return (TSPoint) {self->context[1], self->context[2]}; +TSPoint ts_node_start_point(const TSNode self) { + return (TSPoint) {self.context[1], self.context[2]}; } static inline uint32_t ts_node__alias(const TSNode *self) { @@ -60,7 +60,7 @@ static inline NodeChildIterator ts_node_child_iterator_begin(const TSNode *node) return (NodeChildIterator) { .tree = tree, .parent = subtree, - .position = {ts_node__byte(node), ts_node__position(node)}, + .position = {ts_node_start_byte(*node), ts_node_start_point(*node)}, .child_index = 0, .structural_child_index = 0, .alias_sequence = alias_sequence, @@ -77,13 +77,16 @@ static inline bool ts_node_child_iterator_next(NodeChildIterator *self, TSNode * } self->structural_child_index++; } + if (self->child_index > 0) { + self->position = length_add(self->position, child->padding); + } *result = ts_node_new( self->tree, child, self->position, alias_symbol ); - self->position = length_add(self->position, ts_subtree_total_size(child)); + self->position = length_add(self->position, child->size); self->child_index++; return true; } @@ -217,7 +220,7 @@ static inline TSNode ts_node__next_sibling(TSNode self, bool include_anonymous) NodeChildIterator iterator = ts_node_child_iterator_begin(&node); while (ts_node_child_iterator_next(&iterator, &child)) { if (iterator.position.bytes < target_end_byte) continue; - if (ts_node__byte(&child) <= ts_node__byte(&self)) { + if (ts_node_start_byte(child) <= ts_node_start_byte(self)) { if (ts_node__subtree(child) != ts_node__subtree(self)) { child_containing_target = child; } @@ -296,7 +299,7 @@ static inline TSNode ts_node__descendant_for_byte_range(TSNode self, uint32_t mi NodeChildIterator iterator = ts_node_child_iterator_begin(&node); while (ts_node_child_iterator_next(&iterator, &child)) { if (iterator.position.bytes > max) { - if (ts_node__byte(&child) > min) break; + if (ts_node_start_byte(child) > min) break; node = child; if (ts_node__is_relevant(node, include_anonymous)) last_visible_node = node; did_descend = true; @@ -344,21 +347,10 @@ static inline TSNode ts_node__descendant_for_point_range(TSNode self, TSPoint mi // TSNode - public -uint32_t ts_node_start_byte(TSNode self) { - return ts_node__byte(&self) + ts_node__subtree(self)->padding.bytes; -} - uint32_t ts_node_end_byte(TSNode self) { return ts_node_start_byte(self) + ts_node__subtree(self)->size.bytes; } -TSPoint ts_node_start_point(TSNode self) { - return point_add( - ts_node__position(&self), - ts_node__subtree(self)->padding.extent - ); -} - TSPoint ts_node_end_point(TSNode self) { return point_add(ts_node_start_point(self), ts_node__subtree(self)->size.extent); } @@ -377,10 +369,7 @@ char *ts_node_string(TSNode self) { } bool ts_node_eq(TSNode self, TSNode other) { - return ( - ts_subtree_eq(ts_node__subtree(self), ts_node__subtree(other)) && - ts_node__byte(&self) == ts_node__byte(&other) - ); + return self.tree == other.tree && self.id == other.id; } bool ts_node_is_null(TSNode self) { @@ -421,7 +410,7 @@ TSNode ts_node_parent(TSNode self) { NodeChildIterator iterator = ts_node_child_iterator_begin(&node); while (ts_node_child_iterator_next(&iterator, &child)) { if ( - ts_node__byte(&child) > ts_node__byte(&self) || + ts_node_start_byte(child) > ts_node_start_byte(self) || ts_node__subtree(child) == ts_node__subtree(self) ) break; if (iterator.position.bytes >= end_byte) { diff --git a/src/runtime/tree.c b/src/runtime/tree.c index 5e82df89..9d7c36fc 100644 --- a/src/runtime/tree.c +++ b/src/runtime/tree.c @@ -25,7 +25,7 @@ void ts_tree_delete(TSTree *self) { } TSNode ts_tree_root_node(const TSTree *self) { - return ts_node_new(self, self->root, length_zero(), 0); + return ts_node_new(self, self->root, self->root->padding, 0); } void ts_tree_edit(TSTree *self, const TSInputEdit *edit) { diff --git a/src/runtime/tree_cursor.c b/src/runtime/tree_cursor.c index 092e31f9..4f7e0078 100644 --- a/src/runtime/tree_cursor.c +++ b/src/runtime/tree_cursor.c @@ -184,5 +184,10 @@ TSNode ts_tree_cursor_current_node(const TSTreeCursor *_self) { alias_symbol = alias_sequence[last_entry->structural_child_index]; } } - return ts_node_new(self->tree, last_entry->subtree, last_entry->position, alias_symbol); + return ts_node_new( + self->tree, + last_entry->subtree, + length_add(last_entry->position, last_entry->subtree->padding), + alias_symbol + ); }