From 30e73505e1904b3859fd7bb1cbda1a612ac52e7a Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Tue, 18 Jun 2019 20:36:24 -0700 Subject: [PATCH] Fix handling of extra tokens in ts_tree_cursor_current_field_id --- cli/src/tests/tree_test.rs | 39 +++++++++++++++++++++++++++++++++++++- lib/src/tree_cursor.c | 1 + 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/cli/src/tests/tree_test.rs b/cli/src/tests/tree_test.rs index b4e8ce7c..fb461d1b 100644 --- a/cli/src/tests/tree_test.rs +++ b/cli/src/tests/tree_test.rs @@ -190,7 +190,7 @@ fn test_tree_edit() { } #[test] -fn test_tree_walk() { +fn test_tree_cursor() { let mut parser = Parser::new(); parser.set_language(get_language("rust")).unwrap(); @@ -225,6 +225,43 @@ fn test_tree_walk() { assert_eq!(cursor.node().is_named(), true); } +#[test] +fn test_tree_cursor_fields() { + let mut parser = Parser::new(); + parser.set_language(get_language("javascript")).unwrap(); + + let tree = parser + .parse("function /*1*/ bar /*2*/ () {}", None) + .unwrap(); + + let mut cursor = tree.walk(); + assert_eq!(cursor.node().kind(), "program"); + + cursor.goto_first_child(); + assert_eq!(cursor.node().kind(), "function_declaration"); + assert_eq!(cursor.field_name(), None); + + cursor.goto_first_child(); + assert_eq!(cursor.node().kind(), "function"); + assert_eq!(cursor.field_name(), None); + + cursor.goto_next_sibling(); + assert_eq!(cursor.node().kind(), "comment"); + assert_eq!(cursor.field_name(), None); + + cursor.goto_next_sibling(); + assert_eq!(cursor.node().kind(), "identifier"); + assert_eq!(cursor.field_name(), Some("name")); + + cursor.goto_next_sibling(); + assert_eq!(cursor.node().kind(), "comment"); + assert_eq!(cursor.field_name(), None); + + cursor.goto_next_sibling(); + assert_eq!(cursor.node().kind(), "formal_parameters"); + assert_eq!(cursor.field_name(), Some("parameters")); +} + #[test] fn test_tree_node_equality() { let mut parser = Parser::new(); diff --git a/lib/src/tree_cursor.c b/lib/src/tree_cursor.c index 7103fc41..ba77ebc0 100644 --- a/lib/src/tree_cursor.c +++ b/lib/src/tree_cursor.c @@ -273,6 +273,7 @@ TSFieldId ts_tree_cursor_current_field_id(const TSTreeCursor *_self) { while (field_map < field_map_end) { if ( + !ts_subtree_extra(*entry->subtree) && !field_map->inherited && field_map->child_index == entry->structural_child_index ) return field_map->field_id;