fix(lib): correct extra node creation from non-zero root-alias cursors

This commit is contained in:
Amaan Qureshi 2024-08-30 22:46:03 -04:00
parent 4387e44b98
commit ee06325f67
2 changed files with 30 additions and 2 deletions

View file

@ -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 */::<D>::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()
}

View file

@ -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,