Simplify storage of position on nodes

This commit is contained in:
Max Brunsfeld 2015-08-14 14:48:29 -07:00
parent da7eb1496b
commit 8a9626bfc5
4 changed files with 12 additions and 22 deletions

View file

@ -91,10 +91,7 @@ describe("Tree", []() {
AssertThat(children[0].offset.chars, Equals<size_t>(0));
AssertThat(children[1].tree, Equals(tree2));
AssertThat(children[1].offset.bytes, Equals<size_t>(
tree1->size.bytes + tree2->padding.bytes));
AssertThat(children[1].offset.chars, Equals<size_t>(
tree1->size.chars + tree2->padding.chars));
AssertThat(children[1].offset, Equals(ts_tree_total_size(tree1)));
});
describe("when one of the child nodes is hidden", [&]() {
@ -128,16 +125,10 @@ describe("Tree", []() {
AssertThat(children[0].offset.chars, Equals<size_t>(0));
AssertThat(children[1].tree, Equals(tree2));
AssertThat(children[1].offset.bytes, Equals<size_t>(
tree1->size.bytes + tree2->padding.bytes));
AssertThat(children[1].offset.chars, Equals<size_t>(
tree1->size.chars + tree2->padding.chars));
AssertThat(children[1].offset, Equals(ts_tree_total_size(tree1)));
AssertThat(children[2].tree, Equals(tree3));
AssertThat(children[2].offset.bytes, Equals<size_t>(
tree1->size.bytes + tree2->padding.bytes + tree2->size.bytes + tree3->padding.bytes));
AssertThat(children[2].offset.chars, Equals<size_t>(
tree1->size.chars + tree2->padding.chars + tree2->size.chars + tree3->padding.chars));
AssertThat(children[2].offset, Equals(ts_length_add(ts_tree_total_size(tree1), ts_tree_total_size(tree2))));
});
});
@ -303,3 +294,7 @@ describe("Tree", []() {
});
END_TEST
bool operator==(TSLength left, TSLength right) {
return ts_length_eq(left, right);
}

View file

@ -63,7 +63,7 @@ void ts_document_set_input_string(TSDocument *document, const char *text) {
TSNode ts_document_root_node(const TSDocument *document) {
if (document->tree)
return ts_node_make(document->tree, document->tree->padding);
return ts_node_make(document->tree, ts_length_zero());
else
return ts_node_null();
}

View file

@ -13,7 +13,7 @@ static inline const TSTree *get_tree(TSNode this) {
}
TSLength ts_node_pos(TSNode this) {
return this.position;
return ts_length_add(this.position, get_tree(this)->padding);
}
TSLength ts_node_size(TSNode this) {
@ -41,7 +41,7 @@ typedef struct {
static inline NodeWithIndex ts_node_parent_with_index(TSNode this) {
const TSTree *tree = get_tree(this);
size_t index = 0;
TSLength position = ts_length_sub(this.position, tree->padding);
TSLength position = this.position;
do {
TSTree *parent = tree->parent;
@ -64,9 +64,7 @@ static inline NodeWithIndex ts_node_parent_with_index(TSNode this) {
tree = parent;
} while (!ts_tree_is_visible(tree));
return (NodeWithIndex){
ts_node_make(tree, ts_length_add(position, tree->padding)), index
};
return (NodeWithIndex){ ts_node_make(tree, position), index };
}
TSNode ts_node_parent(TSNode this) {

View file

@ -101,9 +101,6 @@ TSTree *ts_tree_make_node(TSSymbol symbol, size_t child_count,
TSTree *child = children[i];
child->parent = result;
if (i > 0)
offset = ts_length_add(offset, child->padding);
if (ts_tree_is_visible(child)) {
visible_children[vis_i].tree = child;
visible_children[vis_i].offset = offset;
@ -119,7 +116,7 @@ TSTree *ts_tree_make_node(TSSymbol symbol, size_t child_count,
}
}
offset = ts_length_add(offset, child->size);
offset = ts_length_add(offset, ts_tree_total_size(child));
}
return result;