aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ssr
diff options
context:
space:
mode:
authorDavid Lattimore <[email protected]>2020-06-22 09:15:51 +0100
committerDavid Lattimore <[email protected]>2020-06-22 22:42:34 +0100
commit467af611fb5b1add25b36a2127b172240bc141cf (patch)
tree099d64f5095a74fd118266f0f96efa31412910d4 /crates/ra_ssr
parentd8842e89e9053b62c081d2995cbf43b8fd5c51b2 (diff)
SSR: Allow matching of whole macro calls
Matching within macro calls is to come later and matching of macro calls within macro calls later still.
Diffstat (limited to 'crates/ra_ssr')
-rw-r--r--crates/ra_ssr/src/lib.rs18
-rw-r--r--crates/ra_ssr/src/matching.rs105
-rw-r--r--crates/ra_ssr/src/replacing.rs10
-rw-r--r--crates/ra_ssr/src/tests.rs53
4 files changed, 173 insertions, 13 deletions
diff --git a/crates/ra_ssr/src/lib.rs b/crates/ra_ssr/src/lib.rs
index fc716ae82..da26ee669 100644
--- a/crates/ra_ssr/src/lib.rs
+++ b/crates/ra_ssr/src/lib.rs
@@ -91,14 +91,16 @@ impl<'db> MatchFinder<'db> {
91 if let Ok(mut m) = matching::get_match(false, rule, &code, restrict_range, &self.sema) { 91 if let Ok(mut m) = matching::get_match(false, rule, &code, restrict_range, &self.sema) {
92 // Continue searching in each of our placeholders. 92 // Continue searching in each of our placeholders.
93 for placeholder_value in m.placeholder_values.values_mut() { 93 for placeholder_value in m.placeholder_values.values_mut() {
94 // Don't search our placeholder if it's the entire matched node, otherwise we'd 94 if let Some(placeholder_node) = &placeholder_value.node {
95 // find the same match over and over until we got a stack overflow. 95 // Don't search our placeholder if it's the entire matched node, otherwise we'd
96 if placeholder_value.node != *code { 96 // find the same match over and over until we got a stack overflow.
97 self.find_matches( 97 if placeholder_node != code {
98 &placeholder_value.node, 98 self.find_matches(
99 restrict_range, 99 placeholder_node,
100 &mut placeholder_value.inner_matches, 100 restrict_range,
101 ); 101 &mut placeholder_value.inner_matches,
102 );
103 }
102 } 104 }
103 } 105 }
104 matches_out.matches.push(m); 106 matches_out.matches.push(m);
diff --git a/crates/ra_ssr/src/matching.rs b/crates/ra_ssr/src/matching.rs
index 265b6d793..bdaba9f1b 100644
--- a/crates/ra_ssr/src/matching.rs
+++ b/crates/ra_ssr/src/matching.rs
@@ -61,8 +61,9 @@ pub(crate) struct Var(pub String);
61/// Information about a placeholder bound in a match. 61/// Information about a placeholder bound in a match.
62#[derive(Debug)] 62#[derive(Debug)]
63pub(crate) struct PlaceholderMatch { 63pub(crate) struct PlaceholderMatch {
64 /// The node that the placeholder matched to. 64 /// The node that the placeholder matched to. If set, then we'll search for further matches
65 pub(crate) node: SyntaxNode, 65 /// within this node. It isn't set when we match tokens within a macro call's token tree.
66 pub(crate) node: Option<SyntaxNode>,
66 pub(crate) range: FileRange, 67 pub(crate) range: FileRange,
67 /// More matches, found within `node`. 68 /// More matches, found within `node`.
68 pub(crate) inner_matches: SsrMatches, 69 pub(crate) inner_matches: SsrMatches,
@@ -195,6 +196,7 @@ impl<'db, 'sema> MatchState<'db, 'sema> {
195 SyntaxKind::RECORD_FIELD_LIST => { 196 SyntaxKind::RECORD_FIELD_LIST => {
196 self.attempt_match_record_field_list(match_inputs, pattern, code) 197 self.attempt_match_record_field_list(match_inputs, pattern, code)
197 } 198 }
199 SyntaxKind::TOKEN_TREE => self.attempt_match_token_tree(match_inputs, pattern, code),
198 _ => self.attempt_match_node_children(match_inputs, pattern, code), 200 _ => self.attempt_match_node_children(match_inputs, pattern, code),
199 } 201 }
200 } 202 }
@@ -340,6 +342,90 @@ impl<'db, 'sema> MatchState<'db, 'sema> {
340 Ok(()) 342 Ok(())
341 } 343 }
342 344
345 /// Outside of token trees, a placeholder can only match a single AST node, whereas in a token
346 /// tree it can match a sequence of tokens.
347 fn attempt_match_token_tree(
348 &mut self,
349 match_inputs: &MatchInputs,
350 pattern: &SyntaxNode,
351 code: &ra_syntax::SyntaxNode,
352 ) -> Result<(), MatchFailed> {
353 let mut pattern = PatternIterator::new(pattern).peekable();
354 let mut children = code.children_with_tokens();
355 while let Some(child) = children.next() {
356 if let Some(placeholder) = pattern.peek().and_then(|p| match_inputs.get_placeholder(p))
357 {
358 pattern.next();
359 let next_pattern_token = pattern
360 .peek()
361 .and_then(|p| match p {
362 SyntaxElement::Token(t) => Some(t.clone()),
363 SyntaxElement::Node(n) => n.first_token(),
364 })
365 .map(|p| p.text().to_string());
366 let first_matched_token = child.clone();
367 let mut last_matched_token = child;
368 // Read code tokens util we reach one equal to the next token from our pattern
369 // or we reach the end of the token tree.
370 while let Some(next) = children.next() {
371 match &next {
372 SyntaxElement::Token(t) => {
373 if Some(t.to_string()) == next_pattern_token {
374 pattern.next();
375 break;
376 }
377 }
378 SyntaxElement::Node(n) => {
379 if let Some(first_token) = n.first_token() {
380 if Some(first_token.to_string()) == next_pattern_token {
381 if let Some(SyntaxElement::Node(p)) = pattern.next() {
382 // We have a subtree that starts with the next token in our pattern.
383 self.attempt_match_token_tree(match_inputs, &p, &n)?;
384 break;
385 }
386 }
387 }
388 }
389 };
390 last_matched_token = next;
391 }
392 if let Some(match_out) = &mut self.match_out {
393 match_out.placeholder_values.insert(
394 Var(placeholder.ident.to_string()),
395 PlaceholderMatch::from_range(FileRange {
396 file_id: self.sema.original_range(code).file_id,
397 range: first_matched_token
398 .text_range()
399 .cover(last_matched_token.text_range()),
400 }),
401 );
402 }
403 continue;
404 }
405 // Match literal (non-placeholder) tokens.
406 match child {
407 SyntaxElement::Token(token) => {
408 self.attempt_match_token(&mut pattern, &token)?;
409 }
410 SyntaxElement::Node(node) => match pattern.next() {
411 Some(SyntaxElement::Node(p)) => {
412 self.attempt_match_token_tree(match_inputs, &p, &node)?;
413 }
414 Some(SyntaxElement::Token(p)) => fail_match!(
415 "Pattern has token '{}', code has subtree '{}'",
416 p.text(),
417 node.text()
418 ),
419 None => fail_match!("Pattern has nothing, code has '{}'", node.text()),
420 },
421 }
422 }
423 if let Some(p) = pattern.next() {
424 fail_match!("Reached end of token tree in code, but pattern still has {:?}", p);
425 }
426 Ok(())
427 }
428
343 fn next_non_trivial(&mut self, code_it: &mut SyntaxElementChildren) -> Option<SyntaxElement> { 429 fn next_non_trivial(&mut self, code_it: &mut SyntaxElementChildren) -> Option<SyntaxElement> {
344 loop { 430 loop {
345 let c = code_it.next(); 431 let c = code_it.next();
@@ -399,7 +485,11 @@ fn recording_match_fail_reasons() -> bool {
399 485
400impl PlaceholderMatch { 486impl PlaceholderMatch {
401 fn new(node: &SyntaxNode, range: FileRange) -> Self { 487 fn new(node: &SyntaxNode, range: FileRange) -> Self {
402 Self { node: node.clone(), range, inner_matches: SsrMatches::default() } 488 Self { node: Some(node.clone()), range, inner_matches: SsrMatches::default() }
489 }
490
491 fn from_range(range: FileRange) -> Self {
492 Self { node: None, range, inner_matches: SsrMatches::default() }
403 } 493 }
404} 494}
405 495
@@ -484,7 +574,14 @@ mod tests {
484 assert_eq!(matches.matches.len(), 1); 574 assert_eq!(matches.matches.len(), 1);
485 assert_eq!(matches.matches[0].matched_node.text(), "foo(1+2)"); 575 assert_eq!(matches.matches[0].matched_node.text(), "foo(1+2)");
486 assert_eq!(matches.matches[0].placeholder_values.len(), 1); 576 assert_eq!(matches.matches[0].placeholder_values.len(), 1);
487 assert_eq!(matches.matches[0].placeholder_values[&Var("x".to_string())].node.text(), "1+2"); 577 assert_eq!(
578 matches.matches[0].placeholder_values[&Var("x".to_string())]
579 .node
580 .as_ref()
581 .unwrap()
582 .text(),
583 "1+2"
584 );
488 585
489 let edit = crate::replacing::matches_to_edit(&matches); 586 let edit = crate::replacing::matches_to_edit(&matches);
490 let mut after = input.to_string(); 587 let mut after = input.to_string();
diff --git a/crates/ra_ssr/src/replacing.rs b/crates/ra_ssr/src/replacing.rs
index 81a5e06a9..5dcde82a2 100644
--- a/crates/ra_ssr/src/replacing.rs
+++ b/crates/ra_ssr/src/replacing.rs
@@ -24,6 +24,7 @@ fn matches_to_edit_at_offset(matches: &SsrMatches, relative_start: TextSize) ->
24 24
25fn render_replace(match_info: &Match) -> String { 25fn render_replace(match_info: &Match) -> String {
26 let mut out = String::new(); 26 let mut out = String::new();
27 let match_start = match_info.matched_node.text_range().start();
27 for r in &match_info.template.tokens { 28 for r in &match_info.template.tokens {
28 match r { 29 match r {
29 PatternElement::Token(t) => out.push_str(t.text.as_str()), 30 PatternElement::Token(t) => out.push_str(t.text.as_str()),
@@ -32,7 +33,14 @@ fn render_replace(match_info: &Match) -> String {
32 match_info.placeholder_values.get(&Var(p.ident.to_string())) 33 match_info.placeholder_values.get(&Var(p.ident.to_string()))
33 { 34 {
34 let range = &placeholder_value.range.range; 35 let range = &placeholder_value.range.range;
35 let mut matched_text = placeholder_value.node.text().to_string(); 36 let mut matched_text = if let Some(node) = &placeholder_value.node {
37 node.text().to_string()
38 } else {
39 let relative_range = range.checked_sub(match_start).unwrap();
40 match_info.matched_node.text().to_string()
41 [usize::from(relative_range.start())..usize::from(relative_range.end())]
42 .to_string()
43 };
36 let edit = 44 let edit =
37 matches_to_edit_at_offset(&placeholder_value.inner_matches, range.start()); 45 matches_to_edit_at_offset(&placeholder_value.inner_matches, range.start());
38 edit.apply(&mut matched_text); 46 edit.apply(&mut matched_text);
diff --git a/crates/ra_ssr/src/tests.rs b/crates/ra_ssr/src/tests.rs
index 4b747fe18..3ee1e74e9 100644
--- a/crates/ra_ssr/src/tests.rs
+++ b/crates/ra_ssr/src/tests.rs
@@ -427,6 +427,45 @@ fn match_reordered_struct_instantiation() {
427} 427}
428 428
429#[test] 429#[test]
430fn match_macro_invocation() {
431 assert_matches("foo!($a)", "fn() {foo(foo!(foo()))}", &["foo!(foo())"]);
432 assert_matches("foo!(41, $a, 43)", "fn() {foo!(41, 42, 43)}", &["foo!(41, 42, 43)"]);
433 assert_no_match("foo!(50, $a, 43)", "fn() {foo!(41, 42, 43}");
434 assert_no_match("foo!(41, $a, 50)", "fn() {foo!(41, 42, 43}");
435 assert_matches("foo!($a())", "fn() {foo!(bar())}", &["foo!(bar())"]);
436}
437
438// When matching within a macro expansion, we only allow matches of nodes that originated from
439// the macro call, not from the macro definition.
440#[test]
441fn no_match_expression_from_macro() {
442 assert_no_match(
443 "$a.clone()",
444 r#"
445 macro_rules! m1 {
446 () => {42.clone()}
447 }
448 fn f1() {m1!()}
449 "#,
450 );
451}
452
453// We definitely don't want to allow matching of an expression that part originates from the
454// macro call `42` and part from the macro definition `.clone()`.
455#[test]
456fn no_match_split_expression() {
457 assert_no_match(
458 "$a.clone()",
459 r#"
460 macro_rules! m1 {
461 ($x:expr) => {$x.clone()}
462 }
463 fn f1() {m1!(42)}
464 "#,
465 );
466}
467
468#[test]
430fn replace_function_call() { 469fn replace_function_call() {
431 assert_ssr_transform("foo() ==>> bar()", "fn f1() {foo(); foo();}", "fn f1() {bar(); bar();}"); 470 assert_ssr_transform("foo() ==>> bar()", "fn f1() {foo(); foo();}", "fn f1() {bar(); bar();}");
432} 471}
@@ -468,6 +507,20 @@ fn replace_struct_init() {
468} 507}
469 508
470#[test] 509#[test]
510fn replace_macro_invocations() {
511 assert_ssr_transform(
512 "try!($a) ==>> $a?",
513 "fn f1() -> Result<(), E> {bar(try!(foo()));}",
514 "fn f1() -> Result<(), E> {bar(foo()?);}",
515 );
516 assert_ssr_transform(
517 "foo!($a($b)) ==>> foo($b, $a)",
518 "fn f1() {foo!(abc(def() + 2));}",
519 "fn f1() {foo(def() + 2, abc);}",
520 );
521}
522
523#[test]
471fn replace_binary_op() { 524fn replace_binary_op() {
472 assert_ssr_transform( 525 assert_ssr_transform(
473 "$a + $b ==>> $b + $a", 526 "$a + $b ==>> $b + $a",