From b36ef4b7f489194248c2b23f55a386200dfd7af9 Mon Sep 17 00:00:00 2001 From: Riley Bruins Date: Sun, 29 Sep 2024 11:26:18 -0700 Subject: [PATCH] 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. --- cli/src/tests/node_test.rs | 20 +++++++++++++++++--- lib/src/node.c | 8 +++++--- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/cli/src/tests/node_test.rs b/cli/src/tests/node_test.rs index 0cbf1963..d798e726 100644 --- a/cli/src/tests/node_test.rs +++ b/cli/src/tests/node_test.rs @@ -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 = ""; 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] diff --git a/lib/src/node.c b/lib/src/node.c index 3d68c765..3f07e442 100644 --- a/lib/src/node.c +++ b/lib/src/node.c @@ -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;