fix(lib)!: child_containing_descendant now returns direct children

Previously, `child_containing_descendant` would return `null` when
called on a node's direct parent. In my opinion, this doesn't make much
sense; it seems like a node would contain itself. This (breaking)
commit changes the function so that it can return direct children.
This commit is contained in:
Riley Bruins 2024-09-29 11:26:18 -07:00 committed by Amaan Qureshi
parent 6b1ebd3d29
commit b36ef4b7f4
2 changed files with 22 additions and 6 deletions

View file

@ -182,7 +182,11 @@ fn test_node_child() {
object_node.child_containing_descendant(null_node).unwrap(),
pair_node
);
assert_eq!(pair_node.child_containing_descendant(null_node), None);
assert_eq!(
pair_node.child_containing_descendant(null_node).unwrap(),
null_node
);
assert_eq!(null_node.child_containing_descendant(null_node), None);
}
#[test]
@ -287,7 +291,13 @@ fn test_parent_of_zero_width_node() {
root.child_containing_descendant(block).unwrap(),
function_definition
);
assert_eq!(function_definition.child_containing_descendant(block), None);
assert_eq!(
function_definition
.child_containing_descendant(block)
.unwrap(),
block
);
assert_eq!(block.child_containing_descendant(block), None);
let code = "<script></script>";
parser.set_language(&get_language("html")).unwrap();
@ -480,7 +490,11 @@ fn test_node_named_child() {
object_node.child_containing_descendant(null_node).unwrap(),
pair_node
);
assert_eq!(pair_node.child_containing_descendant(null_node), None);
assert_eq!(
pair_node.child_containing_descendant(null_node).unwrap(),
null_node
);
assert_eq!(null_node.child_containing_descendant(null_node), None);
}
#[test]

View file

@ -550,7 +550,7 @@ TSNode ts_node_parent(TSNode self) {
while (true) {
TSNode next_node = ts_node_child_containing_descendant(node, self);
if (ts_node_is_null(next_node)) break;
if (next_node.id == self.id) break;
node = next_node;
}
@ -567,10 +567,12 @@ TSNode ts_node_child_containing_descendant(TSNode self, TSNode subnode) {
if (
!ts_node_child_iterator_next(&iter, &self)
|| ts_node_start_byte(self) > start_byte
|| self.id == subnode.id
) {
return ts_node__null();
}
if (self.id == subnode.id) {
return self;
}
// Here we check the current self node and *all* of its zero-width token siblings that follow.
// If any of these nodes contain the target subnode, we return that node. Otherwise, we restore the node we started at
@ -585,7 +587,7 @@ TSNode ts_node_child_containing_descendant(TSNode self, TSNode subnode) {
}
ts_node_child_iterator_next(&iter, &self);
if (self.id == subnode.id) {
return ts_node__null();
return self;
}
}
self = old;