Fully implement ts_node_child_by_field_id
This commit is contained in:
parent
bef80c162e
commit
1d1674811c
15 changed files with 455 additions and 181 deletions
|
|
@ -343,7 +343,7 @@ fn test_node_field_names() {
|
|||
let (parser_name, parser_code) = generate_parser_for_grammar(
|
||||
r#"
|
||||
{
|
||||
"name": "test_grammar_with_refs",
|
||||
"name": "test_grammar_with_fields",
|
||||
"extras": [
|
||||
{"type": "PATTERN", "value": "\\s+"}
|
||||
],
|
||||
|
|
@ -354,30 +354,54 @@ fn test_node_field_names() {
|
|||
{
|
||||
"type": "FIELD",
|
||||
"name": "field_1",
|
||||
"content": {
|
||||
"type": "STRING",
|
||||
"value": "child-1"
|
||||
}
|
||||
"content": {"type": "STRING", "value": "child-0"}
|
||||
},
|
||||
{
|
||||
"type": "CHOICE",
|
||||
"members": [
|
||||
{"type": "STRING", "value": "child-1"},
|
||||
{"type": "BLANK"},
|
||||
|
||||
// This isn't used in the test, but prevents `_hidden_rule1`
|
||||
// from being eliminated as a unit reduction.
|
||||
{
|
||||
"type": "STRING",
|
||||
"value": "child-2"
|
||||
},
|
||||
{
|
||||
"type": "BLANK"
|
||||
"type": "ALIAS",
|
||||
"value": "x",
|
||||
"named": true,
|
||||
"content": {
|
||||
"type": "SYMBOL",
|
||||
"name": "_hidden_rule1"
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"type": "FIELD",
|
||||
"name": "field_2",
|
||||
"content": {
|
||||
"type": "STRING",
|
||||
"value": "child-3"
|
||||
}
|
||||
"content": {"type": "SYMBOL", "name": "_hidden_rule1"}
|
||||
},
|
||||
{"type": "SYMBOL", "name": "_hidden_rule2"}
|
||||
]
|
||||
},
|
||||
|
||||
// Fields pointing to hidden nodes with a single child resolve to the child.
|
||||
"_hidden_rule1": {
|
||||
"type": "CHOICE",
|
||||
"members": [
|
||||
{"type": "STRING", "value": "child-2"},
|
||||
{"type": "STRING", "value": "child-2.5"}
|
||||
]
|
||||
},
|
||||
|
||||
// Fields within hidden nodes can be referenced through the parent node.
|
||||
"_hidden_rule2": {
|
||||
"type": "SEQ",
|
||||
"members": [
|
||||
{"type": "STRING", "value": "child-3"},
|
||||
{
|
||||
"type": "FIELD",
|
||||
"name": "field_3",
|
||||
"content": {"type": "STRING", "value": "child-4"}
|
||||
}
|
||||
]
|
||||
}
|
||||
|
|
@ -391,10 +415,30 @@ fn test_node_field_names() {
|
|||
let language = get_test_language(&parser_name, &parser_code, None);
|
||||
parser.set_language(language).unwrap();
|
||||
|
||||
let tree = parser.parse("child-1 child-2 child-3", None).unwrap();
|
||||
let tree = parser.parse("child-0 child-1 child-2 child-3 child-4", None).unwrap();
|
||||
let root_node = tree.root_node();
|
||||
|
||||
assert_eq!(root_node.child_by_field_name("field_1"), root_node.child(0));
|
||||
assert_eq!(root_node.child_by_field_name("field_2"), root_node.child(2));
|
||||
assert_eq!(root_node.child_by_field_name("field_3"), root_node.child(4));
|
||||
|
||||
let mut cursor = root_node.walk();
|
||||
assert_eq!(cursor.field_name(), None);
|
||||
cursor.goto_first_child();
|
||||
assert_eq!(cursor.node().kind(), "child-0");
|
||||
assert_eq!(cursor.field_name(), Some("field_1"));
|
||||
cursor.goto_next_sibling();
|
||||
assert_eq!(cursor.node().kind(), "child-1");
|
||||
assert_eq!(cursor.field_name(), None);
|
||||
cursor.goto_next_sibling();
|
||||
assert_eq!(cursor.node().kind(), "child-2");
|
||||
assert_eq!(cursor.field_name(), Some("field_2"));
|
||||
cursor.goto_next_sibling();
|
||||
assert_eq!(cursor.node().kind(), "child-3");
|
||||
assert_eq!(cursor.field_name(), None);
|
||||
cursor.goto_next_sibling();
|
||||
assert_eq!(cursor.node().kind(), "child-4");
|
||||
assert_eq!(cursor.field_name(), Some("field_3"));
|
||||
}
|
||||
|
||||
fn get_all_nodes(tree: &Tree) -> Vec<Node> {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue