From 3d9a44d880fd863841279648c0b394a5d9342221 Mon Sep 17 00:00:00 2001 From: joshvera Date: Wed, 25 Nov 2015 13:36:19 -0500 Subject: [PATCH] Calculate the column and offset separately in TSNode --- include/tree_sitter/runtime.h | 4 +-- script/util/run_tests.sh | 6 ++--- spec/runtime/node_spec.cc | 6 +++++ src/runtime/document.c | 2 +- src/runtime/length.h | 22 ++++++++-------- src/runtime/lexer.c | 6 ++--- src/runtime/node.c | 48 +++++++++++++++++------------------ src/runtime/node.h | 2 +- src/runtime/tree.c | 20 ++++++++++----- src/runtime/tree.h | 2 +- 10 files changed, 66 insertions(+), 52 deletions(-) diff --git a/include/tree_sitter/runtime.h b/include/tree_sitter/runtime.h index 69f5a1b1..0eb769bf 100644 --- a/include/tree_sitter/runtime.h +++ b/include/tree_sitter/runtime.h @@ -36,14 +36,14 @@ typedef struct { } TSInputEdit; typedef struct { - size_t line; + size_t row; size_t column; } TSPoint; typedef struct { const void *data; TSLength offset; - TSPoint offset_point; + size_t row; } TSNode; typedef unsigned short TSSymbol; diff --git a/script/util/run_tests.sh b/script/util/run_tests.sh index 4e3759e4..df1f29db 100644 --- a/script/util/run_tests.sh +++ b/script/util/run_tests.sh @@ -68,10 +68,10 @@ function run_tests { ;; debug) - if which -s gdb; then - gdb $cmd -- "${args[@]}" - elif which -s lldb; then + if which -s lldb; then lldb $cmd -- "${args[@]}" + elif which -s gdb; then + gdb $cmd -- "${args[@]}" else echo "No debugger found" exit 1 diff --git a/spec/runtime/node_spec.cc b/spec/runtime/node_spec.cc index e58317c9..a85288d0 100644 --- a/spec/runtime/node_spec.cc +++ b/spec/runtime/node_spec.cc @@ -43,6 +43,12 @@ describe("Node", []() { AssertThat(ts_node_pos(array_node).bytes, Equals(2)); AssertThat(ts_node_size(array_node).bytes, Equals(25)); + AssertThat(ts_node_start_point(array_node).row, Equals(0)); + AssertThat(ts_node_start_point(array_node).column, Equals(2)); + + AssertThat(ts_node_end_point(array_node).row, Equals(0)); + AssertThat(ts_node_end_point(array_node).column, Equals(27)); + AssertThat(ts_node_pos(child1).bytes, Equals(3)); AssertThat(ts_node_size(child1).bytes, Equals(3)); diff --git a/src/runtime/document.c b/src/runtime/document.c index 7042516b..1afcb2f3 100644 --- a/src/runtime/document.c +++ b/src/runtime/document.c @@ -85,7 +85,7 @@ void ts_document_invalidate(TSDocument *self) { } TSNode ts_document_root_node(const TSDocument *self) { - TSNode result = ts_node_make(self->tree, ts_length_zero(), ts_point_zero()); + TSNode result = ts_node_make(self->tree, ts_length_zero(), 0); while (result.data && !ts_tree_is_visible(result.data)) result = ts_node_named_child(result, 0); return result; diff --git a/src/runtime/length.h b/src/runtime/length.h index 562e626d..a3a574e6 100644 --- a/src/runtime/length.h +++ b/src/runtime/length.h @@ -27,29 +27,29 @@ static inline TSLength ts_length_sub(TSLength len1, TSLength len2) { } static inline TSPoint ts_point_add(TSPoint point1, TSPoint point2) { - size_t line = point1.line + point2.line; + size_t row = point1.row + point2.row; size_t column; - if (point2.line == 0) { + if (point2.row == 0) { column = point1.column + point2.column; } else { column = point2.column; } - return (TSPoint){ .line = line, .column = column }; + return (TSPoint){ .row = row, .column = column }; } static inline TSPoint ts_point_sub(TSPoint point1, TSPoint point2) { - size_t line, column; - if (point1.line == point2.line) { - line = 0; + size_t row, column; + if (point1.row == point2.row) { + row = 0; column = point1.column - point2.column; } else { - line = point1.line - point2.line; + row = point1.row - point2.row; column = point1.column; } - return (TSPoint){ .line = line, .column = column }; + return (TSPoint){ .row = row, .column = column }; } static inline TSLength ts_length_zero() { @@ -63,11 +63,11 @@ static inline bool ts_length_eq(TSLength len1, TSLength len2) { } static inline TSPoint ts_point_zero() { - return (TSPoint){ .line = 1, .column = 1 }; + return (TSPoint){ .row = 0, .column = 0 }; } -static inline TSPoint ts_point_make(size_t line, size_t column) { - return (TSPoint){ .line = line, .column = column }; +static inline TSPoint ts_point_make(size_t row, size_t column) { + return (TSPoint){ .row = row, .column = column }; } static inline TSLength ts_length_make(size_t bytes, size_t chars) { diff --git a/src/runtime/lexer.c b/src/runtime/lexer.c index 09e567f6..5a6fcf1d 100644 --- a/src/runtime/lexer.c +++ b/src/runtime/lexer.c @@ -54,7 +54,7 @@ static void ts_lexer__start_token(TSLexer *self) { DEBUG("start_token chars:%lu", self->current_position.chars); self->token_start_position = self->current_position; - DEBUG("start_token line:%lu", self->current_point.line); + DEBUG("start_token row:%lu", self->current_point.row); DEBUG("start_token column:%lu", self->current_point.column); self->token_start_point = self->current_point; } @@ -70,7 +70,7 @@ static bool ts_lexer__advance(TSLexer *self, TSStateId state) { self->current_position.chars += 1; if (self->lookahead == '\n') { - self->current_point.line += 1; + self->current_point.row += 1; self->current_point.column = 0; } else { self->current_point.column += 1; @@ -93,7 +93,7 @@ static TSTree *ts_lexer__accept(TSLexer *self, TSSymbol symbol, self->token_end_position = self->current_position; TSPoint size_point = ts_point_sub(self->current_point, self ->token_start_point); - TSPoint padding_point = ts_point_sub(self->token_start_point, self ->token_end_point); + TSPoint padding_point = ts_point_sub(self->token_start_point, self->token_end_point); self->token_end_point = self->current_point; if (symbol == ts_builtin_sym_error) { diff --git a/src/runtime/node.c b/src/runtime/node.c index 8dbb255f..ffff0ed4 100644 --- a/src/runtime/node.c +++ b/src/runtime/node.c @@ -4,8 +4,8 @@ #include "runtime/tree.h" #include "runtime/document.h" -TSNode ts_node_make(const TSTree *tree, TSLength offset, TSPoint offset_point) { - return (TSNode){.data = tree, .offset = offset, .offset_point = offset_point }; +TSNode ts_node_make(const TSTree *tree, TSLength offset, size_t row) { + return (TSNode){.data = tree, .offset = offset, .row = row }; } /* @@ -13,7 +13,7 @@ TSNode ts_node_make(const TSTree *tree, TSLength offset, TSPoint offset_point) { */ static inline TSNode ts_node__null() { - return ts_node_make(NULL, ts_length_zero(), ts_point_zero()); + return ts_node_make(NULL, ts_length_zero(), 0); } static inline const TSTree *ts_node__tree(TSNode self) { @@ -25,15 +25,11 @@ static inline TSLength ts_node__offset(TSNode self) { } -static inline TSPoint ts_node__offset_point(TSNode self) { - return self.offset_point; -} - static inline TSNode ts_node__child(TSNode self, size_t child_index, TSNodeType type) { const TSTree *tree = ts_node__tree(self); TSLength position = ts_node__offset(self); - TSPoint point = ts_node__offset_point(self); + size_t offset_row = self.row; bool did_descend = true; while (did_descend) { @@ -44,7 +40,7 @@ static inline TSNode ts_node__child(TSNode self, size_t child_index, TSTree *child = tree->children[i]; if (child->options.type >= type) { if (index == child_index) - return ts_node_make(child, position, point); + return ts_node_make(child, position, offset_row); index++; } else { size_t grandchild_index = child_index - index; @@ -60,6 +56,7 @@ static inline TSNode ts_node__child(TSNode self, size_t child_index, index += grandchild_count; } position = ts_length_add(position, ts_tree_total_size(child)); + offset_row += child->padding_point.row + child->size_point.row; } } @@ -69,12 +66,12 @@ static inline TSNode ts_node__child(TSNode self, size_t child_index, static inline TSNode ts_node__prev_sibling(TSNode self, TSNodeType type) { const TSTree *tree = ts_node__tree(self); TSLength offset = ts_node__offset(self); - TSPoint offset_point = ts_node__offset_point(self); + size_t offset_row = self.row; do { size_t index = tree->context.index; offset = ts_length_sub(offset, tree->context.offset); - offset_point = ts_find_parent_offset_point(tree); + offset_row -= tree->context.offset_point.row; tree = tree->context.parent; if (!tree) break; @@ -82,14 +79,14 @@ static inline TSNode ts_node__prev_sibling(TSNode self, TSNodeType type) { for (size_t i = index - 1; i + 1 > 0; i--) { const TSTree *child = tree->children[i]; TSLength child_offset = ts_length_add(offset, child->context.offset); - TSPoint child_offset_point = ts_point_add(offset_point, child->context.offset_point); + size_t child_offset_row = offset_row + child->context.offset_point.row; if (child->options.type >= type) - return ts_node_make(child, child_offset, child_offset_point); + return ts_node_make(child, child_offset, child_offset_row); size_t grandchild_count = (type == TSNodeTypeNamed) ? child->named_child_count : child->visible_child_count; if (grandchild_count > 0) - return ts_node__child(ts_node_make(child, child_offset, child_offset_point), + return ts_node__child(ts_node_make(child, child_offset, child_offset_row), grandchild_count - 1, type); } } while (!ts_tree_is_visible(tree)); @@ -100,11 +97,12 @@ static inline TSNode ts_node__prev_sibling(TSNode self, TSNodeType type) { static inline TSNode ts_node__next_sibling(TSNode self, TSNodeType type) { const TSTree *tree = ts_node__tree(self); TSLength offset = ts_node__offset(self); - TSPoint offset_point = ts_node__offset_point(self); + size_t offset_row = self.row; do { size_t index = tree->context.index; offset = ts_length_sub(offset, tree->context.offset); + offset_row -= tree->context.offset_point.row; tree = tree->context.parent; if (!tree) break; @@ -112,14 +110,14 @@ static inline TSNode ts_node__next_sibling(TSNode self, TSNodeType type) { for (size_t i = index + 1; i < tree->child_count; i++) { const TSTree *child = tree->children[i]; TSLength child_offset = ts_length_add(offset, child->context.offset); - TSPoint child_offset_point = ts_point_add(offset_point, child->context.offset_point); + size_t child_offset_row = offset_row + child->context.offset_point.row; if (child->options.type >= type) - return ts_node_make(child, child_offset, child_offset_point); + return ts_node_make(child, child_offset, child_offset_row); size_t grandchild_count = (type == TSNodeTypeNamed) ? child->named_child_count : child->visible_child_count; if (grandchild_count > 0) - return ts_node__child(ts_node_make(child, child_offset, child_offset_point), 0, type); + return ts_node__child(ts_node_make(child, child_offset, child_offset_row), 0, type); } } while (!ts_tree_is_visible(tree)); @@ -130,7 +128,7 @@ static inline TSNode ts_node__descendent_for_range(TSNode self, size_t min, size_t max, TSNodeType type) { const TSTree *tree = ts_node__tree(self), *last_visible_tree = tree; TSLength position = ts_node__offset(self), last_visible_position = position; - TSPoint point = ts_node__offset_point(self), last_visible_point = point; + size_t offset_row = self.row, last_visible_row = offset_row; bool did_descend = true; while (did_descend) { @@ -145,15 +143,17 @@ static inline TSNode ts_node__descendent_for_range(TSNode self, size_t min, if (child->options.type >= type) { last_visible_tree = tree; last_visible_position = position; + last_visible_row = offset_row; } did_descend = true; break; } position = ts_length_add(position, ts_tree_total_size(child)); + offset_row += child->padding_point.row + child->size_point.row; } } - return ts_node_make(last_visible_tree, last_visible_position, last_visible_point); + return ts_node_make(last_visible_tree, last_visible_position, last_visible_row); } /* @@ -173,7 +173,7 @@ TSPoint ts_node_size_point(TSNode self) { } TSPoint ts_node_start_point(TSNode self) { - return ts_point_add(ts_node__offset_point(self), ts_node__tree(self)->padding_point); + return ts_point_make(self.row, ts_tree_offset_column(ts_node__tree(self))); } TSPoint ts_node_end_point(TSNode self) { @@ -209,18 +209,18 @@ bool ts_node_has_changes(TSNode self) { TSNode ts_node_parent(TSNode self) { const TSTree *tree = ts_node__tree(self); TSLength position = ts_node__offset(self); - TSPoint point = ts_node__offset_point(self); + size_t offset_row = self.row; do { position = ts_length_sub(position, tree->context.offset); - point = ts_point_sub(point, tree->context.offset_point); + offset_row -= tree->context.offset_point.row; tree = tree->context.parent; if (!tree) return ts_node__null(); } while (!ts_tree_is_visible(tree)); - return ts_node_make(tree, position, point); + return ts_node_make(tree, position, offset_row); } TSNode ts_node_child(TSNode self, size_t child_index) { diff --git a/src/runtime/node.h b/src/runtime/node.h index 3b269887..2a169df8 100644 --- a/src/runtime/node.h +++ b/src/runtime/node.h @@ -5,6 +5,6 @@ #include "runtime/length.h" #include "runtime/tree.h" -TSNode ts_node_make(const TSTree *, TSLength, TSPoint); +TSNode ts_node_make(const TSTree *, TSLength, size_t); #endif diff --git a/src/runtime/tree.c b/src/runtime/tree.c index d51ac0e1..32547532 100644 --- a/src/runtime/tree.c +++ b/src/runtime/tree.c @@ -60,9 +60,12 @@ static void ts_tree__set_children(TSTree *self, TSTree **children, if (i == 0) { self->padding = child->padding; self->size = child->size; + self->padding_point = child->padding_point; + self->size_point = child->size_point; } else { self->size = ts_length_add(ts_length_add(self->size, child->padding), child->size); + self->size_point = ts_point_add(ts_point_add(self->size_point, child->padding_point), child->size_point); } switch (child->options.type) { @@ -113,17 +116,22 @@ void ts_tree_release(TSTree *self) { } } -TSPoint ts_find_parent_offset_point(const TSTree *self) { - TSPoint size_point = self->size_point; +size_t ts_tree_offset_column(const TSTree *self) { const TSTree *parent = self; - TSPoint current_offset_point; + size_t column = self->padding_point.column; + + if (self->padding_point.row > 0) { + return column; + } do { parent = parent->context.parent; - current_offset_point = parent->context.offset_point; - } while (current_offset_point.line == 0); + if (!parent) break; - return (TSPoint){ .line = size_point.line, .column = current_offset_point.column }; + column += parent->context.offset_point.column; + } while (parent->context.offset_point.row == 0); + + return column; } TSLength ts_tree_total_size(const TSTree *self) { diff --git a/src/runtime/tree.h b/src/runtime/tree.h index cd83d0de..f373928b 100644 --- a/src/runtime/tree.h +++ b/src/runtime/tree.h @@ -52,7 +52,7 @@ bool ts_tree_eq(const TSTree *tree1, const TSTree *tree2); char *ts_tree_string(const TSTree *tree, const char **names, bool include_anonymous); -TSPoint ts_find_parent_offset_point(const TSTree *self); +size_t ts_tree_offset_column(const TSTree *self); TSLength ts_tree_total_size(const TSTree *tree); TSPoint ts_tree_offset_point(const TSTree *self); void ts_tree_prepend_children(TSTree *, size_t, TSTree **);