From 40703f110c7f16650b686fc4c56ab128cf61e449 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Wed, 15 Feb 2023 14:40:36 -0800 Subject: [PATCH] Fix bug in maintenance of query cursor's tree depth --- cli/src/tests/query_test.rs | 3 --- lib/src/query.c | 24 +++++++++++++++--------- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/cli/src/tests/query_test.rs b/cli/src/tests/query_test.rs index 63dea5a6..c691df30 100644 --- a/cli/src/tests/query_test.rs +++ b/cli/src/tests/query_test.rs @@ -1876,7 +1876,6 @@ fn test_query_matches_within_byte_range() { cursor .set_byte_range(0..8) .matches(&query, tree.root_node(), source.as_bytes()); - assert_eq!( collect_matches(matches, &query, source), &[ @@ -1890,7 +1889,6 @@ fn test_query_matches_within_byte_range() { cursor .set_byte_range(5..15) .matches(&query, tree.root_node(), source.as_bytes()); - assert_eq!( collect_matches(matches, &query, source), &[ @@ -1904,7 +1902,6 @@ fn test_query_matches_within_byte_range() { cursor .set_byte_range(12..0) .matches(&query, tree.root_node(), source.as_bytes()); - assert_eq!( collect_matches(matches, &query, source), &[ diff --git a/lib/src/query.c b/lib/src/query.c index 04a59f9a..b2450ce2 100644 --- a/lib/src/query.c +++ b/lib/src/query.c @@ -3393,21 +3393,28 @@ static inline bool ts_query_cursor__advance( // Exit the current node. if (self->ascending) { - LOG( - "leave node. depth:%u, type:%s\n", - self->depth, - ts_node_type(ts_tree_cursor_current_node(&self->cursor)) - ); + if (self->on_visible_node) { + LOG( + "leave node. depth:%u, type:%s\n", + self->depth, + ts_node_type(ts_tree_cursor_current_node(&self->cursor)) + ); + } // Leave this node by stepping to its next sibling or to its parent. switch (ts_tree_cursor_goto_next_sibling_internal(&self->cursor)) { case TreeCursorStepVisible: - self->on_visible_node = true; + if (!self->on_visible_node) { + self->depth++; + self->on_visible_node = true; + } self->ascending = false; break; case TreeCursorStepHidden: - self->depth--; - self->on_visible_node = false; + if (self->on_visible_node) { + self->depth--; + self->on_visible_node = false; + } self->ascending = false; break; default: @@ -3467,7 +3474,6 @@ static inline bool ts_query_cursor__advance( // Get the properties of the current node. TSNode node = ts_tree_cursor_current_node(&self->cursor); TSNode parent_node = ts_tree_cursor_parent_node(&self->cursor); - bool parent_precedes_range = !ts_node_is_null(parent_node) && ( ts_node_end_byte(parent_node) <= self->start_byte || point_lte(ts_node_end_point(parent_node), self->start_point)