From e265929f90b6c961466f96679d222c01b1d53bbf Mon Sep 17 00:00:00 2001 From: Daumantas Kavolis Date: Wed, 25 Oct 2023 10:19:03 +0300 Subject: [PATCH 1/3] Fix `goto_previous_sibling` with padded first children --- cli/src/tests/tree_test.rs | 30 ++++++++++++++++++++++++++++++ lib/src/tree_cursor.c | 6 +++++- 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/cli/src/tests/tree_test.rs b/cli/src/tests/tree_test.rs index c63b588b..161f65e9 100644 --- a/cli/src/tests/tree_test.rs +++ b/cli/src/tests/tree_test.rs @@ -376,6 +376,36 @@ fn test_tree_cursor() { assert_eq!(copy.node().kind(), "struct_item"); } +#[test] +fn test_tree_cursor_previous_sibling() { + let mut parser = Parser::new(); + parser.set_language(get_language("rust")).unwrap(); + + let text = " + // Hi there + // This is fun! + // Another one! +"; + let tree = parser.parse(text, None).unwrap(); + + let mut cursor = tree.walk(); + assert_eq!(cursor.node().kind(), "source_file"); + + assert!(cursor.goto_last_child()); + assert_eq!(cursor.node().kind(), "line_comment"); + assert_eq!(cursor.node().utf8_text(text.as_bytes()).unwrap(), "// Another one!"); + + assert!(cursor.goto_previous_sibling()); + assert_eq!(cursor.node().kind(), "line_comment"); + assert_eq!(cursor.node().utf8_text(text.as_bytes()).unwrap(), "// This is fun!"); + + assert!(cursor.goto_previous_sibling()); + assert_eq!(cursor.node().kind(), "line_comment"); + assert_eq!(cursor.node().utf8_text(text.as_bytes()).unwrap(), "// Hi there"); + + assert!(!cursor.goto_previous_sibling()); +} + #[test] fn test_tree_cursor_fields() { let mut parser = Parser::new(); diff --git a/lib/src/tree_cursor.c b/lib/src/tree_cursor.c index 25eca482..f08b9692 100644 --- a/lib/src/tree_cursor.c +++ b/lib/src/tree_cursor.c @@ -377,7 +377,11 @@ TreeCursorStep ts_tree_cursor_goto_previous_sibling_internal(TSTreeCursor *_self position = parent->position; uint32_t child_index = array_back(&self->stack)->child_index; const Subtree *children = ts_subtree_children((*(parent->subtree))); - for (uint32_t i = 0; i < child_index; ++i) { + + // skip first child padding since its position should match the position of the parent + if (child_index > 0) + position = length_add(position, ts_subtree_size(children[0])); + for (uint32_t i = 1; i < child_index; ++i) { position = length_add(position, ts_subtree_total_size(children[i])); } if (child_index > 0) From e26e23fd0e6baad6b5285ba1d990142b1f0ff1aa Mon Sep 17 00:00:00 2001 From: Daumantas Kavolis Date: Wed, 25 Oct 2023 10:24:35 +0300 Subject: [PATCH 2/3] Fix formatting --- cli/src/tests/tree_test.rs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/cli/src/tests/tree_test.rs b/cli/src/tests/tree_test.rs index 161f65e9..f3792138 100644 --- a/cli/src/tests/tree_test.rs +++ b/cli/src/tests/tree_test.rs @@ -393,15 +393,24 @@ fn test_tree_cursor_previous_sibling() { assert!(cursor.goto_last_child()); assert_eq!(cursor.node().kind(), "line_comment"); - assert_eq!(cursor.node().utf8_text(text.as_bytes()).unwrap(), "// Another one!"); + assert_eq!( + cursor.node().utf8_text(text.as_bytes()).unwrap(), + "// Another one!" + ); assert!(cursor.goto_previous_sibling()); assert_eq!(cursor.node().kind(), "line_comment"); - assert_eq!(cursor.node().utf8_text(text.as_bytes()).unwrap(), "// This is fun!"); + assert_eq!( + cursor.node().utf8_text(text.as_bytes()).unwrap(), + "// This is fun!" + ); assert!(cursor.goto_previous_sibling()); assert_eq!(cursor.node().kind(), "line_comment"); - assert_eq!(cursor.node().utf8_text(text.as_bytes()).unwrap(), "// Hi there"); + assert_eq!( + cursor.node().utf8_text(text.as_bytes()).unwrap(), + "// Hi there" + ); assert!(!cursor.goto_previous_sibling()); } From 143ed959c9a4d591538413b59b935fdba1066a5c Mon Sep 17 00:00:00 2001 From: Daumantas Kavolis Date: Wed, 25 Oct 2023 10:47:55 +0300 Subject: [PATCH 3/3] Use single if block --- lib/src/tree_cursor.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/src/tree_cursor.c b/lib/src/tree_cursor.c index f08b9692..63d22c8b 100644 --- a/lib/src/tree_cursor.c +++ b/lib/src/tree_cursor.c @@ -378,14 +378,14 @@ TreeCursorStep ts_tree_cursor_goto_previous_sibling_internal(TSTreeCursor *_self uint32_t child_index = array_back(&self->stack)->child_index; const Subtree *children = ts_subtree_children((*(parent->subtree))); - // skip first child padding since its position should match the position of the parent - if (child_index > 0) + if (child_index > 0) { + // skip first child padding since its position should match the position of the parent position = length_add(position, ts_subtree_size(children[0])); - for (uint32_t i = 1; i < child_index; ++i) { - position = length_add(position, ts_subtree_total_size(children[i])); - } - if (child_index > 0) + for (uint32_t i = 1; i < child_index; ++i) { + position = length_add(position, ts_subtree_total_size(children[i])); + } position = length_add(position, ts_subtree_padding(children[child_index])); + } array_back(&self->stack)->position = position;