query: Optimize handling of patterns with a wildcard at the root

Avoid adding and removing states for these patterns on every node in the tree
by just skipping the wildcard step of the matching process
This commit is contained in:
Max Brunsfeld 2020-03-16 14:02:31 -07:00
parent 1b3a67834b
commit 65f2874b9e
3 changed files with 68 additions and 23 deletions

View file

@ -439,6 +439,10 @@ fn test_query_matches_with_named_wildcard() {
fn test_query_matches_with_wildcard_at_the_root() {
allocations::record(|| {
let language = get_language("javascript");
let mut cursor = QueryCursor::new();
let mut parser = Parser::new();
parser.set_language(language).unwrap();
let query = Query::new(
language,
"
@ -453,16 +457,36 @@ fn test_query_matches_with_wildcard_at_the_root() {
let source = "/* one */ var x; /* two */ function y() {} /* three */ class Z {}";
let mut parser = Parser::new();
parser.set_language(language).unwrap();
let tree = parser.parse(source, None).unwrap();
let mut cursor = QueryCursor::new();
let matches = cursor.matches(&query, tree.root_node(), to_callback(source));
assert_eq!(
collect_matches(matches, &query, source),
&[(0, vec![("doc", "/* two */"), ("name", "y")]),]
);
let query = Query::new(
language,
"
(* (string) @a)
(* (number) @b)
(* (true) @c)
(* (false) @d)
",
)
.unwrap();
let source = "['hi', x(true), {y: false}]";
let tree = parser.parse(source, None).unwrap();
let matches = cursor.matches(&query, tree.root_node(), to_callback(source));
assert_eq!(
collect_matches(matches, &query, source),
&[
(0, vec![("a", "'hi'")]),
(2, vec![("c", "true")]),
(3, vec![("d", "false")]),
]
);
});
}

View file

@ -78,7 +78,7 @@ fn test_tags_javascript() {
.
(method_definition
name: (property_identifier) @name) @method)
(select-adjacent! @doc @method)
; (select-adjacent! @doc @method)
(strip! @doc "(^[/\\*\\s]*)|([/\\*\\s]*$)"))
"#,
"",