Merge pull request #2725 from daumantas-kavolis-sensmetry/fix/cursor-previous-sibling

Fix `goto_previous_sibling` with padded first children
This commit is contained in:
Max Brunsfeld 2023-10-27 11:53:16 +02:00 committed by GitHub
commit ddfbbb00a1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 47 additions and 4 deletions

View file

@ -376,6 +376,45 @@ fn test_tree_cursor() {
assert_eq!(copy.node().kind(), "struct_item"); 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] #[test]
fn test_tree_cursor_fields() { fn test_tree_cursor_fields() {
let mut parser = Parser::new(); let mut parser = Parser::new();

View file

@ -377,11 +377,15 @@ TreeCursorStep ts_tree_cursor_goto_previous_sibling_internal(TSTreeCursor *_self
position = parent->position; position = parent->position;
uint32_t child_index = array_back(&self->stack)->child_index; uint32_t child_index = array_back(&self->stack)->child_index;
const Subtree *children = ts_subtree_children((*(parent->subtree))); const Subtree *children = ts_subtree_children((*(parent->subtree)));
for (uint32_t i = 0; i < child_index; ++i) {
position = length_add(position, ts_subtree_total_size(children[i])); if (child_index > 0) {
} // 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]));
}
position = length_add(position, ts_subtree_padding(children[child_index])); position = length_add(position, ts_subtree_padding(children[child_index]));
}
array_back(&self->stack)->position = position; array_back(&self->stack)->position = position;