From cb4317ba8e480d16d5512854211f08fb6369db51 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Fri, 4 Feb 2022 12:38:33 -0800 Subject: [PATCH] Change goto_first_child_for_{byte,point} to compare nodes' ranges inclusively Co-Authored-By: Antonio Scandurra --- cli/src/tests/tree_test.rs | 14 +++++++------- lib/src/point.h | 4 ++++ lib/src/tree_cursor.c | 4 ++-- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/cli/src/tests/tree_test.rs b/cli/src/tests/tree_test.rs index db13ca3a..d2b1eb80 100644 --- a/cli/src/tests/tree_test.rs +++ b/cli/src/tests/tree_test.rs @@ -280,16 +280,16 @@ fn test_tree_cursor_child_for_point() { assert_eq!(c.node().kind(), "program"); assert_eq!(c.goto_first_child_for_point(Point::new(7, 0)), None); - assert_eq!(c.goto_first_child_for_point(Point::new(6, 6)), None); + assert_eq!(c.goto_first_child_for_point(Point::new(6, 7)), None); assert_eq!(c.node().kind(), "program"); // descend to expression statement - assert_eq!(c.goto_first_child_for_point(Point::new(6, 5)), Some(0)); + assert_eq!(c.goto_first_child_for_point(Point::new(6, 6)), Some(0)); assert_eq!(c.node().kind(), "expression_statement"); // step into ';' and back up assert_eq!(c.goto_first_child_for_point(Point::new(7, 0)), None); - assert_eq!(c.goto_first_child_for_point(Point::new(6, 5)), Some(1)); + assert_eq!(c.goto_first_child_for_point(Point::new(6, 6)), Some(1)); assert_eq!( (c.node().kind(), c.node().start_position()), (";", Point::new(6, 5)) @@ -312,7 +312,7 @@ fn test_tree_cursor_child_for_point() { assert!(c.goto_parent()); // step into identifier 'one' and back up - assert_eq!(c.goto_first_child_for_point(Point::new(0, 5)), Some(1)); + assert_eq!(c.goto_first_child_for_point(Point::new(1, 0)), Some(1)); assert_eq!( (c.node().kind(), c.node().start_position()), ("identifier", Point::new(1, 8)) @@ -326,7 +326,7 @@ fn test_tree_cursor_child_for_point() { assert!(c.goto_parent()); // step into first ',' and back up - assert_eq!(c.goto_first_child_for_point(Point::new(1, 11)), Some(2)); + assert_eq!(c.goto_first_child_for_point(Point::new(1, 12)), Some(2)); assert_eq!( (c.node().kind(), c.node().start_position()), (",", Point::new(1, 11)) @@ -334,7 +334,7 @@ fn test_tree_cursor_child_for_point() { assert!(c.goto_parent()); // step into identifier 'four' and back up - assert_eq!(c.goto_first_child_for_point(Point::new(4, 10)), Some(5)); + assert_eq!(c.goto_first_child_for_point(Point::new(5, 0)), Some(5)); assert_eq!( (c.node().kind(), c.node().start_position()), ("identifier", Point::new(5, 8)) @@ -354,7 +354,7 @@ fn test_tree_cursor_child_for_point() { ("]", Point::new(6, 4)) ); assert!(c.goto_parent()); - assert_eq!(c.goto_first_child_for_point(Point::new(5, 23)), Some(10)); + assert_eq!(c.goto_first_child_for_point(Point::new(6, 0)), Some(10)); assert_eq!( (c.node().kind(), c.node().start_position()), ("]", Point::new(6, 4)) diff --git a/lib/src/point.h b/lib/src/point.h index c3bf3c26..37346c8d 100644 --- a/lib/src/point.h +++ b/lib/src/point.h @@ -37,6 +37,10 @@ static inline bool point_gt(TSPoint a, TSPoint b) { return (a.row > b.row) || (a.row == b.row && a.column > b.column); } +static inline bool point_gte(TSPoint a, TSPoint b) { + return (a.row > b.row) || (a.row == b.row && a.column >= b.column); +} + static inline bool point_eq(TSPoint a, TSPoint b) { return a.row == b.row && a.column == b.column; } diff --git a/lib/src/tree_cursor.c b/lib/src/tree_cursor.c index 8bc1359c..e8dc98a9 100644 --- a/lib/src/tree_cursor.c +++ b/lib/src/tree_cursor.c @@ -139,7 +139,7 @@ int64_t ts_tree_cursor_goto_first_child_for_byte(TSTreeCursor *_self, uint32_t g CursorChildIterator iterator = ts_tree_cursor_iterate_children(self); while (ts_tree_cursor_child_iterator_next(&iterator, &entry, &visible)) { uint32_t end_byte = entry.position.bytes + ts_subtree_size(*entry.subtree).bytes; - bool at_goal = end_byte > goal_byte; + bool at_goal = end_byte >= goal_byte; uint32_t visible_child_count = ts_subtree_visible_child_count(*entry.subtree); if (at_goal) { @@ -179,7 +179,7 @@ int64_t ts_tree_cursor_goto_first_child_for_point(TSTreeCursor *_self, TSPoint g CursorChildIterator iterator = ts_tree_cursor_iterate_children(self); while (ts_tree_cursor_child_iterator_next(&iterator, &entry, &visible)) { TSPoint end_point = point_add(entry.position.extent, ts_subtree_size(*entry.subtree).extent); - bool at_goal = point_gt(end_point, goal_point); + bool at_goal = point_gte(end_point, goal_point); uint32_t visible_child_count = ts_subtree_visible_child_count(*entry.subtree); if (at_goal) { if (visible) {