aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ssr/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_ssr/src')
-rw-r--r--crates/ra_ssr/src/lib.rs122
-rw-r--r--crates/ra_ssr/src/matching.rs591
-rw-r--r--crates/ra_ssr/src/parsing.rs272
-rw-r--r--crates/ra_ssr/src/replacing.rs63
-rw-r--r--crates/ra_ssr/src/tests.rs549
5 files changed, 1597 insertions, 0 deletions
diff --git a/crates/ra_ssr/src/lib.rs b/crates/ra_ssr/src/lib.rs
new file mode 100644
index 000000000..da26ee669
--- /dev/null
+++ b/crates/ra_ssr/src/lib.rs
@@ -0,0 +1,122 @@
1//! Structural Search Replace
2//!
3//! Allows searching the AST for code that matches one or more patterns and then replacing that code
4//! based on a template.
5
6mod matching;
7mod parsing;
8mod replacing;
9#[cfg(test)]
10mod tests;
11
12use crate::matching::Match;
13use hir::Semantics;
14use ra_db::{FileId, FileRange};
15use ra_syntax::{AstNode, SmolStr, SyntaxNode};
16use ra_text_edit::TextEdit;
17use rustc_hash::FxHashMap;
18
19// A structured search replace rule. Create by calling `parse` on a str.
20#[derive(Debug)]
21pub struct SsrRule {
22 /// A structured pattern that we're searching for.
23 pattern: SsrPattern,
24 /// What we'll replace it with.
25 template: parsing::SsrTemplate,
26}
27
28#[derive(Debug)]
29struct SsrPattern {
30 raw: parsing::RawSearchPattern,
31 /// Placeholders keyed by the stand-in ident that we use in Rust source code.
32 placeholders_by_stand_in: FxHashMap<SmolStr, parsing::Placeholder>,
33 // We store our search pattern, parsed as each different kind of thing we can look for. As we
34 // traverse the AST, we get the appropriate one of these for the type of node we're on. For many
35 // search patterns, only some of these will be present.
36 expr: Option<SyntaxNode>,
37 type_ref: Option<SyntaxNode>,
38 item: Option<SyntaxNode>,
39 path: Option<SyntaxNode>,
40 pattern: Option<SyntaxNode>,
41}
42
43#[derive(Debug, PartialEq)]
44pub struct SsrError(String);
45
46#[derive(Debug, Default)]
47pub struct SsrMatches {
48 matches: Vec<Match>,
49}
50
51/// Searches a crate for pattern matches and possibly replaces them with something else.
52pub struct MatchFinder<'db> {
53 /// Our source of information about the user's code.
54 sema: Semantics<'db, ra_ide_db::RootDatabase>,
55 rules: Vec<SsrRule>,
56}
57
58impl<'db> MatchFinder<'db> {
59 pub fn new(db: &'db ra_ide_db::RootDatabase) -> MatchFinder<'db> {
60 MatchFinder { sema: Semantics::new(db), rules: Vec::new() }
61 }
62
63 pub fn add_rule(&mut self, rule: SsrRule) {
64 self.rules.push(rule);
65 }
66
67 pub fn edits_for_file(&self, file_id: FileId) -> Option<TextEdit> {
68 let matches = self.find_matches_in_file(file_id);
69 if matches.matches.is_empty() {
70 None
71 } else {
72 Some(replacing::matches_to_edit(&matches))
73 }
74 }
75
76 fn find_matches_in_file(&self, file_id: FileId) -> SsrMatches {
77 let file = self.sema.parse(file_id);
78 let code = file.syntax();
79 let mut matches = SsrMatches::default();
80 self.find_matches(code, &None, &mut matches);
81 matches
82 }
83
84 fn find_matches(
85 &self,
86 code: &SyntaxNode,
87 restrict_range: &Option<FileRange>,
88 matches_out: &mut SsrMatches,
89 ) {
90 for rule in &self.rules {
91 if let Ok(mut m) = matching::get_match(false, rule, &code, restrict_range, &self.sema) {
92 // Continue searching in each of our placeholders.
93 for placeholder_value in m.placeholder_values.values_mut() {
94 if let Some(placeholder_node) = &placeholder_value.node {
95 // Don't search our placeholder if it's the entire matched node, otherwise we'd
96 // find the same match over and over until we got a stack overflow.
97 if placeholder_node != code {
98 self.find_matches(
99 placeholder_node,
100 restrict_range,
101 &mut placeholder_value.inner_matches,
102 );
103 }
104 }
105 }
106 matches_out.matches.push(m);
107 return;
108 }
109 }
110 for child in code.children() {
111 self.find_matches(&child, restrict_range, matches_out);
112 }
113 }
114}
115
116impl std::fmt::Display for SsrError {
117 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
118 write!(f, "Parse error: {}", self.0)
119 }
120}
121
122impl std::error::Error for SsrError {}
diff --git a/crates/ra_ssr/src/matching.rs b/crates/ra_ssr/src/matching.rs
new file mode 100644
index 000000000..bdaba9f1b
--- /dev/null
+++ b/crates/ra_ssr/src/matching.rs
@@ -0,0 +1,591 @@
1//! This module is responsible for matching a search pattern against a node in the AST. In the
2//! process of matching, placeholder values are recorded.
3
4use crate::{
5 parsing::{Placeholder, SsrTemplate},
6 SsrMatches, SsrPattern, SsrRule,
7};
8use hir::Semantics;
9use ra_db::FileRange;
10use ra_syntax::ast::{AstNode, AstToken};
11use ra_syntax::{
12 ast, SyntaxElement, SyntaxElementChildren, SyntaxKind, SyntaxNode, SyntaxToken, TextRange,
13};
14use rustc_hash::FxHashMap;
15use std::{cell::Cell, iter::Peekable};
16
17// Creates a match error. If we're currently attempting to match some code that we thought we were
18// going to match, as indicated by the --debug-snippet flag, then populate the reason field.
19macro_rules! match_error {
20 ($e:expr) => {{
21 MatchFailed {
22 reason: if recording_match_fail_reasons() {
23 Some(format!("{}", $e))
24 } else {
25 None
26 }
27 }
28 }};
29 ($fmt:expr, $($arg:tt)+) => {{
30 MatchFailed {
31 reason: if recording_match_fail_reasons() {
32 Some(format!($fmt, $($arg)+))
33 } else {
34 None
35 }
36 }
37 }};
38}
39
40// Fails the current match attempt, recording the supplied reason if we're recording match fail reasons.
41macro_rules! fail_match {
42 ($($args:tt)*) => {return Err(match_error!($($args)*))};
43}
44
45/// Information about a match that was found.
46#[derive(Debug)]
47pub(crate) struct Match {
48 pub(crate) range: TextRange,
49 pub(crate) matched_node: SyntaxNode,
50 pub(crate) placeholder_values: FxHashMap<Var, PlaceholderMatch>,
51 pub(crate) ignored_comments: Vec<ast::Comment>,
52 // A copy of the template for the rule that produced this match. We store this on the match for
53 // if/when we do replacement.
54 pub(crate) template: SsrTemplate,
55}
56
57/// Represents a `$var` in an SSR query.
58#[derive(Debug, Clone, PartialEq, Eq, Hash)]
59pub(crate) struct Var(pub String);
60
61/// Information about a placeholder bound in a match.
62#[derive(Debug)]
63pub(crate) struct PlaceholderMatch {
64 /// The node that the placeholder matched to. If set, then we'll search for further matches
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>,
67 pub(crate) range: FileRange,
68 /// More matches, found within `node`.
69 pub(crate) inner_matches: SsrMatches,
70}
71
72#[derive(Debug)]
73pub(crate) struct MatchFailureReason {
74 pub(crate) reason: String,
75}
76
77/// An "error" indicating that matching failed. Use the fail_match! macro to create and return this.
78#[derive(Clone)]
79pub(crate) struct MatchFailed {
80 /// The reason why we failed to match. Only present when debug_active true in call to
81 /// `get_match`.
82 pub(crate) reason: Option<String>,
83}
84
85/// Checks if `code` matches the search pattern found in `search_scope`, returning information about
86/// the match, if it does. Since we only do matching in this module and searching is done by the
87/// parent module, we don't populate nested matches.
88pub(crate) fn get_match(
89 debug_active: bool,
90 rule: &SsrRule,
91 code: &SyntaxNode,
92 restrict_range: &Option<FileRange>,
93 sema: &Semantics<ra_ide_db::RootDatabase>,
94) -> Result<Match, MatchFailed> {
95 record_match_fails_reasons_scope(debug_active, || {
96 MatchState::try_match(rule, code, restrict_range, sema)
97 })
98}
99
100/// Inputs to matching. This cannot be part of `MatchState`, since we mutate `MatchState` and in at
101/// least one case need to hold a borrow of a placeholder from the input pattern while calling a
102/// mutable `MatchState` method.
103struct MatchInputs<'pattern> {
104 ssr_pattern: &'pattern SsrPattern,
105}
106
107/// State used while attempting to match our search pattern against a particular node of the AST.
108struct MatchState<'db, 'sema> {
109 sema: &'sema Semantics<'db, ra_ide_db::RootDatabase>,
110 /// If any placeholders come from anywhere outside of this range, then the match will be
111 /// rejected.
112 restrict_range: Option<FileRange>,
113 /// The match that we're building. We do two passes for a successful match. On the first pass,
114 /// this is None so that we can avoid doing things like storing copies of what placeholders
115 /// matched to. If that pass succeeds, then we do a second pass where we collect those details.
116 /// This means that if we have a pattern like `$a.foo()` we won't do an insert into the
117 /// placeholders map for every single method call in the codebase. Instead we'll discard all the
118 /// method calls that aren't calls to `foo` on the first pass and only insert into the
119 /// placeholders map on the second pass. Likewise for ignored comments.
120 match_out: Option<Match>,
121}
122
123impl<'db, 'sema> MatchState<'db, 'sema> {
124 fn try_match(
125 rule: &SsrRule,
126 code: &SyntaxNode,
127 restrict_range: &Option<FileRange>,
128 sema: &'sema Semantics<'db, ra_ide_db::RootDatabase>,
129 ) -> Result<Match, MatchFailed> {
130 let mut match_state =
131 MatchState { sema, restrict_range: restrict_range.clone(), match_out: None };
132 let match_inputs = MatchInputs { ssr_pattern: &rule.pattern };
133 let pattern_tree = rule.pattern.tree_for_kind(code.kind())?;
134 // First pass at matching, where we check that node types and idents match.
135 match_state.attempt_match_node(&match_inputs, &pattern_tree, code)?;
136 match_state.validate_range(&sema.original_range(code))?;
137 match_state.match_out = Some(Match {
138 range: sema.original_range(code).range,
139 matched_node: code.clone(),
140 placeholder_values: FxHashMap::default(),
141 ignored_comments: Vec::new(),
142 template: rule.template.clone(),
143 });
144 // Second matching pass, where we record placeholder matches, ignored comments and maybe do
145 // any other more expensive checks that we didn't want to do on the first pass.
146 match_state.attempt_match_node(&match_inputs, &pattern_tree, code)?;
147 Ok(match_state.match_out.unwrap())
148 }
149
150 /// Checks that `range` is within the permitted range if any. This is applicable when we're
151 /// processing a macro expansion and we want to fail the match if we're working with a node that
152 /// didn't originate from the token tree of the macro call.
153 fn validate_range(&self, range: &FileRange) -> Result<(), MatchFailed> {
154 if let Some(restrict_range) = &self.restrict_range {
155 if restrict_range.file_id != range.file_id
156 || !restrict_range.range.contains_range(range.range)
157 {
158 fail_match!("Node originated from a macro");
159 }
160 }
161 Ok(())
162 }
163
164 fn attempt_match_node(
165 &mut self,
166 match_inputs: &MatchInputs,
167 pattern: &SyntaxNode,
168 code: &SyntaxNode,
169 ) -> Result<(), MatchFailed> {
170 // Handle placeholders.
171 if let Some(placeholder) =
172 match_inputs.get_placeholder(&SyntaxElement::Node(pattern.clone()))
173 {
174 if self.match_out.is_none() {
175 return Ok(());
176 }
177 let original_range = self.sema.original_range(code);
178 // We validated the range for the node when we started the match, so the placeholder
179 // probably can't fail range validation, but just to be safe...
180 self.validate_range(&original_range)?;
181 if let Some(match_out) = &mut self.match_out {
182 match_out.placeholder_values.insert(
183 Var(placeholder.ident.to_string()),
184 PlaceholderMatch::new(code, original_range),
185 );
186 }
187 return Ok(());
188 }
189 // Non-placeholders.
190 if pattern.kind() != code.kind() {
191 fail_match!("Pattern had a {:?}, code had {:?}", pattern.kind(), code.kind());
192 }
193 // Some kinds of nodes have special handling. For everything else, we fall back to default
194 // matching.
195 match code.kind() {
196 SyntaxKind::RECORD_FIELD_LIST => {
197 self.attempt_match_record_field_list(match_inputs, pattern, code)
198 }
199 SyntaxKind::TOKEN_TREE => self.attempt_match_token_tree(match_inputs, pattern, code),
200 _ => self.attempt_match_node_children(match_inputs, pattern, code),
201 }
202 }
203
204 fn attempt_match_node_children(
205 &mut self,
206 match_inputs: &MatchInputs,
207 pattern: &SyntaxNode,
208 code: &SyntaxNode,
209 ) -> Result<(), MatchFailed> {
210 self.attempt_match_sequences(
211 match_inputs,
212 PatternIterator::new(pattern),
213 code.children_with_tokens(),
214 )
215 }
216
217 fn attempt_match_sequences(
218 &mut self,
219 match_inputs: &MatchInputs,
220 pattern_it: PatternIterator,
221 mut code_it: SyntaxElementChildren,
222 ) -> Result<(), MatchFailed> {
223 let mut pattern_it = pattern_it.peekable();
224 loop {
225 match self.next_non_trivial(&mut code_it) {
226 None => {
227 if let Some(p) = pattern_it.next() {
228 fail_match!("Part of the pattern was unmached: {:?}", p);
229 }
230 return Ok(());
231 }
232 Some(SyntaxElement::Token(c)) => {
233 self.attempt_match_token(&mut pattern_it, &c)?;
234 }
235 Some(SyntaxElement::Node(c)) => match pattern_it.next() {
236 Some(SyntaxElement::Node(p)) => {
237 self.attempt_match_node(match_inputs, &p, &c)?;
238 }
239 Some(p) => fail_match!("Pattern wanted '{}', code has {}", p, c.text()),
240 None => fail_match!("Pattern reached end, code has {}", c.text()),
241 },
242 }
243 }
244 }
245
246 fn attempt_match_token(
247 &mut self,
248 pattern: &mut Peekable<PatternIterator>,
249 code: &ra_syntax::SyntaxToken,
250 ) -> Result<(), MatchFailed> {
251 self.record_ignored_comments(code);
252 // Ignore whitespace and comments.
253 if code.kind().is_trivia() {
254 return Ok(());
255 }
256 if let Some(SyntaxElement::Token(p)) = pattern.peek() {
257 // If the code has a comma and the pattern is about to close something, then accept the
258 // comma without advancing the pattern. i.e. ignore trailing commas.
259 if code.kind() == SyntaxKind::COMMA && is_closing_token(p.kind()) {
260 return Ok(());
261 }
262 // Conversely, if the pattern has a comma and the code doesn't, skip that part of the
263 // pattern and continue to match the code.
264 if p.kind() == SyntaxKind::COMMA && is_closing_token(code.kind()) {
265 pattern.next();
266 }
267 }
268 // Consume an element from the pattern and make sure it matches.
269 match pattern.next() {
270 Some(SyntaxElement::Token(p)) => {
271 if p.kind() != code.kind() || p.text() != code.text() {
272 fail_match!(
273 "Pattern wanted token '{}' ({:?}), but code had token '{}' ({:?})",
274 p.text(),
275 p.kind(),
276 code.text(),
277 code.kind()
278 )
279 }
280 }
281 Some(SyntaxElement::Node(p)) => {
282 // Not sure if this is actually reachable.
283 fail_match!(
284 "Pattern wanted {:?}, but code had token '{}' ({:?})",
285 p,
286 code.text(),
287 code.kind()
288 );
289 }
290 None => {
291 fail_match!("Pattern exhausted, while code remains: `{}`", code.text());
292 }
293 }
294 Ok(())
295 }
296
297 /// We want to allow the records to match in any order, so we have special matching logic for
298 /// them.
299 fn attempt_match_record_field_list(
300 &mut self,
301 match_inputs: &MatchInputs,
302 pattern: &SyntaxNode,
303 code: &SyntaxNode,
304 ) -> Result<(), MatchFailed> {
305 // Build a map keyed by field name.
306 let mut fields_by_name = FxHashMap::default();
307 for child in code.children() {
308 if let Some(record) = ast::RecordField::cast(child.clone()) {
309 if let Some(name) = record.field_name() {
310 fields_by_name.insert(name.text().clone(), child.clone());
311 }
312 }
313 }
314 for p in pattern.children_with_tokens() {
315 if let SyntaxElement::Node(p) = p {
316 if let Some(name_element) = p.first_child_or_token() {
317 if match_inputs.get_placeholder(&name_element).is_some() {
318 // If the pattern is using placeholders for field names then order
319 // independence doesn't make sense. Fall back to regular ordered
320 // matching.
321 return self.attempt_match_node_children(match_inputs, pattern, code);
322 }
323 if let Some(ident) = only_ident(name_element) {
324 let code_record = fields_by_name.remove(ident.text()).ok_or_else(|| {
325 match_error!(
326 "Placeholder has record field '{}', but code doesn't",
327 ident
328 )
329 })?;
330 self.attempt_match_node(match_inputs, &p, &code_record)?;
331 }
332 }
333 }
334 }
335 if let Some(unmatched_fields) = fields_by_name.keys().next() {
336 fail_match!(
337 "{} field(s) of a record literal failed to match, starting with {}",
338 fields_by_name.len(),
339 unmatched_fields
340 );
341 }
342 Ok(())
343 }
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
429 fn next_non_trivial(&mut self, code_it: &mut SyntaxElementChildren) -> Option<SyntaxElement> {
430 loop {
431 let c = code_it.next();
432 if let Some(SyntaxElement::Token(t)) = &c {
433 self.record_ignored_comments(t);
434 if t.kind().is_trivia() {
435 continue;
436 }
437 }
438 return c;
439 }
440 }
441
442 fn record_ignored_comments(&mut self, token: &SyntaxToken) {
443 if token.kind() == SyntaxKind::COMMENT {
444 if let Some(match_out) = &mut self.match_out {
445 if let Some(comment) = ast::Comment::cast(token.clone()) {
446 match_out.ignored_comments.push(comment);
447 }
448 }
449 }
450 }
451}
452
453impl MatchInputs<'_> {
454 fn get_placeholder(&self, element: &SyntaxElement) -> Option<&Placeholder> {
455 only_ident(element.clone())
456 .and_then(|ident| self.ssr_pattern.placeholders_by_stand_in.get(ident.text()))
457 }
458}
459
460fn is_closing_token(kind: SyntaxKind) -> bool {
461 kind == SyntaxKind::R_PAREN || kind == SyntaxKind::R_CURLY || kind == SyntaxKind::R_BRACK
462}
463
464pub(crate) fn record_match_fails_reasons_scope<F, T>(debug_active: bool, f: F) -> T
465where
466 F: Fn() -> T,
467{
468 RECORDING_MATCH_FAIL_REASONS.with(|c| c.set(debug_active));
469 let res = f();
470 RECORDING_MATCH_FAIL_REASONS.with(|c| c.set(false));
471 res
472}
473
474// For performance reasons, we don't want to record the reason why every match fails, only the bit
475// of code that the user indicated they thought would match. We use a thread local to indicate when
476// we are trying to match that bit of code. This saves us having to pass a boolean into all the bits
477// of code that can make the decision to not match.
478thread_local! {
479 pub static RECORDING_MATCH_FAIL_REASONS: Cell<bool> = Cell::new(false);
480}
481
482fn recording_match_fail_reasons() -> bool {
483 RECORDING_MATCH_FAIL_REASONS.with(|c| c.get())
484}
485
486impl PlaceholderMatch {
487 fn new(node: &SyntaxNode, range: FileRange) -> Self {
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() }
493 }
494}
495
496impl SsrPattern {
497 pub(crate) fn tree_for_kind(&self, kind: SyntaxKind) -> Result<&SyntaxNode, MatchFailed> {
498 let (tree, kind_name) = if ast::Expr::can_cast(kind) {
499 (&self.expr, "expression")
500 } else if ast::TypeRef::can_cast(kind) {
501 (&self.type_ref, "type reference")
502 } else if ast::ModuleItem::can_cast(kind) {
503 (&self.item, "item")
504 } else if ast::Path::can_cast(kind) {
505 (&self.path, "path")
506 } else if ast::Pat::can_cast(kind) {
507 (&self.pattern, "pattern")
508 } else {
509 fail_match!("Matching nodes of kind {:?} is not supported", kind);
510 };
511 match tree {
512 Some(tree) => Ok(tree),
513 None => fail_match!("Pattern cannot be parsed as a {}", kind_name),
514 }
515 }
516}
517
518// If `node` contains nothing but an ident then return it, otherwise return None.
519fn only_ident(element: SyntaxElement) -> Option<SyntaxToken> {
520 match element {
521 SyntaxElement::Token(t) => {
522 if t.kind() == SyntaxKind::IDENT {
523 return Some(t);
524 }
525 }
526 SyntaxElement::Node(n) => {
527 let mut children = n.children_with_tokens();
528 if let (Some(only_child), None) = (children.next(), children.next()) {
529 return only_ident(only_child);
530 }
531 }
532 }
533 None
534}
535
536struct PatternIterator {
537 iter: SyntaxElementChildren,
538}
539
540impl Iterator for PatternIterator {
541 type Item = SyntaxElement;
542
543 fn next(&mut self) -> Option<SyntaxElement> {
544 while let Some(element) = self.iter.next() {
545 if !element.kind().is_trivia() {
546 return Some(element);
547 }
548 }
549 None
550 }
551}
552
553impl PatternIterator {
554 fn new(parent: &SyntaxNode) -> Self {
555 Self { iter: parent.children_with_tokens() }
556 }
557}
558
559#[cfg(test)]
560mod tests {
561 use super::*;
562 use crate::MatchFinder;
563
564 #[test]
565 fn parse_match_replace() {
566 let rule: SsrRule = "foo($x) ==>> bar($x)".parse().unwrap();
567 let input = "fn main() { foo(1+2); }";
568
569 use ra_db::fixture::WithFixture;
570 let (db, file_id) = ra_ide_db::RootDatabase::with_single_file(input);
571 let mut match_finder = MatchFinder::new(&db);
572 match_finder.add_rule(rule);
573 let matches = match_finder.find_matches_in_file(file_id);
574 assert_eq!(matches.matches.len(), 1);
575 assert_eq!(matches.matches[0].matched_node.text(), "foo(1+2)");
576 assert_eq!(matches.matches[0].placeholder_values.len(), 1);
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 );
585
586 let edit = crate::replacing::matches_to_edit(&matches);
587 let mut after = input.to_string();
588 edit.apply(&mut after);
589 assert_eq!(after, "fn main() { bar(1+2); }");
590 }
591}
diff --git a/crates/ra_ssr/src/parsing.rs b/crates/ra_ssr/src/parsing.rs
new file mode 100644
index 000000000..90c13dbc2
--- /dev/null
+++ b/crates/ra_ssr/src/parsing.rs
@@ -0,0 +1,272 @@
1//! This file contains code for parsing SSR rules, which look something like `foo($a) ==>> bar($b)`.
2//! We first split everything before and after the separator `==>>`. Next, both the search pattern
3//! and the replacement template get tokenized by the Rust tokenizer. Tokens are then searched for
4//! placeholders, which start with `$`. For replacement templates, this is the final form. For
5//! search patterns, we go further and parse the pattern as each kind of thing that we can match.
6//! e.g. expressions, type references etc.
7
8use crate::{SsrError, SsrPattern, SsrRule};
9use ra_syntax::{ast, AstNode, SmolStr, SyntaxKind};
10use rustc_hash::{FxHashMap, FxHashSet};
11use std::str::FromStr;
12
13/// Returns from the current function with an error, supplied by arguments as for format!
14macro_rules! bail {
15 ($e:expr) => {return Err($crate::SsrError::new($e))};
16 ($fmt:expr, $($arg:tt)+) => {return Err($crate::SsrError::new(format!($fmt, $($arg)+)))}
17}
18
19#[derive(Clone, Debug)]
20pub(crate) struct SsrTemplate {
21 pub(crate) tokens: Vec<PatternElement>,
22}
23
24#[derive(Debug)]
25pub(crate) struct RawSearchPattern {
26 tokens: Vec<PatternElement>,
27}
28
29// Part of a search or replace pattern.
30#[derive(Clone, Debug, PartialEq, Eq)]
31pub(crate) enum PatternElement {
32 Token(Token),
33 Placeholder(Placeholder),
34}
35
36#[derive(Clone, Debug, PartialEq, Eq)]
37pub(crate) struct Placeholder {
38 /// The name of this placeholder. e.g. for "$a", this would be "a"
39 pub(crate) ident: SmolStr,
40 /// A unique name used in place of this placeholder when we parse the pattern as Rust code.
41 stand_in_name: String,
42}
43
44#[derive(Debug, Clone, PartialEq, Eq)]
45pub(crate) struct Token {
46 kind: SyntaxKind,
47 pub(crate) text: SmolStr,
48}
49
50impl FromStr for SsrRule {
51 type Err = SsrError;
52
53 fn from_str(query: &str) -> Result<SsrRule, SsrError> {
54 let mut it = query.split("==>>");
55 let pattern = it.next().expect("at least empty string").trim();
56 let template = it
57 .next()
58 .ok_or_else(|| SsrError("Cannot find delemiter `==>>`".into()))?
59 .trim()
60 .to_string();
61 if it.next().is_some() {
62 return Err(SsrError("More than one delimiter found".into()));
63 }
64 let rule = SsrRule { pattern: pattern.parse()?, template: template.parse()? };
65 validate_rule(&rule)?;
66 Ok(rule)
67 }
68}
69
70impl FromStr for RawSearchPattern {
71 type Err = SsrError;
72
73 fn from_str(pattern_str: &str) -> Result<RawSearchPattern, SsrError> {
74 Ok(RawSearchPattern { tokens: parse_pattern(pattern_str)? })
75 }
76}
77
78impl RawSearchPattern {
79 /// Returns this search pattern as Rust source code that we can feed to the Rust parser.
80 fn as_rust_code(&self) -> String {
81 let mut res = String::new();
82 for t in &self.tokens {
83 res.push_str(match t {
84 PatternElement::Token(token) => token.text.as_str(),
85 PatternElement::Placeholder(placeholder) => placeholder.stand_in_name.as_str(),
86 });
87 }
88 res
89 }
90
91 fn placeholders_by_stand_in(&self) -> FxHashMap<SmolStr, Placeholder> {
92 let mut res = FxHashMap::default();
93 for t in &self.tokens {
94 if let PatternElement::Placeholder(placeholder) = t {
95 res.insert(SmolStr::new(placeholder.stand_in_name.clone()), placeholder.clone());
96 }
97 }
98 res
99 }
100}
101
102impl FromStr for SsrPattern {
103 type Err = SsrError;
104
105 fn from_str(pattern_str: &str) -> Result<SsrPattern, SsrError> {
106 let raw: RawSearchPattern = pattern_str.parse()?;
107 let raw_str = raw.as_rust_code();
108 let res = SsrPattern {
109 expr: ast::Expr::parse(&raw_str).ok().map(|n| n.syntax().clone()),
110 type_ref: ast::TypeRef::parse(&raw_str).ok().map(|n| n.syntax().clone()),
111 item: ast::ModuleItem::parse(&raw_str).ok().map(|n| n.syntax().clone()),
112 path: ast::Path::parse(&raw_str).ok().map(|n| n.syntax().clone()),
113 pattern: ast::Pat::parse(&raw_str).ok().map(|n| n.syntax().clone()),
114 placeholders_by_stand_in: raw.placeholders_by_stand_in(),
115 raw,
116 };
117 if res.expr.is_none()
118 && res.type_ref.is_none()
119 && res.item.is_none()
120 && res.path.is_none()
121 && res.pattern.is_none()
122 {
123 bail!("Pattern is not a valid Rust expression, type, item, path or pattern");
124 }
125 Ok(res)
126 }
127}
128
129impl FromStr for SsrTemplate {
130 type Err = SsrError;
131
132 fn from_str(pattern_str: &str) -> Result<SsrTemplate, SsrError> {
133 let tokens = parse_pattern(pattern_str)?;
134 // Validate that the template is a valid fragment of Rust code. We reuse the validation
135 // logic for search patterns since the only thing that differs is the error message.
136 if SsrPattern::from_str(pattern_str).is_err() {
137 bail!("Replacement is not a valid Rust expression, type, item, path or pattern");
138 }
139 // Our actual template needs to preserve whitespace, so we can't reuse `tokens`.
140 Ok(SsrTemplate { tokens })
141 }
142}
143
144/// Returns `pattern_str`, parsed as a search or replace pattern. If `remove_whitespace` is true,
145/// then any whitespace tokens will be removed, which we do for the search pattern, but not for the
146/// replace pattern.
147fn parse_pattern(pattern_str: &str) -> Result<Vec<PatternElement>, SsrError> {
148 let mut res = Vec::new();
149 let mut placeholder_names = FxHashSet::default();
150 let mut tokens = tokenize(pattern_str)?.into_iter();
151 while let Some(token) = tokens.next() {
152 if token.kind == SyntaxKind::DOLLAR {
153 let placeholder = parse_placeholder(&mut tokens)?;
154 if !placeholder_names.insert(placeholder.ident.clone()) {
155 bail!("Name `{}` repeats more than once", placeholder.ident);
156 }
157 res.push(PatternElement::Placeholder(placeholder));
158 } else {
159 res.push(PatternElement::Token(token));
160 }
161 }
162 Ok(res)
163}
164
165/// Checks for errors in a rule. e.g. the replace pattern referencing placeholders that the search
166/// pattern didn't define.
167fn validate_rule(rule: &SsrRule) -> Result<(), SsrError> {
168 let mut defined_placeholders = std::collections::HashSet::new();
169 for p in &rule.pattern.raw.tokens {
170 if let PatternElement::Placeholder(placeholder) = p {
171 defined_placeholders.insert(&placeholder.ident);
172 }
173 }
174 let mut undefined = Vec::new();
175 for p in &rule.template.tokens {
176 if let PatternElement::Placeholder(placeholder) = p {
177 if !defined_placeholders.contains(&placeholder.ident) {
178 undefined.push(format!("${}", placeholder.ident));
179 }
180 }
181 }
182 if !undefined.is_empty() {
183 bail!("Replacement contains undefined placeholders: {}", undefined.join(", "));
184 }
185 Ok(())
186}
187
188fn tokenize(source: &str) -> Result<Vec<Token>, SsrError> {
189 let mut start = 0;
190 let (raw_tokens, errors) = ra_syntax::tokenize(source);
191 if let Some(first_error) = errors.first() {
192 bail!("Failed to parse pattern: {}", first_error);
193 }
194 let mut tokens: Vec<Token> = Vec::new();
195 for raw_token in raw_tokens {
196 let token_len = usize::from(raw_token.len);
197 tokens.push(Token {
198 kind: raw_token.kind,
199 text: SmolStr::new(&source[start..start + token_len]),
200 });
201 start += token_len;
202 }
203 Ok(tokens)
204}
205
206fn parse_placeholder(tokens: &mut std::vec::IntoIter<Token>) -> Result<Placeholder, SsrError> {
207 let mut name = None;
208 if let Some(token) = tokens.next() {
209 match token.kind {
210 SyntaxKind::IDENT => {
211 name = Some(token.text);
212 }
213 _ => {
214 bail!("Placeholders should be $name");
215 }
216 }
217 }
218 let name = name.ok_or_else(|| SsrError::new("Placeholder ($) with no name"))?;
219 Ok(Placeholder::new(name))
220}
221
222impl Placeholder {
223 fn new(name: SmolStr) -> Self {
224 Self { stand_in_name: format!("__placeholder_{}", name), ident: name }
225 }
226}
227
228impl SsrError {
229 fn new(message: impl Into<String>) -> SsrError {
230 SsrError(message.into())
231 }
232}
233
234#[cfg(test)]
235mod tests {
236 use super::*;
237
238 #[test]
239 fn parser_happy_case() {
240 fn token(kind: SyntaxKind, text: &str) -> PatternElement {
241 PatternElement::Token(Token { kind, text: SmolStr::new(text) })
242 }
243 fn placeholder(name: &str) -> PatternElement {
244 PatternElement::Placeholder(Placeholder::new(SmolStr::new(name)))
245 }
246 let result: SsrRule = "foo($a, $b) ==>> bar($b, $a)".parse().unwrap();
247 assert_eq!(
248 result.pattern.raw.tokens,
249 vec![
250 token(SyntaxKind::IDENT, "foo"),
251 token(SyntaxKind::L_PAREN, "("),
252 placeholder("a"),
253 token(SyntaxKind::COMMA, ","),
254 token(SyntaxKind::WHITESPACE, " "),
255 placeholder("b"),
256 token(SyntaxKind::R_PAREN, ")"),
257 ]
258 );
259 assert_eq!(
260 result.template.tokens,
261 vec![
262 token(SyntaxKind::IDENT, "bar"),
263 token(SyntaxKind::L_PAREN, "("),
264 placeholder("b"),
265 token(SyntaxKind::COMMA, ","),
266 token(SyntaxKind::WHITESPACE, " "),
267 placeholder("a"),
268 token(SyntaxKind::R_PAREN, ")"),
269 ]
270 );
271 }
272}
diff --git a/crates/ra_ssr/src/replacing.rs b/crates/ra_ssr/src/replacing.rs
new file mode 100644
index 000000000..5dcde82a2
--- /dev/null
+++ b/crates/ra_ssr/src/replacing.rs
@@ -0,0 +1,63 @@
1//! Code for applying replacement templates for matches that have previously been found.
2
3use crate::matching::Var;
4use crate::parsing::PatternElement;
5use crate::{Match, SsrMatches};
6use ra_syntax::ast::AstToken;
7use ra_syntax::TextSize;
8use ra_text_edit::TextEdit;
9
10/// Returns a text edit that will replace each match in `matches` with its corresponding replacement
11/// template. Placeholders in the template will have been substituted with whatever they matched to
12/// in the original code.
13pub(crate) fn matches_to_edit(matches: &SsrMatches) -> TextEdit {
14 matches_to_edit_at_offset(matches, 0.into())
15}
16
17fn matches_to_edit_at_offset(matches: &SsrMatches, relative_start: TextSize) -> TextEdit {
18 let mut edit_builder = ra_text_edit::TextEditBuilder::default();
19 for m in &matches.matches {
20 edit_builder.replace(m.range.checked_sub(relative_start).unwrap(), render_replace(m));
21 }
22 edit_builder.finish()
23}
24
25fn render_replace(match_info: &Match) -> String {
26 let mut out = String::new();
27 let match_start = match_info.matched_node.text_range().start();
28 for r in &match_info.template.tokens {
29 match r {
30 PatternElement::Token(t) => out.push_str(t.text.as_str()),
31 PatternElement::Placeholder(p) => {
32 if let Some(placeholder_value) =
33 match_info.placeholder_values.get(&Var(p.ident.to_string()))
34 {
35 let range = &placeholder_value.range.range;
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 };
44 let edit =
45 matches_to_edit_at_offset(&placeholder_value.inner_matches, range.start());
46 edit.apply(&mut matched_text);
47 out.push_str(&matched_text);
48 } else {
49 // We validated that all placeholder references were valid before we
50 // started, so this shouldn't happen.
51 panic!(
52 "Internal error: replacement referenced unknown placeholder {}",
53 p.ident
54 );
55 }
56 }
57 }
58 }
59 for comment in &match_info.ignored_comments {
60 out.push_str(&comment.syntax().to_string());
61 }
62 out
63}
diff --git a/crates/ra_ssr/src/tests.rs b/crates/ra_ssr/src/tests.rs
new file mode 100644
index 000000000..3ee1e74e9
--- /dev/null
+++ b/crates/ra_ssr/src/tests.rs
@@ -0,0 +1,549 @@
1use crate::matching::MatchFailureReason;
2use crate::{matching, Match, MatchFinder, SsrMatches, SsrPattern, SsrRule};
3use matching::record_match_fails_reasons_scope;
4use ra_db::{FileId, FileRange, SourceDatabaseExt};
5use ra_syntax::ast::AstNode;
6use ra_syntax::{ast, SyntaxKind, SyntaxNode, TextRange};
7
8struct MatchDebugInfo {
9 node: SyntaxNode,
10 /// Our search pattern parsed as the same kind of syntax node as `node`. e.g. expression, item,
11 /// etc. Will be absent if the pattern can't be parsed as that kind.
12 pattern: Result<SyntaxNode, MatchFailureReason>,
13 matched: Result<Match, MatchFailureReason>,
14}
15
16impl SsrPattern {
17 pub(crate) fn tree_for_kind_with_reason(
18 &self,
19 kind: SyntaxKind,
20 ) -> Result<&SyntaxNode, MatchFailureReason> {
21 record_match_fails_reasons_scope(true, || self.tree_for_kind(kind))
22 .map_err(|e| MatchFailureReason { reason: e.reason.unwrap() })
23 }
24}
25
26impl std::fmt::Debug for MatchDebugInfo {
27 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
28 write!(f, "========= PATTERN ==========\n")?;
29 match &self.pattern {
30 Ok(pattern) => {
31 write!(f, "{:#?}", pattern)?;
32 }
33 Err(err) => {
34 write!(f, "{}", err.reason)?;
35 }
36 }
37 write!(
38 f,
39 "\n============ AST ===========\n\
40 {:#?}\n============================",
41 self.node
42 )?;
43 match &self.matched {
44 Ok(_) => write!(f, "Node matched")?,
45 Err(reason) => write!(f, "Node failed to match because: {}", reason.reason)?,
46 }
47 Ok(())
48 }
49}
50
51impl SsrMatches {
52 /// Returns `self` with any nested matches removed and made into top-level matches.
53 pub(crate) fn flattened(self) -> SsrMatches {
54 let mut out = SsrMatches::default();
55 self.flatten_into(&mut out);
56 out
57 }
58
59 fn flatten_into(self, out: &mut SsrMatches) {
60 for mut m in self.matches {
61 for p in m.placeholder_values.values_mut() {
62 std::mem::replace(&mut p.inner_matches, SsrMatches::default()).flatten_into(out);
63 }
64 out.matches.push(m);
65 }
66 }
67}
68
69impl Match {
70 pub(crate) fn matched_text(&self) -> String {
71 self.matched_node.text().to_string()
72 }
73}
74
75impl<'db> MatchFinder<'db> {
76 /// Adds a search pattern. For use if you intend to only call `find_matches_in_file`. If you
77 /// intend to do replacement, use `add_rule` instead.
78 fn add_search_pattern(&mut self, pattern: SsrPattern) {
79 self.add_rule(SsrRule { pattern, template: "()".parse().unwrap() })
80 }
81
82 /// Finds all nodes in `file_id` whose text is exactly equal to `snippet` and attempts to match
83 /// them, while recording reasons why they don't match. This API is useful for command
84 /// line-based debugging where providing a range is difficult.
85 fn debug_where_text_equal(&self, file_id: FileId, snippet: &str) -> Vec<MatchDebugInfo> {
86 let file = self.sema.parse(file_id);
87 let mut res = Vec::new();
88 let file_text = self.sema.db.file_text(file_id);
89 let mut remaining_text = file_text.as_str();
90 let mut base = 0;
91 let len = snippet.len() as u32;
92 while let Some(offset) = remaining_text.find(snippet) {
93 let start = base + offset as u32;
94 let end = start + len;
95 self.output_debug_for_nodes_at_range(
96 file.syntax(),
97 TextRange::new(start.into(), end.into()),
98 &None,
99 &mut res,
100 );
101 remaining_text = &remaining_text[offset + snippet.len()..];
102 base = end;
103 }
104 res
105 }
106
107 fn output_debug_for_nodes_at_range(
108 &self,
109 node: &SyntaxNode,
110 range: TextRange,
111 restrict_range: &Option<FileRange>,
112 out: &mut Vec<MatchDebugInfo>,
113 ) {
114 for node in node.children() {
115 if !node.text_range().contains_range(range) {
116 continue;
117 }
118 if node.text_range() == range {
119 for rule in &self.rules {
120 let pattern =
121 rule.pattern.tree_for_kind_with_reason(node.kind()).map(|p| p.clone());
122 out.push(MatchDebugInfo {
123 matched: matching::get_match(true, rule, &node, restrict_range, &self.sema)
124 .map_err(|e| MatchFailureReason {
125 reason: e.reason.unwrap_or_else(|| {
126 "Match failed, but no reason was given".to_owned()
127 }),
128 }),
129 pattern,
130 node: node.clone(),
131 });
132 }
133 } else if let Some(macro_call) = ast::MacroCall::cast(node.clone()) {
134 if let Some(expanded) = self.sema.expand(&macro_call) {
135 if let Some(tt) = macro_call.token_tree() {
136 self.output_debug_for_nodes_at_range(
137 &expanded,
138 range,
139 &Some(self.sema.original_range(tt.syntax())),
140 out,
141 );
142 }
143 }
144 }
145 }
146 }
147}
148
149fn parse_error_text(query: &str) -> String {
150 format!("{}", query.parse::<SsrRule>().unwrap_err())
151}
152
153#[test]
154fn parser_empty_query() {
155 assert_eq!(parse_error_text(""), "Parse error: Cannot find delemiter `==>>`");
156}
157
158#[test]
159fn parser_no_delimiter() {
160 assert_eq!(parse_error_text("foo()"), "Parse error: Cannot find delemiter `==>>`");
161}
162
163#[test]
164fn parser_two_delimiters() {
165 assert_eq!(
166 parse_error_text("foo() ==>> a ==>> b "),
167 "Parse error: More than one delimiter found"
168 );
169}
170
171#[test]
172fn parser_repeated_name() {
173 assert_eq!(
174 parse_error_text("foo($a, $a) ==>>"),
175 "Parse error: Name `a` repeats more than once"
176 );
177}
178
179#[test]
180fn parser_invalid_pattern() {
181 assert_eq!(
182 parse_error_text(" ==>> ()"),
183 "Parse error: Pattern is not a valid Rust expression, type, item, path or pattern"
184 );
185}
186
187#[test]
188fn parser_invalid_template() {
189 assert_eq!(
190 parse_error_text("() ==>> )"),
191 "Parse error: Replacement is not a valid Rust expression, type, item, path or pattern"
192 );
193}
194
195#[test]
196fn parser_undefined_placeholder_in_replacement() {
197 assert_eq!(
198 parse_error_text("42 ==>> $a"),
199 "Parse error: Replacement contains undefined placeholders: $a"
200 );
201}
202
203fn single_file(code: &str) -> (ra_ide_db::RootDatabase, FileId) {
204 use ra_db::fixture::WithFixture;
205 ra_ide_db::RootDatabase::with_single_file(code)
206}
207
208fn assert_ssr_transform(rule: &str, input: &str, result: &str) {
209 assert_ssr_transforms(&[rule], input, result);
210}
211
212fn assert_ssr_transforms(rules: &[&str], input: &str, result: &str) {
213 let (db, file_id) = single_file(input);
214 let mut match_finder = MatchFinder::new(&db);
215 for rule in rules {
216 let rule: SsrRule = rule.parse().unwrap();
217 match_finder.add_rule(rule);
218 }
219 if let Some(edits) = match_finder.edits_for_file(file_id) {
220 let mut after = input.to_string();
221 edits.apply(&mut after);
222 assert_eq!(after, result);
223 } else {
224 panic!("No edits were made");
225 }
226}
227
228fn assert_matches(pattern: &str, code: &str, expected: &[&str]) {
229 let (db, file_id) = single_file(code);
230 let mut match_finder = MatchFinder::new(&db);
231 match_finder.add_search_pattern(pattern.parse().unwrap());
232 let matched_strings: Vec<String> = match_finder
233 .find_matches_in_file(file_id)
234 .flattened()
235 .matches
236 .iter()
237 .map(|m| m.matched_text())
238 .collect();
239 if matched_strings != expected && !expected.is_empty() {
240 let debug_info = match_finder.debug_where_text_equal(file_id, &expected[0]);
241 eprintln!("Test is about to fail. Some possibly useful info: {} nodes had text exactly equal to '{}'", debug_info.len(), &expected[0]);
242 for d in debug_info {
243 eprintln!("{:#?}", d);
244 }
245 }
246 assert_eq!(matched_strings, expected);
247}
248
249fn assert_no_match(pattern: &str, code: &str) {
250 assert_matches(pattern, code, &[]);
251}
252
253#[test]
254fn ssr_function_to_method() {
255 assert_ssr_transform(
256 "my_function($a, $b) ==>> ($a).my_method($b)",
257 "loop { my_function( other_func(x, y), z + w) }",
258 "loop { (other_func(x, y)).my_method(z + w) }",
259 )
260}
261
262#[test]
263fn ssr_nested_function() {
264 assert_ssr_transform(
265 "foo($a, $b, $c) ==>> bar($c, baz($a, $b))",
266 "fn main { foo (x + value.method(b), x+y-z, true && false) }",
267 "fn main { bar(true && false, baz(x + value.method(b), x+y-z)) }",
268 )
269}
270
271#[test]
272fn ssr_expected_spacing() {
273 assert_ssr_transform(
274 "foo($x) + bar() ==>> bar($x)",
275 "fn main() { foo(5) + bar() }",
276 "fn main() { bar(5) }",
277 );
278}
279
280#[test]
281fn ssr_with_extra_space() {
282 assert_ssr_transform(
283 "foo($x ) + bar() ==>> bar($x)",
284 "fn main() { foo( 5 ) +bar( ) }",
285 "fn main() { bar(5) }",
286 );
287}
288
289#[test]
290fn ssr_keeps_nested_comment() {
291 assert_ssr_transform(
292 "foo($x) ==>> bar($x)",
293 "fn main() { foo(other(5 /* using 5 */)) }",
294 "fn main() { bar(other(5 /* using 5 */)) }",
295 )
296}
297
298#[test]
299fn ssr_keeps_comment() {
300 assert_ssr_transform(
301 "foo($x) ==>> bar($x)",
302 "fn main() { foo(5 /* using 5 */) }",
303 "fn main() { bar(5)/* using 5 */ }",
304 )
305}
306
307#[test]
308fn ssr_struct_lit() {
309 assert_ssr_transform(
310 "foo{a: $a, b: $b} ==>> foo::new($a, $b)",
311 "fn main() { foo{b:2, a:1} }",
312 "fn main() { foo::new(1, 2) }",
313 )
314}
315
316#[test]
317fn ignores_whitespace() {
318 assert_matches("1+2", "fn f() -> i32 {1 + 2}", &["1 + 2"]);
319 assert_matches("1 + 2", "fn f() -> i32 {1+2}", &["1+2"]);
320}
321
322#[test]
323fn no_match() {
324 assert_no_match("1 + 3", "fn f() -> i32 {1 + 2}");
325}
326
327#[test]
328fn match_fn_definition() {
329 assert_matches("fn $a($b: $t) {$c}", "fn f(a: i32) {bar()}", &["fn f(a: i32) {bar()}"]);
330}
331
332#[test]
333fn match_struct_definition() {
334 assert_matches(
335 "struct $n {$f: Option<String>}",
336 "struct Bar {} struct Foo {name: Option<String>}",
337 &["struct Foo {name: Option<String>}"],
338 );
339}
340
341#[test]
342fn match_expr() {
343 let code = "fn f() -> i32 {foo(40 + 2, 42)}";
344 assert_matches("foo($a, $b)", code, &["foo(40 + 2, 42)"]);
345 assert_no_match("foo($a, $b, $c)", code);
346 assert_no_match("foo($a)", code);
347}
348
349#[test]
350fn match_nested_method_calls() {
351 assert_matches(
352 "$a.z().z().z()",
353 "fn f() {h().i().j().z().z().z().d().e()}",
354 &["h().i().j().z().z().z()"],
355 );
356}
357
358#[test]
359fn match_complex_expr() {
360 let code = "fn f() -> i32 {foo(bar(40, 2), 42)}";
361 assert_matches("foo($a, $b)", code, &["foo(bar(40, 2), 42)"]);
362 assert_no_match("foo($a, $b, $c)", code);
363 assert_no_match("foo($a)", code);
364 assert_matches("bar($a, $b)", code, &["bar(40, 2)"]);
365}
366
367// Trailing commas in the code should be ignored.
368#[test]
369fn match_with_trailing_commas() {
370 // Code has comma, pattern doesn't.
371 assert_matches("foo($a, $b)", "fn f() {foo(1, 2,);}", &["foo(1, 2,)"]);
372 assert_matches("Foo{$a, $b}", "fn f() {Foo{1, 2,};}", &["Foo{1, 2,}"]);
373
374 // Pattern has comma, code doesn't.
375 assert_matches("foo($a, $b,)", "fn f() {foo(1, 2);}", &["foo(1, 2)"]);
376 assert_matches("Foo{$a, $b,}", "fn f() {Foo{1, 2};}", &["Foo{1, 2}"]);
377}
378
379#[test]
380fn match_type() {
381 assert_matches("i32", "fn f() -> i32 {1 + 2}", &["i32"]);
382 assert_matches("Option<$a>", "fn f() -> Option<i32> {42}", &["Option<i32>"]);
383 assert_no_match("Option<$a>", "fn f() -> Result<i32, ()> {42}");
384}
385
386#[test]
387fn match_struct_instantiation() {
388 assert_matches(
389 "Foo {bar: 1, baz: 2}",
390 "fn f() {Foo {bar: 1, baz: 2}}",
391 &["Foo {bar: 1, baz: 2}"],
392 );
393 // Now with placeholders for all parts of the struct.
394 assert_matches(
395 "Foo {$a: $b, $c: $d}",
396 "fn f() {Foo {bar: 1, baz: 2}}",
397 &["Foo {bar: 1, baz: 2}"],
398 );
399 assert_matches("Foo {}", "fn f() {Foo {}}", &["Foo {}"]);
400}
401
402#[test]
403fn match_path() {
404 assert_matches("foo::bar", "fn f() {foo::bar(42)}", &["foo::bar"]);
405 assert_matches("$a::bar", "fn f() {foo::bar(42)}", &["foo::bar"]);
406 assert_matches("foo::$b", "fn f() {foo::bar(42)}", &["foo::bar"]);
407}
408
409#[test]
410fn match_pattern() {
411 assert_matches("Some($a)", "fn f() {if let Some(x) = foo() {}}", &["Some(x)"]);
412}
413
414#[test]
415fn match_reordered_struct_instantiation() {
416 assert_matches(
417 "Foo {aa: 1, b: 2, ccc: 3}",
418 "fn f() {Foo {b: 2, ccc: 3, aa: 1}}",
419 &["Foo {b: 2, ccc: 3, aa: 1}"],
420 );
421 assert_no_match("Foo {a: 1}", "fn f() {Foo {b: 1}}");
422 assert_no_match("Foo {a: 1}", "fn f() {Foo {a: 2}}");
423 assert_no_match("Foo {a: 1, b: 2}", "fn f() {Foo {a: 1}}");
424 assert_no_match("Foo {a: 1, b: 2}", "fn f() {Foo {b: 2}}");
425 assert_no_match("Foo {a: 1, }", "fn f() {Foo {a: 1, b: 2}}");
426 assert_no_match("Foo {a: 1, z: 9}", "fn f() {Foo {a: 1}}");
427}
428
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]
469fn replace_function_call() {
470 assert_ssr_transform("foo() ==>> bar()", "fn f1() {foo(); foo();}", "fn f1() {bar(); bar();}");
471}
472
473#[test]
474fn replace_function_call_with_placeholders() {
475 assert_ssr_transform(
476 "foo($a, $b) ==>> bar($b, $a)",
477 "fn f1() {foo(5, 42)}",
478 "fn f1() {bar(42, 5)}",
479 );
480}
481
482#[test]
483fn replace_nested_function_calls() {
484 assert_ssr_transform(
485 "foo($a) ==>> bar($a)",
486 "fn f1() {foo(foo(42))}",
487 "fn f1() {bar(bar(42))}",
488 );
489}
490
491#[test]
492fn replace_type() {
493 assert_ssr_transform(
494 "Result<(), $a> ==>> Option<$a>",
495 "fn f1() -> Result<(), Vec<Error>> {foo()}",
496 "fn f1() -> Option<Vec<Error>> {foo()}",
497 );
498}
499
500#[test]
501fn replace_struct_init() {
502 assert_ssr_transform(
503 "Foo {a: $a, b: $b} ==>> Foo::new($a, $b)",
504 "fn f1() {Foo{b: 1, a: 2}}",
505 "fn f1() {Foo::new(2, 1)}",
506 );
507}
508
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]
524fn replace_binary_op() {
525 assert_ssr_transform(
526 "$a + $b ==>> $b + $a",
527 "fn f() {2 * 3 + 4 * 5}",
528 "fn f() {4 * 5 + 2 * 3}",
529 );
530 assert_ssr_transform(
531 "$a + $b ==>> $b + $a",
532 "fn f() {1 + 2 + 3 + 4}",
533 "fn f() {4 + 3 + 2 + 1}",
534 );
535}
536
537#[test]
538fn match_binary_op() {
539 assert_matches("$a + $b", "fn f() {1 + 2 + 3 + 4}", &["1 + 2", "1 + 2 + 3", "1 + 2 + 3 + 4"]);
540}
541
542#[test]
543fn multiple_rules() {
544 assert_ssr_transforms(
545 &["$a + 1 ==>> add_one($a)", "$a + $b ==>> add($a, $b)"],
546 "fn f() -> i32 {3 + 2 + 1}",
547 "fn f() -> i32 {add_one(add(3, 2))}",
548 )
549}