Add function for retrieving a leaf node by position
This commit is contained in:
parent
1ecafb874e
commit
466daeaf99
5 changed files with 58 additions and 17 deletions
|
|
@ -1,8 +1,7 @@
|
|||
#include "runtime/node.h"
|
||||
#include "tree_sitter/parser.h"
|
||||
#include "runtime/tree.h"
|
||||
|
||||
TSNode * ts_node_make(const TSTree *tree, TSNode *parent, size_t index, size_t start_position, TSParserConfig *config) {
|
||||
TSNode * ts_node_make(const TSTree *tree, TSNode *parent, size_t index, size_t start_position, const char **names) {
|
||||
if (parent) ts_node_retain(parent);
|
||||
TSNode *result = malloc(sizeof(TSNode));
|
||||
*result = (TSNode) {
|
||||
|
|
@ -11,15 +10,15 @@ TSNode * ts_node_make(const TSTree *tree, TSNode *parent, size_t index, size_t s
|
|||
.index = index,
|
||||
.content = tree,
|
||||
.start_position = start_position,
|
||||
.config = config
|
||||
.names = names,
|
||||
};
|
||||
return result;
|
||||
}
|
||||
|
||||
TSNode * ts_node_make_root(const TSTree *tree, TSParserConfig *config) {
|
||||
TSNode * ts_node_make_root(const TSTree *tree, const char **names) {
|
||||
while (ts_tree_is_wrapper(tree))
|
||||
tree = tree->children[0];
|
||||
return ts_node_make(tree, NULL, 0, 0, config);
|
||||
return ts_node_make(tree, NULL, 0, 0, names);
|
||||
}
|
||||
|
||||
void ts_node_retain(TSNode *node) {
|
||||
|
|
@ -47,11 +46,11 @@ int ts_node_eq(const TSNode *left, const TSNode *right) {
|
|||
}
|
||||
|
||||
const char * ts_node_name(const TSNode *node) {
|
||||
return node->config->symbol_names[node->content->symbol];
|
||||
return node->names[node->content->symbol];
|
||||
}
|
||||
|
||||
const char * ts_node_string(const TSNode *node) {
|
||||
return ts_tree_string(node->content, node->config->symbol_names);
|
||||
return ts_tree_string(node->content, node->names);
|
||||
}
|
||||
|
||||
TSNode * ts_node_parent(TSNode *child) {
|
||||
|
|
@ -72,9 +71,25 @@ TSNode * ts_node_child(TSNode *parent, size_t index) {
|
|||
if (child_count <= index)
|
||||
return NULL;
|
||||
size_t position = parent->start_position + children[index].position;
|
||||
return ts_node_make(children[index].tree, parent, index, position, parent->config);
|
||||
return ts_node_make(children[index].tree, parent, index, position, parent->names);
|
||||
}
|
||||
|
||||
TSNode * ts_node_leaf_at_pos(TSNode *parent, size_t child_index) {
|
||||
return NULL;
|
||||
TSNode * ts_node_leaf_at_pos(TSNode *parent, size_t position) {
|
||||
size_t child_count;
|
||||
TSChildWithPosition *children = ts_tree_visible_children(parent->content, &child_count);
|
||||
for (size_t i = 0; i < child_count; i++) {
|
||||
TSChildWithPosition child = children[i];
|
||||
size_t child_left = child.position + child.tree->offset;
|
||||
if (child_left > position)
|
||||
break;
|
||||
if (child_left + child.tree->size > position) {
|
||||
TSNode *node = ts_node_make(child.tree, parent, i, child.position, parent->names);
|
||||
TSNode *result = ts_node_leaf_at_pos(node, position);
|
||||
ts_node_release(node);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
ts_node_retain(parent);
|
||||
return parent;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue