diff --git a/include/tree_sitter/runtime.h b/include/tree_sitter/runtime.h index 95bd0e42..6461a202 100644 --- a/include/tree_sitter/runtime.h +++ b/include/tree_sitter/runtime.h @@ -135,11 +135,12 @@ void ts_node_edit(TSNode *, const TSInputEdit *); TSTreeCursor ts_tree_cursor_new(TSNode); void ts_tree_cursor_delete(TSTreeCursor *); +void ts_tree_cursor_reset(TSTreeCursor *, TSNode); +TSNode ts_tree_cursor_current_node(const TSTreeCursor *); +bool ts_tree_cursor_goto_parent(TSTreeCursor *); +bool ts_tree_cursor_goto_next_sibling(TSTreeCursor *); bool ts_tree_cursor_goto_first_child(TSTreeCursor *); int64_t ts_tree_cursor_goto_first_child_for_byte(TSTreeCursor *, uint32_t); -bool ts_tree_cursor_goto_next_sibling(TSTreeCursor *); -bool ts_tree_cursor_goto_parent(TSTreeCursor *); -TSNode ts_tree_cursor_current_node(const TSTreeCursor *); uint32_t ts_language_symbol_count(const TSLanguage *); const char *ts_language_symbol_name(const TSLanguage *, TSSymbol); diff --git a/src/runtime/tree.c b/src/runtime/tree.c index 0b1ccf62..9943b799 100644 --- a/src/runtime/tree.c +++ b/src/runtime/tree.c @@ -48,7 +48,8 @@ void ts_tree_edit(TSTree *self, const TSInputEdit *edit) { TSRange *ts_tree_get_changed_ranges(const TSTree *self, const TSTree *other, uint32_t *count) { TSRange *result; - TreeCursor cursor1, cursor2; + TreeCursor cursor1 = {array_new(), NULL}; + TreeCursor cursor2 = {array_new(), NULL}; TSNode root = ts_tree_root_node(self); ts_tree_cursor_init(&cursor1, root); ts_tree_cursor_init(&cursor2, root); diff --git a/src/runtime/tree_cursor.c b/src/runtime/tree_cursor.c index 3a7df9f4..b3c765d4 100644 --- a/src/runtime/tree_cursor.c +++ b/src/runtime/tree_cursor.c @@ -66,14 +66,18 @@ static inline bool ts_tree_cursor_child_iterator_next(ChildIterator *self, // TSTreeCursor - lifecycle TSTreeCursor ts_tree_cursor_new(TSNode node) { - TSTreeCursor self; + TSTreeCursor self = {{0, 0}, NULL, NULL}; ts_tree_cursor_init((TreeCursor *)&self, node); return self; } +void ts_tree_cursor_reset(TSTreeCursor *_self, TSNode node) { + ts_tree_cursor_init((TreeCursor *)_self, node); +} + void ts_tree_cursor_init(TreeCursor *self, TSNode node) { self->tree = node.tree; - array_init(&self->stack); + array_clear(&self->stack); array_push(&self->stack, ((TreeCursorEntry) { .subtree = (const Subtree *)node.id, .position = {