diff options
Diffstat (limited to 'crates')
-rw-r--r-- | crates/ra_ide/src/snapshots/highlight_doctest.html | 5 | ||||
-rw-r--r-- | crates/ra_ide/src/ssr.rs | 1 | ||||
-rw-r--r-- | crates/ra_ide/src/syntax_highlighting/injection.rs | 12 | ||||
-rw-r--r-- | crates/ra_ide/src/syntax_highlighting/tests.rs | 3 | ||||
-rw-r--r-- | crates/ra_ssr/src/lib.rs | 18 | ||||
-rw-r--r-- | crates/ra_ssr/src/matching.rs | 105 | ||||
-rw-r--r-- | crates/ra_ssr/src/replacing.rs | 10 | ||||
-rw-r--r-- | crates/ra_ssr/src/tests.rs | 53 |
8 files changed, 189 insertions, 18 deletions
diff --git a/crates/ra_ide/src/snapshots/highlight_doctest.html b/crates/ra_ide/src/snapshots/highlight_doctest.html index 63199cdbe..ac546806e 100644 --- a/crates/ra_ide/src/snapshots/highlight_doctest.html +++ b/crates/ra_ide/src/snapshots/highlight_doctest.html | |||
@@ -32,7 +32,10 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd | |||
32 | .keyword.unsafe { color: #BC8383; font-weight: bold; } | 32 | .keyword.unsafe { color: #BC8383; font-weight: bold; } |
33 | .control { font-style: italic; } | 33 | .control { font-style: italic; } |
34 | </style> | 34 | </style> |
35 | <pre><code><span class="keyword">struct</span> <span class="struct declaration">Foo</span> { | 35 | <pre><code><span class="comment documentation">/// ```</span> |
36 | <span class="comment documentation">/// </span><span class="keyword">let</span> _ = <span class="string_literal">"early doctests should not go boom"</span>; | ||
37 | <span class="comment documentation">/// ```</span> | ||
38 | <span class="keyword">struct</span> <span class="struct declaration">Foo</span> { | ||
36 | <span class="field declaration">bar</span>: <span class="builtin_type">bool</span>, | 39 | <span class="field declaration">bar</span>: <span class="builtin_type">bool</span>, |
37 | } | 40 | } |
38 | 41 | ||
diff --git a/crates/ra_ide/src/ssr.rs b/crates/ra_ide/src/ssr.rs index 59c230f6c..03f18c617 100644 --- a/crates/ra_ide/src/ssr.rs +++ b/crates/ra_ide/src/ssr.rs | |||
@@ -9,6 +9,7 @@ use ra_ssr::{MatchFinder, SsrError, SsrRule}; | |||
9 | // Search and replace with named wildcards that will match any expression, type, path, pattern or item. | 9 | // Search and replace with named wildcards that will match any expression, type, path, pattern or item. |
10 | // The syntax for a structural search replace command is `<search_pattern> ==>> <replace_pattern>`. | 10 | // The syntax for a structural search replace command is `<search_pattern> ==>> <replace_pattern>`. |
11 | // A `$<name>` placeholder in the search pattern will match any AST node and `$<name>` will reference it in the replacement. | 11 | // A `$<name>` placeholder in the search pattern will match any AST node and `$<name>` will reference it in the replacement. |
12 | // Within a macro call, a placeholder will match up until whatever token follows the placeholder. | ||
12 | // Available via the command `rust-analyzer.ssr`. | 13 | // Available via the command `rust-analyzer.ssr`. |
13 | // | 14 | // |
14 | // ```rust | 15 | // ```rust |
diff --git a/crates/ra_ide/src/syntax_highlighting/injection.rs b/crates/ra_ide/src/syntax_highlighting/injection.rs index 415f24a6d..9d82b4009 100644 --- a/crates/ra_ide/src/syntax_highlighting/injection.rs +++ b/crates/ra_ide/src/syntax_highlighting/injection.rs | |||
@@ -155,17 +155,21 @@ pub(super) fn highlight_doc_comment( | |||
155 | let mut start_offset = None; | 155 | let mut start_offset = None; |
156 | let mut end_offset = None; | 156 | let mut end_offset = None; |
157 | for (line_start, orig_line_start) in range_mapping.range(..h.range.end()).rev() { | 157 | for (line_start, orig_line_start) in range_mapping.range(..h.range.end()).rev() { |
158 | // It's possible for orig_line_start - line_start to be negative. Add h.range.start() | ||
159 | // here and remove it from the end range after the loop below so that the values are | ||
160 | // always non-negative. | ||
161 | let offset = h.range.start() + orig_line_start - line_start; | ||
158 | if line_start <= &h.range.start() { | 162 | if line_start <= &h.range.start() { |
159 | start_offset.get_or_insert(orig_line_start - line_start); | 163 | start_offset.get_or_insert(offset); |
160 | break; | 164 | break; |
161 | } else { | 165 | } else { |
162 | end_offset.get_or_insert(orig_line_start - line_start); | 166 | end_offset.get_or_insert(offset); |
163 | } | 167 | } |
164 | } | 168 | } |
165 | if let Some(start_offset) = start_offset { | 169 | if let Some(start_offset) = start_offset { |
166 | h.range = TextRange::new( | 170 | h.range = TextRange::new( |
167 | h.range.start() + start_offset, | 171 | start_offset, |
168 | h.range.end() + end_offset.unwrap_or(start_offset), | 172 | h.range.end() + end_offset.unwrap_or(start_offset) - h.range.start(), |
169 | ); | 173 | ); |
170 | 174 | ||
171 | stack.add(h); | 175 | stack.add(h); |
diff --git a/crates/ra_ide/src/syntax_highlighting/tests.rs b/crates/ra_ide/src/syntax_highlighting/tests.rs index 93a276ffe..b1f48f03b 100644 --- a/crates/ra_ide/src/syntax_highlighting/tests.rs +++ b/crates/ra_ide/src/syntax_highlighting/tests.rs | |||
@@ -291,6 +291,9 @@ fn main() { | |||
291 | fn test_highlight_doctest() { | 291 | fn test_highlight_doctest() { |
292 | check_highlighting( | 292 | check_highlighting( |
293 | r#" | 293 | r#" |
294 | /// ``` | ||
295 | /// let _ = "early doctests should not go boom"; | ||
296 | /// ``` | ||
294 | struct Foo { | 297 | struct Foo { |
295 | bar: bool, | 298 | bar: bool, |
296 | } | 299 | } |
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)] |
63 | pub(crate) struct PlaceholderMatch { | 63 | pub(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 | ||
400 | impl PlaceholderMatch { | 486 | impl 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 | ||
25 | fn render_replace(match_info: &Match) -> String { | 25 | fn 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] |
430 | fn 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] | ||
441 | fn 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] | ||
456 | fn 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] | ||
430 | fn replace_function_call() { | 469 | fn 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] |
510 | fn 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] | ||
471 | fn replace_binary_op() { | 524 | fn replace_binary_op() { |
472 | assert_ssr_transform( | 525 | assert_ssr_transform( |
473 | "$a + $b ==>> $b + $a", | 526 | "$a + $b ==>> $b + $a", |