diff --git a/crates/metaslang/cst/src/query/engine.rs b/crates/metaslang/cst/src/query/engine.rs index 3daff200f8..b758f438c5 100644 --- a/crates/metaslang/cst/src/query/engine.rs +++ b/crates/metaslang/cst/src/query/engine.rs @@ -668,8 +668,16 @@ impl Matcher for OneOrMoreMatcher { } else { let tail = self.children.last_mut().unwrap(); if let Some(child_matcher_result) = tail.next() { - if !child_matcher_result.cursor.is_completed() { - self.result_for_next_repetition = Some(child_matcher_result.clone()); + // Skip over trivia before saving the result for next repetition + let mut cursor = child_matcher_result.cursor.clone(); + while !cursor.is_completed() && cursor.node().is_trivia() { + cursor.irrevocably_go_to_next_sibling(); + } + if !cursor.is_completed() { + self.result_for_next_repetition = Some(MatcherResult { + cursor, + ..child_matcher_result + }); } return Some(child_matcher_result); } diff --git a/crates/testlang/outputs/cargo/tests/src/query/engine_tests.rs b/crates/testlang/outputs/cargo/tests/src/query/engine_tests.rs index 5a1e78037d..c046381363 100644 --- a/crates/testlang/outputs/cargo/tests/src/query/engine_tests.rs +++ b/crates/testlang/outputs/cargo/tests/src/query/engine_tests.rs @@ -267,6 +267,42 @@ fn test_one_or_more() { ); } +#[test] +fn test_one_or_more_anonymous() { + run_query_test( + &common_test_tree(), + "[TreeNode (@x [_])+ .]", + query_matches! { + {x: ["A", "B", "C", "DE"]} + {x: ["B", "C", "DE"]} + {x: ["C", "DE"]} + {x: ["DE"]} + }, + ); +} + +#[test] +fn test_one_or_more_anonymous_both_adjacent() { + run_query_test( + &common_test_tree(), + "[TreeNode . (@x [_])+ .]", + query_matches! { + {x: ["A", "B", "C", "DE"]} + }, + ); +} + +#[test] +fn test_one_or_more_anonymous_both_adjacent_with_trivia() { + run_query_test( + &common_test_tree_with_trivia(), + "[TreeNodeChild . @children [_]+ .]", + query_matches! { + {children: ["D", "E"]} + }, + ); +} + #[test] fn test_zero_or_more() { run_query_test(