diff --git a/cli/src/tests/tree_test.rs b/cli/src/tests/tree_test.rs index 3e4b2775..62cc23cd 100644 --- a/cli/src/tests/tree_test.rs +++ b/cli/src/tests/tree_test.rs @@ -702,6 +702,33 @@ fn test_consistency_with_mid_codepoint_edit() { assert_eq!(tree3.root_node().to_sexp(), tree.root_node().to_sexp()); } +#[test] +fn test_tree_cursor_on_aliased_root_with_extra_child() { + let source = r#" +fn main() { + C/* hi */::::E; +} +"#; + + let mut parser = Parser::new(); + parser.set_language(&get_language("rust")).unwrap(); + + let tree = parser.parse(source, None).unwrap(); + + let function = tree.root_node().child(0).unwrap(); + let block = function.child(3).unwrap(); + let expression_statement = block.child(1).unwrap(); + let scoped_identifier = expression_statement.child(0).unwrap(); + let generic_type = scoped_identifier.child(0).unwrap(); + assert_eq!(generic_type.kind(), "generic_type"); + + let mut cursor = generic_type.walk(); + assert!(cursor.goto_first_child()); + assert_eq!(cursor.node().kind(), "type_identifier"); + assert!(cursor.goto_next_sibling()); + assert_eq!(cursor.node().kind(), "block_comment"); +} + fn index_of(text: &[u8], substring: &str) -> usize { str::from_utf8(text).unwrap().find(substring).unwrap() } diff --git a/lib/src/tree_cursor.c b/lib/src/tree_cursor.c index ddd7d66b..24416663 100644 --- a/lib/src/tree_cursor.c +++ b/lib/src/tree_cursor.c @@ -475,8 +475,9 @@ uint32_t ts_tree_cursor_current_descendant_index(const TSTreeCursor *_self) { TSNode ts_tree_cursor_current_node(const TSTreeCursor *_self) { const TreeCursor *self = (const TreeCursor *)_self; TreeCursorEntry *last_entry = array_back(&self->stack); - TSSymbol alias_symbol = self->root_alias_symbol; - if (self->stack.size > 1 && !ts_subtree_extra(*last_entry->subtree)) { + bool is_extra = ts_subtree_extra(*last_entry->subtree); + TSSymbol alias_symbol = is_extra ? 0 : self->root_alias_symbol; + if (self->stack.size > 1 && !is_extra) { TreeCursorEntry *parent_entry = &self->stack.contents[self->stack.size - 2]; alias_symbol = ts_language_alias_at( self->tree->language,