Fix failure to match queries with wildcard at root with range restrictions

This commit is contained in:
Max Brunsfeld 2022-06-22 15:54:06 -07:00
parent 46a2a5933f
commit 58b719541b
2 changed files with 66 additions and 5 deletions

View file

@ -2002,6 +2002,68 @@ fn test_query_matches_with_unrooted_patterns_intersecting_byte_range() {
});
}
#[test]
fn test_query_matches_with_wildcard_at_root_intersecting_byte_range() {
allocations::record(|| {
let language = get_language("python");
let query = Query::new(
language,
"
[
(_ body: (block))
(_ consequence: (block))
] @indent
",
)
.unwrap();
let source = "
class A:
def b():
if c:
d
else:
e
"
.trim();
let mut parser = Parser::new();
parser.set_language(language).unwrap();
let tree = parser.parse(source, None).unwrap();
let mut cursor = QueryCursor::new();
// After the first line of the class definition
let offset = source.find("A:").unwrap() + 2;
let matches = cursor
.set_byte_range(offset..offset)
.matches(&query, tree.root_node(), source.as_bytes())
.map(|mat| mat.captures[0].node.kind())
.collect::<Vec<_>>();
assert_eq!(matches, &["class_definition"]);
// After the first line of the function definition
let offset = source.find("b():").unwrap() + 4;
let matches = cursor
.set_byte_range(offset..offset)
.matches(&query, tree.root_node(), source.as_bytes())
.map(|mat| mat.captures[0].node.kind())
.collect::<Vec<_>>();
assert_eq!(matches, &["class_definition", "function_definition"]);
// After the first line of the if statement
let offset = source.find("c:").unwrap() + 2;
let matches = cursor
.set_byte_range(offset..offset)
.matches(&query, tree.root_node(), source.as_bytes())
.map(|mat| mat.captures[0].node.kind())
.collect::<Vec<_>>();
assert_eq!(
matches,
&["class_definition", "function_definition", "if_statement"]
);
});
}
#[test]
fn test_query_captures_within_byte_range_assigned_after_iterating() {
allocations::record(|| {