aboutsummaryrefslogtreecommitdiff
path: root/crates/ssr/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ssr/src')
-rw-r--r--crates/ssr/src/errors.rs29
-rw-r--r--crates/ssr/src/lib.rs341
-rw-r--r--crates/ssr/src/matching.rs814
-rw-r--r--crates/ssr/src/nester.rs94
-rw-r--r--crates/ssr/src/parsing.rs403
-rw-r--r--crates/ssr/src/replacing.rs240
-rw-r--r--crates/ssr/src/resolving.rs301
-rw-r--r--crates/ssr/src/search.rs282
-rw-r--r--crates/ssr/src/tests.rs1281
9 files changed, 3785 insertions, 0 deletions
diff --git a/crates/ssr/src/errors.rs b/crates/ssr/src/errors.rs
new file mode 100644
index 000000000..c02bacae6
--- /dev/null
+++ b/crates/ssr/src/errors.rs
@@ -0,0 +1,29 @@
1//! Code relating to errors produced by SSR.
2
3/// Constructs an SsrError taking arguments like the format macro.
4macro_rules! _error {
5 ($fmt:expr) => {$crate::SsrError::new(format!($fmt))};
6 ($fmt:expr, $($arg:tt)+) => {$crate::SsrError::new(format!($fmt, $($arg)+))}
7}
8pub(crate) use _error as error;
9
10/// Returns from the current function with an error, supplied by arguments as for format!
11macro_rules! _bail {
12 ($($tokens:tt)*) => {return Err(crate::errors::error!($($tokens)*))}
13}
14pub(crate) use _bail as bail;
15
16#[derive(Debug, PartialEq)]
17pub struct SsrError(pub(crate) String);
18
19impl std::fmt::Display for SsrError {
20 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
21 write!(f, "Parse error: {}", self.0)
22 }
23}
24
25impl SsrError {
26 pub(crate) fn new(message: impl Into<String>) -> SsrError {
27 SsrError(message.into())
28 }
29}
diff --git a/crates/ssr/src/lib.rs b/crates/ssr/src/lib.rs
new file mode 100644
index 000000000..ba669fd56
--- /dev/null
+++ b/crates/ssr/src/lib.rs
@@ -0,0 +1,341 @@
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
6// Feature: Structural Search and Replace
7//
8// Search and replace with named wildcards that will match any expression, type, path, pattern or item.
9// The syntax for a structural search replace command is `<search_pattern> ==>> <replace_pattern>`.
10// A `$<name>` placeholder in the search pattern will match any AST node and `$<name>` will reference it in the replacement.
11// Within a macro call, a placeholder will match up until whatever token follows the placeholder.
12//
13// All paths in both the search pattern and the replacement template must resolve in the context
14// in which this command is invoked. Paths in the search pattern will then match the code if they
15// resolve to the same item, even if they're written differently. For example if we invoke the
16// command in the module `foo` with a pattern of `Bar`, then code in the parent module that refers
17// to `foo::Bar` will match.
18//
19// Paths in the replacement template will be rendered appropriately for the context in which the
20// replacement occurs. For example if our replacement template is `foo::Bar` and we match some
21// code in the `foo` module, we'll insert just `Bar`.
22//
23// Inherent method calls should generally be written in UFCS form. e.g. `foo::Bar::baz($s, $a)` will
24// match `$s.baz($a)`, provided the method call `baz` resolves to the method `foo::Bar::baz`. When a
25// placeholder is the receiver of a method call in the search pattern (e.g. `$s.foo()`), but not in
26// the replacement template (e.g. `bar($s)`), then *, & and &mut will be added as needed to mirror
27// whatever autoderef and autoref was happening implicitly in the matched code.
28//
29// The scope of the search / replace will be restricted to the current selection if any, otherwise
30// it will apply to the whole workspace.
31//
32// Placeholders may be given constraints by writing them as `${<name>:<constraint1>:<constraint2>...}`.
33//
34// Supported constraints:
35//
36// |===
37// | Constraint | Restricts placeholder
38//
39// | kind(literal) | Is a literal (e.g. `42` or `"forty two"`)
40// | not(a) | Negates the constraint `a`
41// |===
42//
43// Available via the command `rust-analyzer.ssr`.
44//
45// ```rust
46// // Using structural search replace command [foo($a, $b) ==>> ($a).foo($b)]
47//
48// // BEFORE
49// String::from(foo(y + 5, z))
50//
51// // AFTER
52// String::from((y + 5).foo(z))
53// ```
54//
55// |===
56// | Editor | Action Name
57//
58// | VS Code | **Rust Analyzer: Structural Search Replace**
59// |===
60
61mod matching;
62mod nester;
63mod parsing;
64mod replacing;
65mod resolving;
66mod search;
67#[macro_use]
68mod errors;
69#[cfg(test)]
70mod tests;
71
72use crate::errors::bail;
73pub use crate::errors::SsrError;
74pub use crate::matching::Match;
75use crate::matching::MatchFailureReason;
76use base_db::{FileId, FilePosition, FileRange};
77use hir::Semantics;
78use ide_db::source_change::SourceFileEdit;
79use resolving::ResolvedRule;
80use rustc_hash::FxHashMap;
81use syntax::{ast, AstNode, SyntaxNode, TextRange};
82
83// A structured search replace rule. Create by calling `parse` on a str.
84#[derive(Debug)]
85pub struct SsrRule {
86 /// A structured pattern that we're searching for.
87 pattern: parsing::RawPattern,
88 /// What we'll replace it with.
89 template: parsing::RawPattern,
90 parsed_rules: Vec<parsing::ParsedRule>,
91}
92
93#[derive(Debug)]
94pub struct SsrPattern {
95 raw: parsing::RawPattern,
96 parsed_rules: Vec<parsing::ParsedRule>,
97}
98
99#[derive(Debug, Default)]
100pub struct SsrMatches {
101 pub matches: Vec<Match>,
102}
103
104/// Searches a crate for pattern matches and possibly replaces them with something else.
105pub struct MatchFinder<'db> {
106 /// Our source of information about the user's code.
107 sema: Semantics<'db, ide_db::RootDatabase>,
108 rules: Vec<ResolvedRule>,
109 resolution_scope: resolving::ResolutionScope<'db>,
110 restrict_ranges: Vec<FileRange>,
111}
112
113impl<'db> MatchFinder<'db> {
114 /// Constructs a new instance where names will be looked up as if they appeared at
115 /// `lookup_context`.
116 pub fn in_context(
117 db: &'db ide_db::RootDatabase,
118 lookup_context: FilePosition,
119 mut restrict_ranges: Vec<FileRange>,
120 ) -> MatchFinder<'db> {
121 restrict_ranges.retain(|range| !range.range.is_empty());
122 let sema = Semantics::new(db);
123 let resolution_scope = resolving::ResolutionScope::new(&sema, lookup_context);
124 MatchFinder { sema, rules: Vec::new(), resolution_scope, restrict_ranges }
125 }
126
127 /// Constructs an instance using the start of the first file in `db` as the lookup context.
128 pub fn at_first_file(db: &'db ide_db::RootDatabase) -> Result<MatchFinder<'db>, SsrError> {
129 use base_db::SourceDatabaseExt;
130 use ide_db::symbol_index::SymbolsDatabase;
131 if let Some(first_file_id) = db
132 .local_roots()
133 .iter()
134 .next()
135 .and_then(|root| db.source_root(root.clone()).iter().next())
136 {
137 Ok(MatchFinder::in_context(
138 db,
139 FilePosition { file_id: first_file_id, offset: 0.into() },
140 vec![],
141 ))
142 } else {
143 bail!("No files to search");
144 }
145 }
146
147 /// Adds a rule to be applied. The order in which rules are added matters. Earlier rules take
148 /// precedence. If a node is matched by an earlier rule, then later rules won't be permitted to
149 /// match to it.
150 pub fn add_rule(&mut self, rule: SsrRule) -> Result<(), SsrError> {
151 for parsed_rule in rule.parsed_rules {
152 self.rules.push(ResolvedRule::new(
153 parsed_rule,
154 &self.resolution_scope,
155 self.rules.len(),
156 )?);
157 }
158 Ok(())
159 }
160
161 /// Finds matches for all added rules and returns edits for all found matches.
162 pub fn edits(&self) -> Vec<SourceFileEdit> {
163 use base_db::SourceDatabaseExt;
164 let mut matches_by_file = FxHashMap::default();
165 for m in self.matches().matches {
166 matches_by_file
167 .entry(m.range.file_id)
168 .or_insert_with(|| SsrMatches::default())
169 .matches
170 .push(m);
171 }
172 let mut edits = vec![];
173 for (file_id, matches) in matches_by_file {
174 let edit =
175 replacing::matches_to_edit(&matches, &self.sema.db.file_text(file_id), &self.rules);
176 edits.push(SourceFileEdit { file_id, edit });
177 }
178 edits
179 }
180
181 /// Adds a search pattern. For use if you intend to only call `find_matches_in_file`. If you
182 /// intend to do replacement, use `add_rule` instead.
183 pub fn add_search_pattern(&mut self, pattern: SsrPattern) -> Result<(), SsrError> {
184 for parsed_rule in pattern.parsed_rules {
185 self.rules.push(ResolvedRule::new(
186 parsed_rule,
187 &self.resolution_scope,
188 self.rules.len(),
189 )?);
190 }
191 Ok(())
192 }
193
194 /// Returns matches for all added rules.
195 pub fn matches(&self) -> SsrMatches {
196 let mut matches = Vec::new();
197 let mut usage_cache = search::UsageCache::default();
198 for rule in &self.rules {
199 self.find_matches_for_rule(rule, &mut usage_cache, &mut matches);
200 }
201 nester::nest_and_remove_collisions(matches, &self.sema)
202 }
203
204 /// Finds all nodes in `file_id` whose text is exactly equal to `snippet` and attempts to match
205 /// them, while recording reasons why they don't match. This API is useful for command
206 /// line-based debugging where providing a range is difficult.
207 pub fn debug_where_text_equal(&self, file_id: FileId, snippet: &str) -> Vec<MatchDebugInfo> {
208 use base_db::SourceDatabaseExt;
209 let file = self.sema.parse(file_id);
210 let mut res = Vec::new();
211 let file_text = self.sema.db.file_text(file_id);
212 let mut remaining_text = file_text.as_str();
213 let mut base = 0;
214 let len = snippet.len() as u32;
215 while let Some(offset) = remaining_text.find(snippet) {
216 let start = base + offset as u32;
217 let end = start + len;
218 self.output_debug_for_nodes_at_range(
219 file.syntax(),
220 FileRange { file_id, range: TextRange::new(start.into(), end.into()) },
221 &None,
222 &mut res,
223 );
224 remaining_text = &remaining_text[offset + snippet.len()..];
225 base = end;
226 }
227 res
228 }
229
230 fn output_debug_for_nodes_at_range(
231 &self,
232 node: &SyntaxNode,
233 range: FileRange,
234 restrict_range: &Option<FileRange>,
235 out: &mut Vec<MatchDebugInfo>,
236 ) {
237 for node in node.children() {
238 let node_range = self.sema.original_range(&node);
239 if node_range.file_id != range.file_id || !node_range.range.contains_range(range.range)
240 {
241 continue;
242 }
243 if node_range.range == range.range {
244 for rule in &self.rules {
245 // For now we ignore rules that have a different kind than our node, otherwise
246 // we get lots of noise. If at some point we add support for restricting rules
247 // to a particular kind of thing (e.g. only match type references), then we can
248 // relax this. We special-case expressions, since function calls can match
249 // method calls.
250 if rule.pattern.node.kind() != node.kind()
251 && !(ast::Expr::can_cast(rule.pattern.node.kind())
252 && ast::Expr::can_cast(node.kind()))
253 {
254 continue;
255 }
256 out.push(MatchDebugInfo {
257 matched: matching::get_match(true, rule, &node, restrict_range, &self.sema)
258 .map_err(|e| MatchFailureReason {
259 reason: e.reason.unwrap_or_else(|| {
260 "Match failed, but no reason was given".to_owned()
261 }),
262 }),
263 pattern: rule.pattern.node.clone(),
264 node: node.clone(),
265 });
266 }
267 } else if let Some(macro_call) = ast::MacroCall::cast(node.clone()) {
268 if let Some(expanded) = self.sema.expand(&macro_call) {
269 if let Some(tt) = macro_call.token_tree() {
270 self.output_debug_for_nodes_at_range(
271 &expanded,
272 range,
273 &Some(self.sema.original_range(tt.syntax())),
274 out,
275 );
276 }
277 }
278 }
279 self.output_debug_for_nodes_at_range(&node, range, restrict_range, out);
280 }
281 }
282}
283
284pub struct MatchDebugInfo {
285 node: SyntaxNode,
286 /// Our search pattern parsed as an expression or item, etc
287 pattern: SyntaxNode,
288 matched: Result<Match, MatchFailureReason>,
289}
290
291impl std::fmt::Debug for MatchDebugInfo {
292 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
293 match &self.matched {
294 Ok(_) => writeln!(f, "Node matched")?,
295 Err(reason) => writeln!(f, "Node failed to match because: {}", reason.reason)?,
296 }
297 writeln!(
298 f,
299 "============ AST ===========\n\
300 {:#?}",
301 self.node
302 )?;
303 writeln!(f, "========= PATTERN ==========")?;
304 writeln!(f, "{:#?}", self.pattern)?;
305 writeln!(f, "============================")?;
306 Ok(())
307 }
308}
309
310impl SsrMatches {
311 /// Returns `self` with any nested matches removed and made into top-level matches.
312 pub fn flattened(self) -> SsrMatches {
313 let mut out = SsrMatches::default();
314 self.flatten_into(&mut out);
315 out
316 }
317
318 fn flatten_into(self, out: &mut SsrMatches) {
319 for mut m in self.matches {
320 for p in m.placeholder_values.values_mut() {
321 std::mem::replace(&mut p.inner_matches, SsrMatches::default()).flatten_into(out);
322 }
323 out.matches.push(m);
324 }
325 }
326}
327
328impl Match {
329 pub fn matched_text(&self) -> String {
330 self.matched_node.text().to_string()
331 }
332}
333
334impl std::error::Error for SsrError {}
335
336#[cfg(test)]
337impl MatchDebugInfo {
338 pub(crate) fn match_failure_reason(&self) -> Option<&str> {
339 self.matched.as_ref().err().map(|r| r.reason.as_str())
340 }
341}
diff --git a/crates/ssr/src/matching.rs b/crates/ssr/src/matching.rs
new file mode 100644
index 000000000..8bb5ced90
--- /dev/null
+++ b/crates/ssr/src/matching.rs
@@ -0,0 +1,814 @@
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::{Constraint, NodeKind, Placeholder, Var},
6 resolving::{ResolvedPattern, ResolvedRule, UfcsCallInfo},
7 SsrMatches,
8};
9use base_db::FileRange;
10use hir::Semantics;
11use rustc_hash::FxHashMap;
12use std::{cell::Cell, iter::Peekable};
13use syntax::ast::{AstNode, AstToken};
14use syntax::{ast, SyntaxElement, SyntaxElementChildren, SyntaxKind, SyntaxNode, SyntaxToken};
15use test_utils::mark;
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 struct Match {
48 pub(crate) range: FileRange,
49 pub(crate) matched_node: SyntaxNode,
50 pub(crate) placeholder_values: FxHashMap<Var, PlaceholderMatch>,
51 pub(crate) ignored_comments: Vec<ast::Comment>,
52 pub(crate) rule_index: usize,
53 /// The depth of matched_node.
54 pub(crate) depth: usize,
55 // Each path in the template rendered for the module in which the match was found.
56 pub(crate) rendered_template_paths: FxHashMap<SyntaxNode, hir::ModPath>,
57}
58
59/// Information about a placeholder bound in a match.
60#[derive(Debug)]
61pub(crate) struct PlaceholderMatch {
62 /// The node that the placeholder matched to. If set, then we'll search for further matches
63 /// within this node. It isn't set when we match tokens within a macro call's token tree.
64 pub(crate) node: Option<SyntaxNode>,
65 pub(crate) range: FileRange,
66 /// More matches, found within `node`.
67 pub(crate) inner_matches: SsrMatches,
68 /// How many times the code that the placeholder matched needed to be dereferenced. Will only be
69 /// non-zero if the placeholder matched to the receiver of a method call.
70 pub(crate) autoderef_count: usize,
71 pub(crate) autoref_kind: ast::SelfParamKind,
72}
73
74#[derive(Debug)]
75pub(crate) struct MatchFailureReason {
76 pub(crate) reason: String,
77}
78
79/// An "error" indicating that matching failed. Use the fail_match! macro to create and return this.
80#[derive(Clone)]
81pub(crate) struct MatchFailed {
82 /// The reason why we failed to match. Only present when debug_active true in call to
83 /// `get_match`.
84 pub(crate) reason: Option<String>,
85}
86
87/// Checks if `code` matches the search pattern found in `search_scope`, returning information about
88/// the match, if it does. Since we only do matching in this module and searching is done by the
89/// parent module, we don't populate nested matches.
90pub(crate) fn get_match(
91 debug_active: bool,
92 rule: &ResolvedRule,
93 code: &SyntaxNode,
94 restrict_range: &Option<FileRange>,
95 sema: &Semantics<ide_db::RootDatabase>,
96) -> Result<Match, MatchFailed> {
97 record_match_fails_reasons_scope(debug_active, || {
98 Matcher::try_match(rule, code, restrict_range, sema)
99 })
100}
101
102/// Checks if our search pattern matches a particular node of the AST.
103struct Matcher<'db, 'sema> {
104 sema: &'sema Semantics<'db, ide_db::RootDatabase>,
105 /// If any placeholders come from anywhere outside of this range, then the match will be
106 /// rejected.
107 restrict_range: Option<FileRange>,
108 rule: &'sema ResolvedRule,
109}
110
111/// Which phase of matching we're currently performing. We do two phases because most attempted
112/// matches will fail and it means we can defer more expensive checks to the second phase.
113enum Phase<'a> {
114 /// On the first phase, we perform cheap checks. No state is mutated and nothing is recorded.
115 First,
116 /// On the second phase, we construct the `Match`. Things like what placeholders bind to is
117 /// recorded.
118 Second(&'a mut Match),
119}
120
121impl<'db, 'sema> Matcher<'db, 'sema> {
122 fn try_match(
123 rule: &ResolvedRule,
124 code: &SyntaxNode,
125 restrict_range: &Option<FileRange>,
126 sema: &'sema Semantics<'db, ide_db::RootDatabase>,
127 ) -> Result<Match, MatchFailed> {
128 let match_state = Matcher { sema, restrict_range: restrict_range.clone(), rule };
129 // First pass at matching, where we check that node types and idents match.
130 match_state.attempt_match_node(&mut Phase::First, &rule.pattern.node, code)?;
131 match_state.validate_range(&sema.original_range(code))?;
132 let mut the_match = Match {
133 range: sema.original_range(code),
134 matched_node: code.clone(),
135 placeholder_values: FxHashMap::default(),
136 ignored_comments: Vec::new(),
137 rule_index: rule.index,
138 depth: 0,
139 rendered_template_paths: FxHashMap::default(),
140 };
141 // Second matching pass, where we record placeholder matches, ignored comments and maybe do
142 // any other more expensive checks that we didn't want to do on the first pass.
143 match_state.attempt_match_node(
144 &mut Phase::Second(&mut the_match),
145 &rule.pattern.node,
146 code,
147 )?;
148 the_match.depth = sema.ancestors_with_macros(the_match.matched_node.clone()).count();
149 if let Some(template) = &rule.template {
150 the_match.render_template_paths(template, sema)?;
151 }
152 Ok(the_match)
153 }
154
155 /// Checks that `range` is within the permitted range if any. This is applicable when we're
156 /// processing a macro expansion and we want to fail the match if we're working with a node that
157 /// didn't originate from the token tree of the macro call.
158 fn validate_range(&self, range: &FileRange) -> Result<(), MatchFailed> {
159 if let Some(restrict_range) = &self.restrict_range {
160 if restrict_range.file_id != range.file_id
161 || !restrict_range.range.contains_range(range.range)
162 {
163 fail_match!("Node originated from a macro");
164 }
165 }
166 Ok(())
167 }
168
169 fn attempt_match_node(
170 &self,
171 phase: &mut Phase,
172 pattern: &SyntaxNode,
173 code: &SyntaxNode,
174 ) -> Result<(), MatchFailed> {
175 // Handle placeholders.
176 if let Some(placeholder) = self.get_placeholder_for_node(pattern) {
177 for constraint in &placeholder.constraints {
178 self.check_constraint(constraint, code)?;
179 }
180 if let Phase::Second(matches_out) = phase {
181 let original_range = self.sema.original_range(code);
182 // We validated the range for the node when we started the match, so the placeholder
183 // probably can't fail range validation, but just to be safe...
184 self.validate_range(&original_range)?;
185 matches_out.placeholder_values.insert(
186 placeholder.ident.clone(),
187 PlaceholderMatch::new(Some(code), original_range),
188 );
189 }
190 return Ok(());
191 }
192 // We allow a UFCS call to match a method call, provided they resolve to the same function.
193 if let Some(pattern_ufcs) = self.rule.pattern.ufcs_function_calls.get(pattern) {
194 if let Some(code) = ast::MethodCallExpr::cast(code.clone()) {
195 return self.attempt_match_ufcs_to_method_call(phase, pattern_ufcs, &code);
196 }
197 if let Some(code) = ast::CallExpr::cast(code.clone()) {
198 return self.attempt_match_ufcs_to_ufcs(phase, pattern_ufcs, &code);
199 }
200 }
201 if pattern.kind() != code.kind() {
202 fail_match!(
203 "Pattern had `{}` ({:?}), code had `{}` ({:?})",
204 pattern.text(),
205 pattern.kind(),
206 code.text(),
207 code.kind()
208 );
209 }
210 // Some kinds of nodes have special handling. For everything else, we fall back to default
211 // matching.
212 match code.kind() {
213 SyntaxKind::RECORD_EXPR_FIELD_LIST => {
214 self.attempt_match_record_field_list(phase, pattern, code)
215 }
216 SyntaxKind::TOKEN_TREE => self.attempt_match_token_tree(phase, pattern, code),
217 SyntaxKind::PATH => self.attempt_match_path(phase, pattern, code),
218 _ => self.attempt_match_node_children(phase, pattern, code),
219 }
220 }
221
222 fn attempt_match_node_children(
223 &self,
224 phase: &mut Phase,
225 pattern: &SyntaxNode,
226 code: &SyntaxNode,
227 ) -> Result<(), MatchFailed> {
228 self.attempt_match_sequences(
229 phase,
230 PatternIterator::new(pattern),
231 code.children_with_tokens(),
232 )
233 }
234
235 fn attempt_match_sequences(
236 &self,
237 phase: &mut Phase,
238 pattern_it: PatternIterator,
239 mut code_it: SyntaxElementChildren,
240 ) -> Result<(), MatchFailed> {
241 let mut pattern_it = pattern_it.peekable();
242 loop {
243 match phase.next_non_trivial(&mut code_it) {
244 None => {
245 if let Some(p) = pattern_it.next() {
246 fail_match!("Part of the pattern was unmatched: {:?}", p);
247 }
248 return Ok(());
249 }
250 Some(SyntaxElement::Token(c)) => {
251 self.attempt_match_token(phase, &mut pattern_it, &c)?;
252 }
253 Some(SyntaxElement::Node(c)) => match pattern_it.next() {
254 Some(SyntaxElement::Node(p)) => {
255 self.attempt_match_node(phase, &p, &c)?;
256 }
257 Some(p) => fail_match!("Pattern wanted '{}', code has {}", p, c.text()),
258 None => fail_match!("Pattern reached end, code has {}", c.text()),
259 },
260 }
261 }
262 }
263
264 fn attempt_match_token(
265 &self,
266 phase: &mut Phase,
267 pattern: &mut Peekable<PatternIterator>,
268 code: &syntax::SyntaxToken,
269 ) -> Result<(), MatchFailed> {
270 phase.record_ignored_comments(code);
271 // Ignore whitespace and comments.
272 if code.kind().is_trivia() {
273 return Ok(());
274 }
275 if let Some(SyntaxElement::Token(p)) = pattern.peek() {
276 // If the code has a comma and the pattern is about to close something, then accept the
277 // comma without advancing the pattern. i.e. ignore trailing commas.
278 if code.kind() == SyntaxKind::COMMA && is_closing_token(p.kind()) {
279 return Ok(());
280 }
281 // Conversely, if the pattern has a comma and the code doesn't, skip that part of the
282 // pattern and continue to match the code.
283 if p.kind() == SyntaxKind::COMMA && is_closing_token(code.kind()) {
284 pattern.next();
285 }
286 }
287 // Consume an element from the pattern and make sure it matches.
288 match pattern.next() {
289 Some(SyntaxElement::Token(p)) => {
290 if p.kind() != code.kind() || p.text() != code.text() {
291 fail_match!(
292 "Pattern wanted token '{}' ({:?}), but code had token '{}' ({:?})",
293 p.text(),
294 p.kind(),
295 code.text(),
296 code.kind()
297 )
298 }
299 }
300 Some(SyntaxElement::Node(p)) => {
301 // Not sure if this is actually reachable.
302 fail_match!(
303 "Pattern wanted {:?}, but code had token '{}' ({:?})",
304 p,
305 code.text(),
306 code.kind()
307 );
308 }
309 None => {
310 fail_match!("Pattern exhausted, while code remains: `{}`", code.text());
311 }
312 }
313 Ok(())
314 }
315
316 fn check_constraint(
317 &self,
318 constraint: &Constraint,
319 code: &SyntaxNode,
320 ) -> Result<(), MatchFailed> {
321 match constraint {
322 Constraint::Kind(kind) => {
323 kind.matches(code)?;
324 }
325 Constraint::Not(sub) => {
326 if self.check_constraint(&*sub, code).is_ok() {
327 fail_match!("Constraint {:?} failed for '{}'", constraint, code.text());
328 }
329 }
330 }
331 Ok(())
332 }
333
334 /// Paths are matched based on whether they refer to the same thing, even if they're written
335 /// differently.
336 fn attempt_match_path(
337 &self,
338 phase: &mut Phase,
339 pattern: &SyntaxNode,
340 code: &SyntaxNode,
341 ) -> Result<(), MatchFailed> {
342 if let Some(pattern_resolved) = self.rule.pattern.resolved_paths.get(pattern) {
343 let pattern_path = ast::Path::cast(pattern.clone()).unwrap();
344 let code_path = ast::Path::cast(code.clone()).unwrap();
345 if let (Some(pattern_segment), Some(code_segment)) =
346 (pattern_path.segment(), code_path.segment())
347 {
348 // Match everything within the segment except for the name-ref, which is handled
349 // separately via comparing what the path resolves to below.
350 self.attempt_match_opt(
351 phase,
352 pattern_segment.generic_arg_list(),
353 code_segment.generic_arg_list(),
354 )?;
355 self.attempt_match_opt(
356 phase,
357 pattern_segment.param_list(),
358 code_segment.param_list(),
359 )?;
360 }
361 if matches!(phase, Phase::Second(_)) {
362 let resolution = self
363 .sema
364 .resolve_path(&code_path)
365 .ok_or_else(|| match_error!("Failed to resolve path `{}`", code.text()))?;
366 if pattern_resolved.resolution != resolution {
367 fail_match!("Pattern had path `{}` code had `{}`", pattern.text(), code.text());
368 }
369 }
370 } else {
371 return self.attempt_match_node_children(phase, pattern, code);
372 }
373 Ok(())
374 }
375
376 fn attempt_match_opt<T: AstNode>(
377 &self,
378 phase: &mut Phase,
379 pattern: Option<T>,
380 code: Option<T>,
381 ) -> Result<(), MatchFailed> {
382 match (pattern, code) {
383 (Some(p), Some(c)) => self.attempt_match_node(phase, &p.syntax(), &c.syntax()),
384 (None, None) => Ok(()),
385 (Some(p), None) => fail_match!("Pattern `{}` had nothing to match", p.syntax().text()),
386 (None, Some(c)) => {
387 fail_match!("Nothing in pattern to match code `{}`", c.syntax().text())
388 }
389 }
390 }
391
392 /// We want to allow the records to match in any order, so we have special matching logic for
393 /// them.
394 fn attempt_match_record_field_list(
395 &self,
396 phase: &mut Phase,
397 pattern: &SyntaxNode,
398 code: &SyntaxNode,
399 ) -> Result<(), MatchFailed> {
400 // Build a map keyed by field name.
401 let mut fields_by_name = FxHashMap::default();
402 for child in code.children() {
403 if let Some(record) = ast::RecordExprField::cast(child.clone()) {
404 if let Some(name) = record.field_name() {
405 fields_by_name.insert(name.text().clone(), child.clone());
406 }
407 }
408 }
409 for p in pattern.children_with_tokens() {
410 if let SyntaxElement::Node(p) = p {
411 if let Some(name_element) = p.first_child_or_token() {
412 if self.get_placeholder(&name_element).is_some() {
413 // If the pattern is using placeholders for field names then order
414 // independence doesn't make sense. Fall back to regular ordered
415 // matching.
416 return self.attempt_match_node_children(phase, pattern, code);
417 }
418 if let Some(ident) = only_ident(name_element) {
419 let code_record = fields_by_name.remove(ident.text()).ok_or_else(|| {
420 match_error!(
421 "Placeholder has record field '{}', but code doesn't",
422 ident
423 )
424 })?;
425 self.attempt_match_node(phase, &p, &code_record)?;
426 }
427 }
428 }
429 }
430 if let Some(unmatched_fields) = fields_by_name.keys().next() {
431 fail_match!(
432 "{} field(s) of a record literal failed to match, starting with {}",
433 fields_by_name.len(),
434 unmatched_fields
435 );
436 }
437 Ok(())
438 }
439
440 /// Outside of token trees, a placeholder can only match a single AST node, whereas in a token
441 /// tree it can match a sequence of tokens. Note, that this code will only be used when the
442 /// pattern matches the macro invocation. For matches within the macro call, we'll already have
443 /// expanded the macro.
444 fn attempt_match_token_tree(
445 &self,
446 phase: &mut Phase,
447 pattern: &SyntaxNode,
448 code: &syntax::SyntaxNode,
449 ) -> Result<(), MatchFailed> {
450 let mut pattern = PatternIterator::new(pattern).peekable();
451 let mut children = code.children_with_tokens();
452 while let Some(child) = children.next() {
453 if let Some(placeholder) = pattern.peek().and_then(|p| self.get_placeholder(p)) {
454 pattern.next();
455 let next_pattern_token = pattern
456 .peek()
457 .and_then(|p| match p {
458 SyntaxElement::Token(t) => Some(t.clone()),
459 SyntaxElement::Node(n) => n.first_token(),
460 })
461 .map(|p| p.text().to_string());
462 let first_matched_token = child.clone();
463 let mut last_matched_token = child;
464 // Read code tokens util we reach one equal to the next token from our pattern
465 // or we reach the end of the token tree.
466 while let Some(next) = children.next() {
467 match &next {
468 SyntaxElement::Token(t) => {
469 if Some(t.to_string()) == next_pattern_token {
470 pattern.next();
471 break;
472 }
473 }
474 SyntaxElement::Node(n) => {
475 if let Some(first_token) = n.first_token() {
476 if Some(first_token.to_string()) == next_pattern_token {
477 if let Some(SyntaxElement::Node(p)) = pattern.next() {
478 // We have a subtree that starts with the next token in our pattern.
479 self.attempt_match_token_tree(phase, &p, &n)?;
480 break;
481 }
482 }
483 }
484 }
485 };
486 last_matched_token = next;
487 }
488 if let Phase::Second(match_out) = phase {
489 match_out.placeholder_values.insert(
490 placeholder.ident.clone(),
491 PlaceholderMatch::from_range(FileRange {
492 file_id: self.sema.original_range(code).file_id,
493 range: first_matched_token
494 .text_range()
495 .cover(last_matched_token.text_range()),
496 }),
497 );
498 }
499 continue;
500 }
501 // Match literal (non-placeholder) tokens.
502 match child {
503 SyntaxElement::Token(token) => {
504 self.attempt_match_token(phase, &mut pattern, &token)?;
505 }
506 SyntaxElement::Node(node) => match pattern.next() {
507 Some(SyntaxElement::Node(p)) => {
508 self.attempt_match_token_tree(phase, &p, &node)?;
509 }
510 Some(SyntaxElement::Token(p)) => fail_match!(
511 "Pattern has token '{}', code has subtree '{}'",
512 p.text(),
513 node.text()
514 ),
515 None => fail_match!("Pattern has nothing, code has '{}'", node.text()),
516 },
517 }
518 }
519 if let Some(p) = pattern.next() {
520 fail_match!("Reached end of token tree in code, but pattern still has {:?}", p);
521 }
522 Ok(())
523 }
524
525 fn attempt_match_ufcs_to_method_call(
526 &self,
527 phase: &mut Phase,
528 pattern_ufcs: &UfcsCallInfo,
529 code: &ast::MethodCallExpr,
530 ) -> Result<(), MatchFailed> {
531 use ast::ArgListOwner;
532 let code_resolved_function = self
533 .sema
534 .resolve_method_call(code)
535 .ok_or_else(|| match_error!("Failed to resolve method call"))?;
536 if pattern_ufcs.function != code_resolved_function {
537 fail_match!("Method call resolved to a different function");
538 }
539 // Check arguments.
540 let mut pattern_args = pattern_ufcs
541 .call_expr
542 .arg_list()
543 .ok_or_else(|| match_error!("Pattern function call has no args"))?
544 .args();
545 // If the function we're calling takes a self parameter, then we store additional
546 // information on the placeholder match about autoderef and autoref. This allows us to use
547 // the placeholder in a context where autoderef and autoref don't apply.
548 if code_resolved_function.has_self_param(self.sema.db) {
549 if let (Some(pattern_type), Some(expr)) = (&pattern_ufcs.qualifier_type, &code.expr()) {
550 let deref_count = self.check_expr_type(pattern_type, expr)?;
551 let pattern_receiver = pattern_args.next();
552 self.attempt_match_opt(phase, pattern_receiver.clone(), code.expr())?;
553 if let Phase::Second(match_out) = phase {
554 if let Some(placeholder_value) = pattern_receiver
555 .and_then(|n| self.get_placeholder_for_node(n.syntax()))
556 .and_then(|placeholder| {
557 match_out.placeholder_values.get_mut(&placeholder.ident)
558 })
559 {
560 placeholder_value.autoderef_count = deref_count;
561 placeholder_value.autoref_kind = self
562 .sema
563 .resolve_method_call_as_callable(code)
564 .and_then(|callable| callable.receiver_param(self.sema.db))
565 .map(|self_param| self_param.kind())
566 .unwrap_or(ast::SelfParamKind::Owned);
567 }
568 }
569 }
570 } else {
571 self.attempt_match_opt(phase, pattern_args.next(), code.expr())?;
572 }
573 let mut code_args =
574 code.arg_list().ok_or_else(|| match_error!("Code method call has no args"))?.args();
575 loop {
576 match (pattern_args.next(), code_args.next()) {
577 (None, None) => return Ok(()),
578 (p, c) => self.attempt_match_opt(phase, p, c)?,
579 }
580 }
581 }
582
583 fn attempt_match_ufcs_to_ufcs(
584 &self,
585 phase: &mut Phase,
586 pattern_ufcs: &UfcsCallInfo,
587 code: &ast::CallExpr,
588 ) -> Result<(), MatchFailed> {
589 use ast::ArgListOwner;
590 // Check that the first argument is the expected type.
591 if let (Some(pattern_type), Some(expr)) = (
592 &pattern_ufcs.qualifier_type,
593 &code.arg_list().and_then(|code_args| code_args.args().next()),
594 ) {
595 self.check_expr_type(pattern_type, expr)?;
596 }
597 self.attempt_match_node_children(phase, pattern_ufcs.call_expr.syntax(), code.syntax())
598 }
599
600 /// Verifies that `expr` matches `pattern_type`, possibly after dereferencing some number of
601 /// times. Returns the number of times it needed to be dereferenced.
602 fn check_expr_type(
603 &self,
604 pattern_type: &hir::Type,
605 expr: &ast::Expr,
606 ) -> Result<usize, MatchFailed> {
607 use hir::HirDisplay;
608 let code_type = self.sema.type_of_expr(&expr).ok_or_else(|| {
609 match_error!("Failed to get receiver type for `{}`", expr.syntax().text())
610 })?;
611 // Temporary needed to make the borrow checker happy.
612 let res = code_type
613 .autoderef(self.sema.db)
614 .enumerate()
615 .find(|(_, deref_code_type)| pattern_type == deref_code_type)
616 .map(|(count, _)| count)
617 .ok_or_else(|| {
618 match_error!(
619 "Pattern type `{}` didn't match code type `{}`",
620 pattern_type.display(self.sema.db),
621 code_type.display(self.sema.db)
622 )
623 });
624 res
625 }
626
627 fn get_placeholder_for_node(&self, node: &SyntaxNode) -> Option<&Placeholder> {
628 self.get_placeholder(&SyntaxElement::Node(node.clone()))
629 }
630
631 fn get_placeholder(&self, element: &SyntaxElement) -> Option<&Placeholder> {
632 only_ident(element.clone()).and_then(|ident| self.rule.get_placeholder(&ident))
633 }
634}
635
636impl Match {
637 fn render_template_paths(
638 &mut self,
639 template: &ResolvedPattern,
640 sema: &Semantics<ide_db::RootDatabase>,
641 ) -> Result<(), MatchFailed> {
642 let module = sema
643 .scope(&self.matched_node)
644 .module()
645 .ok_or_else(|| match_error!("Matched node isn't in a module"))?;
646 for (path, resolved_path) in &template.resolved_paths {
647 if let hir::PathResolution::Def(module_def) = resolved_path.resolution {
648 let mod_path = module.find_use_path(sema.db, module_def).ok_or_else(|| {
649 match_error!("Failed to render template path `{}` at match location")
650 })?;
651 self.rendered_template_paths.insert(path.clone(), mod_path);
652 }
653 }
654 Ok(())
655 }
656}
657
658impl Phase<'_> {
659 fn next_non_trivial(&mut self, code_it: &mut SyntaxElementChildren) -> Option<SyntaxElement> {
660 loop {
661 let c = code_it.next();
662 if let Some(SyntaxElement::Token(t)) = &c {
663 self.record_ignored_comments(t);
664 if t.kind().is_trivia() {
665 continue;
666 }
667 }
668 return c;
669 }
670 }
671
672 fn record_ignored_comments(&mut self, token: &SyntaxToken) {
673 if token.kind() == SyntaxKind::COMMENT {
674 if let Phase::Second(match_out) = self {
675 if let Some(comment) = ast::Comment::cast(token.clone()) {
676 match_out.ignored_comments.push(comment);
677 }
678 }
679 }
680 }
681}
682
683fn is_closing_token(kind: SyntaxKind) -> bool {
684 kind == SyntaxKind::R_PAREN || kind == SyntaxKind::R_CURLY || kind == SyntaxKind::R_BRACK
685}
686
687pub(crate) fn record_match_fails_reasons_scope<F, T>(debug_active: bool, f: F) -> T
688where
689 F: Fn() -> T,
690{
691 RECORDING_MATCH_FAIL_REASONS.with(|c| c.set(debug_active));
692 let res = f();
693 RECORDING_MATCH_FAIL_REASONS.with(|c| c.set(false));
694 res
695}
696
697// For performance reasons, we don't want to record the reason why every match fails, only the bit
698// of code that the user indicated they thought would match. We use a thread local to indicate when
699// we are trying to match that bit of code. This saves us having to pass a boolean into all the bits
700// of code that can make the decision to not match.
701thread_local! {
702 pub static RECORDING_MATCH_FAIL_REASONS: Cell<bool> = Cell::new(false);
703}
704
705fn recording_match_fail_reasons() -> bool {
706 RECORDING_MATCH_FAIL_REASONS.with(|c| c.get())
707}
708
709impl PlaceholderMatch {
710 fn new(node: Option<&SyntaxNode>, range: FileRange) -> Self {
711 Self {
712 node: node.cloned(),
713 range,
714 inner_matches: SsrMatches::default(),
715 autoderef_count: 0,
716 autoref_kind: ast::SelfParamKind::Owned,
717 }
718 }
719
720 fn from_range(range: FileRange) -> Self {
721 Self::new(None, range)
722 }
723}
724
725impl NodeKind {
726 fn matches(&self, node: &SyntaxNode) -> Result<(), MatchFailed> {
727 let ok = match self {
728 Self::Literal => {
729 mark::hit!(literal_constraint);
730 ast::Literal::can_cast(node.kind())
731 }
732 };
733 if !ok {
734 fail_match!("Code '{}' isn't of kind {:?}", node.text(), self);
735 }
736 Ok(())
737 }
738}
739
740// If `node` contains nothing but an ident then return it, otherwise return None.
741fn only_ident(element: SyntaxElement) -> Option<SyntaxToken> {
742 match element {
743 SyntaxElement::Token(t) => {
744 if t.kind() == SyntaxKind::IDENT {
745 return Some(t);
746 }
747 }
748 SyntaxElement::Node(n) => {
749 let mut children = n.children_with_tokens();
750 if let (Some(only_child), None) = (children.next(), children.next()) {
751 return only_ident(only_child);
752 }
753 }
754 }
755 None
756}
757
758struct PatternIterator {
759 iter: SyntaxElementChildren,
760}
761
762impl Iterator for PatternIterator {
763 type Item = SyntaxElement;
764
765 fn next(&mut self) -> Option<SyntaxElement> {
766 while let Some(element) = self.iter.next() {
767 if !element.kind().is_trivia() {
768 return Some(element);
769 }
770 }
771 None
772 }
773}
774
775impl PatternIterator {
776 fn new(parent: &SyntaxNode) -> Self {
777 Self { iter: parent.children_with_tokens() }
778 }
779}
780
781#[cfg(test)]
782mod tests {
783 use super::*;
784 use crate::{MatchFinder, SsrRule};
785
786 #[test]
787 fn parse_match_replace() {
788 let rule: SsrRule = "foo($x) ==>> bar($x)".parse().unwrap();
789 let input = "fn foo() {} fn bar() {} fn main() { foo(1+2); }";
790
791 let (db, position, selections) = crate::tests::single_file(input);
792 let mut match_finder = MatchFinder::in_context(&db, position, selections);
793 match_finder.add_rule(rule).unwrap();
794 let matches = match_finder.matches();
795 assert_eq!(matches.matches.len(), 1);
796 assert_eq!(matches.matches[0].matched_node.text(), "foo(1+2)");
797 assert_eq!(matches.matches[0].placeholder_values.len(), 1);
798 assert_eq!(
799 matches.matches[0].placeholder_values[&Var("x".to_string())]
800 .node
801 .as_ref()
802 .unwrap()
803 .text(),
804 "1+2"
805 );
806
807 let edits = match_finder.edits();
808 assert_eq!(edits.len(), 1);
809 let edit = &edits[0];
810 let mut after = input.to_string();
811 edit.edit.apply(&mut after);
812 assert_eq!(after, "fn foo() {} fn bar() {} fn main() { bar(1+2); }");
813 }
814}
diff --git a/crates/ssr/src/nester.rs b/crates/ssr/src/nester.rs
new file mode 100644
index 000000000..6ac355dfc
--- /dev/null
+++ b/crates/ssr/src/nester.rs
@@ -0,0 +1,94 @@
1//! Converts a flat collection of matches into a nested form suitable for replacement. When there
2//! are multiple matches for a node, or that overlap, priority is given to the earlier rule. Nested
3//! matches are only permitted if the inner match is contained entirely within a placeholder of an
4//! outer match.
5//!
6//! For example, if our search pattern is `foo(foo($a))` and the code had `foo(foo(foo(foo(42))))`,
7//! then we'll get 3 matches, however only the outermost and innermost matches can be accepted. The
8//! middle match would take the second `foo` from the outer match.
9
10use crate::{Match, SsrMatches};
11use rustc_hash::FxHashMap;
12use syntax::SyntaxNode;
13
14pub(crate) fn nest_and_remove_collisions(
15 mut matches: Vec<Match>,
16 sema: &hir::Semantics<ide_db::RootDatabase>,
17) -> SsrMatches {
18 // We sort the matches by depth then by rule index. Sorting by depth means that by the time we
19 // see a match, any parent matches or conflicting matches will have already been seen. Sorting
20 // by rule_index means that if there are two matches for the same node, the rule added first
21 // will take precedence.
22 matches.sort_by(|a, b| a.depth.cmp(&b.depth).then_with(|| a.rule_index.cmp(&b.rule_index)));
23 let mut collector = MatchCollector::default();
24 for m in matches {
25 collector.add_match(m, sema);
26 }
27 collector.into()
28}
29
30#[derive(Default)]
31struct MatchCollector {
32 matches_by_node: FxHashMap<SyntaxNode, Match>,
33}
34
35impl MatchCollector {
36 /// Attempts to add `m` to matches. If it conflicts with an existing match, it is discarded. If
37 /// it is entirely within the a placeholder of an existing match, then it is added as a child
38 /// match of the existing match.
39 fn add_match(&mut self, m: Match, sema: &hir::Semantics<ide_db::RootDatabase>) {
40 let matched_node = m.matched_node.clone();
41 if let Some(existing) = self.matches_by_node.get_mut(&matched_node) {
42 try_add_sub_match(m, existing, sema);
43 return;
44 }
45 for ancestor in sema.ancestors_with_macros(m.matched_node.clone()) {
46 if let Some(existing) = self.matches_by_node.get_mut(&ancestor) {
47 try_add_sub_match(m, existing, sema);
48 return;
49 }
50 }
51 self.matches_by_node.insert(matched_node, m);
52 }
53}
54
55/// Attempts to add `m` as a sub-match of `existing`.
56fn try_add_sub_match(m: Match, existing: &mut Match, sema: &hir::Semantics<ide_db::RootDatabase>) {
57 for p in existing.placeholder_values.values_mut() {
58 // Note, no need to check if p.range.file is equal to m.range.file, since we
59 // already know we're within `existing`.
60 if p.range.range.contains_range(m.range.range) {
61 // Convert the inner matches in `p` into a temporary MatchCollector. When
62 // we're done, we then convert it back into an SsrMatches. If we expected
63 // lots of inner matches, it might be worthwhile keeping a MatchCollector
64 // around for each placeholder match. However we expect most placeholder
65 // will have 0 and a few will have 1. More than that should hopefully be
66 // exceptional.
67 let mut collector = MatchCollector::default();
68 for m in std::mem::replace(&mut p.inner_matches.matches, Vec::new()) {
69 collector.matches_by_node.insert(m.matched_node.clone(), m);
70 }
71 collector.add_match(m, sema);
72 p.inner_matches = collector.into();
73 break;
74 }
75 }
76}
77
78impl From<MatchCollector> for SsrMatches {
79 fn from(mut match_collector: MatchCollector) -> Self {
80 let mut matches = SsrMatches::default();
81 for (_, m) in match_collector.matches_by_node.drain() {
82 matches.matches.push(m);
83 }
84 matches.matches.sort_by(|a, b| {
85 // Order matches by file_id then by start range. This should be sufficient since ranges
86 // shouldn't be overlapping.
87 a.range
88 .file_id
89 .cmp(&b.range.file_id)
90 .then_with(|| a.range.range.start().cmp(&b.range.range.start()))
91 });
92 matches
93 }
94}
diff --git a/crates/ssr/src/parsing.rs b/crates/ssr/src/parsing.rs
new file mode 100644
index 000000000..05b66dcd7
--- /dev/null
+++ b/crates/ssr/src/parsing.rs
@@ -0,0 +1,403 @@
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::errors::bail;
9use crate::{SsrError, SsrPattern, SsrRule};
10use rustc_hash::{FxHashMap, FxHashSet};
11use std::{fmt::Display, str::FromStr};
12use syntax::{ast, AstNode, SmolStr, SyntaxKind, SyntaxNode, T};
13use test_utils::mark;
14
15#[derive(Debug)]
16pub(crate) struct ParsedRule {
17 pub(crate) placeholders_by_stand_in: FxHashMap<SmolStr, Placeholder>,
18 pub(crate) pattern: SyntaxNode,
19 pub(crate) template: Option<SyntaxNode>,
20}
21
22#[derive(Debug)]
23pub(crate) struct RawPattern {
24 tokens: Vec<PatternElement>,
25}
26
27// Part of a search or replace pattern.
28#[derive(Clone, Debug, PartialEq, Eq)]
29pub(crate) enum PatternElement {
30 Token(Token),
31 Placeholder(Placeholder),
32}
33
34#[derive(Clone, Debug, PartialEq, Eq)]
35pub(crate) struct Placeholder {
36 /// The name of this placeholder. e.g. for "$a", this would be "a"
37 pub(crate) ident: Var,
38 /// A unique name used in place of this placeholder when we parse the pattern as Rust code.
39 stand_in_name: String,
40 pub(crate) constraints: Vec<Constraint>,
41}
42
43/// Represents a `$var` in an SSR query.
44#[derive(Debug, Clone, PartialEq, Eq, Hash)]
45pub(crate) struct Var(pub String);
46
47#[derive(Clone, Debug, PartialEq, Eq)]
48pub(crate) enum Constraint {
49 Kind(NodeKind),
50 Not(Box<Constraint>),
51}
52
53#[derive(Clone, Debug, PartialEq, Eq)]
54pub(crate) enum NodeKind {
55 Literal,
56}
57
58#[derive(Debug, Clone, PartialEq, Eq)]
59pub(crate) struct Token {
60 kind: SyntaxKind,
61 pub(crate) text: SmolStr,
62}
63
64impl ParsedRule {
65 fn new(
66 pattern: &RawPattern,
67 template: Option<&RawPattern>,
68 ) -> Result<Vec<ParsedRule>, SsrError> {
69 let raw_pattern = pattern.as_rust_code();
70 let raw_template = template.map(|t| t.as_rust_code());
71 let raw_template = raw_template.as_ref().map(|s| s.as_str());
72 let mut builder = RuleBuilder {
73 placeholders_by_stand_in: pattern.placeholders_by_stand_in(),
74 rules: Vec::new(),
75 };
76 builder.try_add(ast::Expr::parse(&raw_pattern), raw_template.map(ast::Expr::parse));
77 builder.try_add(ast::Type::parse(&raw_pattern), raw_template.map(ast::Type::parse));
78 builder.try_add(ast::Item::parse(&raw_pattern), raw_template.map(ast::Item::parse));
79 builder.try_add(ast::Path::parse(&raw_pattern), raw_template.map(ast::Path::parse));
80 builder.try_add(ast::Pat::parse(&raw_pattern), raw_template.map(ast::Pat::parse));
81 builder.build()
82 }
83}
84
85struct RuleBuilder {
86 placeholders_by_stand_in: FxHashMap<SmolStr, Placeholder>,
87 rules: Vec<ParsedRule>,
88}
89
90impl RuleBuilder {
91 fn try_add<T: AstNode>(&mut self, pattern: Result<T, ()>, template: Option<Result<T, ()>>) {
92 match (pattern, template) {
93 (Ok(pattern), Some(Ok(template))) => self.rules.push(ParsedRule {
94 placeholders_by_stand_in: self.placeholders_by_stand_in.clone(),
95 pattern: pattern.syntax().clone(),
96 template: Some(template.syntax().clone()),
97 }),
98 (Ok(pattern), None) => self.rules.push(ParsedRule {
99 placeholders_by_stand_in: self.placeholders_by_stand_in.clone(),
100 pattern: pattern.syntax().clone(),
101 template: None,
102 }),
103 _ => {}
104 }
105 }
106
107 fn build(mut self) -> Result<Vec<ParsedRule>, SsrError> {
108 if self.rules.is_empty() {
109 bail!("Not a valid Rust expression, type, item, path or pattern");
110 }
111 // If any rules contain paths, then we reject any rules that don't contain paths. Allowing a
112 // mix leads to strange semantics, since the path-based rules only match things where the
113 // path refers to semantically the same thing, whereas the non-path-based rules could match
114 // anything. Specifically, if we have a rule like `foo ==>> bar` we only want to match the
115 // `foo` that is in the current scope, not any `foo`. However "foo" can be parsed as a
116 // pattern (IDENT_PAT -> NAME -> IDENT). Allowing such a rule through would result in
117 // renaming everything called `foo` to `bar`. It'd also be slow, since without a path, we'd
118 // have to use the slow-scan search mechanism.
119 if self.rules.iter().any(|rule| contains_path(&rule.pattern)) {
120 let old_len = self.rules.len();
121 self.rules.retain(|rule| contains_path(&rule.pattern));
122 if self.rules.len() < old_len {
123 mark::hit!(pattern_is_a_single_segment_path);
124 }
125 }
126 Ok(self.rules)
127 }
128}
129
130/// Returns whether there are any paths in `node`.
131fn contains_path(node: &SyntaxNode) -> bool {
132 node.kind() == SyntaxKind::PATH
133 || node.descendants().any(|node| node.kind() == SyntaxKind::PATH)
134}
135
136impl FromStr for SsrRule {
137 type Err = SsrError;
138
139 fn from_str(query: &str) -> Result<SsrRule, SsrError> {
140 let mut it = query.split("==>>");
141 let pattern = it.next().expect("at least empty string").trim();
142 let template = it
143 .next()
144 .ok_or_else(|| SsrError("Cannot find delimiter `==>>`".into()))?
145 .trim()
146 .to_string();
147 if it.next().is_some() {
148 return Err(SsrError("More than one delimiter found".into()));
149 }
150 let raw_pattern = pattern.parse()?;
151 let raw_template = template.parse()?;
152 let parsed_rules = ParsedRule::new(&raw_pattern, Some(&raw_template))?;
153 let rule = SsrRule { pattern: raw_pattern, template: raw_template, parsed_rules };
154 validate_rule(&rule)?;
155 Ok(rule)
156 }
157}
158
159impl FromStr for RawPattern {
160 type Err = SsrError;
161
162 fn from_str(pattern_str: &str) -> Result<RawPattern, SsrError> {
163 Ok(RawPattern { tokens: parse_pattern(pattern_str)? })
164 }
165}
166
167impl RawPattern {
168 /// Returns this search pattern as Rust source code that we can feed to the Rust parser.
169 fn as_rust_code(&self) -> String {
170 let mut res = String::new();
171 for t in &self.tokens {
172 res.push_str(match t {
173 PatternElement::Token(token) => token.text.as_str(),
174 PatternElement::Placeholder(placeholder) => placeholder.stand_in_name.as_str(),
175 });
176 }
177 res
178 }
179
180 pub(crate) fn placeholders_by_stand_in(&self) -> FxHashMap<SmolStr, Placeholder> {
181 let mut res = FxHashMap::default();
182 for t in &self.tokens {
183 if let PatternElement::Placeholder(placeholder) = t {
184 res.insert(SmolStr::new(placeholder.stand_in_name.clone()), placeholder.clone());
185 }
186 }
187 res
188 }
189}
190
191impl FromStr for SsrPattern {
192 type Err = SsrError;
193
194 fn from_str(pattern_str: &str) -> Result<SsrPattern, SsrError> {
195 let raw_pattern = pattern_str.parse()?;
196 let parsed_rules = ParsedRule::new(&raw_pattern, None)?;
197 Ok(SsrPattern { raw: raw_pattern, parsed_rules })
198 }
199}
200
201/// Returns `pattern_str`, parsed as a search or replace pattern. If `remove_whitespace` is true,
202/// then any whitespace tokens will be removed, which we do for the search pattern, but not for the
203/// replace pattern.
204fn parse_pattern(pattern_str: &str) -> Result<Vec<PatternElement>, SsrError> {
205 let mut res = Vec::new();
206 let mut placeholder_names = FxHashSet::default();
207 let mut tokens = tokenize(pattern_str)?.into_iter();
208 while let Some(token) = tokens.next() {
209 if token.kind == T![$] {
210 let placeholder = parse_placeholder(&mut tokens)?;
211 if !placeholder_names.insert(placeholder.ident.clone()) {
212 bail!("Placeholder `{}` repeats more than once", placeholder.ident);
213 }
214 res.push(PatternElement::Placeholder(placeholder));
215 } else {
216 res.push(PatternElement::Token(token));
217 }
218 }
219 Ok(res)
220}
221
222/// Checks for errors in a rule. e.g. the replace pattern referencing placeholders that the search
223/// pattern didn't define.
224fn validate_rule(rule: &SsrRule) -> Result<(), SsrError> {
225 let mut defined_placeholders = FxHashSet::default();
226 for p in &rule.pattern.tokens {
227 if let PatternElement::Placeholder(placeholder) = p {
228 defined_placeholders.insert(&placeholder.ident);
229 }
230 }
231 let mut undefined = Vec::new();
232 for p in &rule.template.tokens {
233 if let PatternElement::Placeholder(placeholder) = p {
234 if !defined_placeholders.contains(&placeholder.ident) {
235 undefined.push(placeholder.ident.to_string());
236 }
237 if !placeholder.constraints.is_empty() {
238 bail!("Replacement placeholders cannot have constraints");
239 }
240 }
241 }
242 if !undefined.is_empty() {
243 bail!("Replacement contains undefined placeholders: {}", undefined.join(", "));
244 }
245 Ok(())
246}
247
248fn tokenize(source: &str) -> Result<Vec<Token>, SsrError> {
249 let mut start = 0;
250 let (raw_tokens, errors) = syntax::tokenize(source);
251 if let Some(first_error) = errors.first() {
252 bail!("Failed to parse pattern: {}", first_error);
253 }
254 let mut tokens: Vec<Token> = Vec::new();
255 for raw_token in raw_tokens {
256 let token_len = usize::from(raw_token.len);
257 tokens.push(Token {
258 kind: raw_token.kind,
259 text: SmolStr::new(&source[start..start + token_len]),
260 });
261 start += token_len;
262 }
263 Ok(tokens)
264}
265
266fn parse_placeholder(tokens: &mut std::vec::IntoIter<Token>) -> Result<Placeholder, SsrError> {
267 let mut name = None;
268 let mut constraints = Vec::new();
269 if let Some(token) = tokens.next() {
270 match token.kind {
271 SyntaxKind::IDENT => {
272 name = Some(token.text);
273 }
274 T!['{'] => {
275 let token =
276 tokens.next().ok_or_else(|| SsrError::new("Unexpected end of placeholder"))?;
277 if token.kind == SyntaxKind::IDENT {
278 name = Some(token.text);
279 }
280 loop {
281 let token = tokens
282 .next()
283 .ok_or_else(|| SsrError::new("Placeholder is missing closing brace '}'"))?;
284 match token.kind {
285 T![:] => {
286 constraints.push(parse_constraint(tokens)?);
287 }
288 T!['}'] => break,
289 _ => bail!("Unexpected token while parsing placeholder: '{}'", token.text),
290 }
291 }
292 }
293 _ => {
294 bail!("Placeholders should either be $name or ${{name:constraints}}");
295 }
296 }
297 }
298 let name = name.ok_or_else(|| SsrError::new("Placeholder ($) with no name"))?;
299 Ok(Placeholder::new(name, constraints))
300}
301
302fn parse_constraint(tokens: &mut std::vec::IntoIter<Token>) -> Result<Constraint, SsrError> {
303 let constraint_type = tokens
304 .next()
305 .ok_or_else(|| SsrError::new("Found end of placeholder while looking for a constraint"))?
306 .text
307 .to_string();
308 match constraint_type.as_str() {
309 "kind" => {
310 expect_token(tokens, "(")?;
311 let t = tokens.next().ok_or_else(|| {
312 SsrError::new("Unexpected end of constraint while looking for kind")
313 })?;
314 if t.kind != SyntaxKind::IDENT {
315 bail!("Expected ident, found {:?} while parsing kind constraint", t.kind);
316 }
317 expect_token(tokens, ")")?;
318 Ok(Constraint::Kind(NodeKind::from(&t.text)?))
319 }
320 "not" => {
321 expect_token(tokens, "(")?;
322 let sub = parse_constraint(tokens)?;
323 expect_token(tokens, ")")?;
324 Ok(Constraint::Not(Box::new(sub)))
325 }
326 x => bail!("Unsupported constraint type '{}'", x),
327 }
328}
329
330fn expect_token(tokens: &mut std::vec::IntoIter<Token>, expected: &str) -> Result<(), SsrError> {
331 if let Some(t) = tokens.next() {
332 if t.text == expected {
333 return Ok(());
334 }
335 bail!("Expected {} found {}", expected, t.text);
336 }
337 bail!("Expected {} found end of stream", expected);
338}
339
340impl NodeKind {
341 fn from(name: &SmolStr) -> Result<NodeKind, SsrError> {
342 Ok(match name.as_str() {
343 "literal" => NodeKind::Literal,
344 _ => bail!("Unknown node kind '{}'", name),
345 })
346 }
347}
348
349impl Placeholder {
350 fn new(name: SmolStr, constraints: Vec<Constraint>) -> Self {
351 Self {
352 stand_in_name: format!("__placeholder_{}", name),
353 constraints,
354 ident: Var(name.to_string()),
355 }
356 }
357}
358
359impl Display for Var {
360 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
361 write!(f, "${}", self.0)
362 }
363}
364
365#[cfg(test)]
366mod tests {
367 use super::*;
368
369 #[test]
370 fn parser_happy_case() {
371 fn token(kind: SyntaxKind, text: &str) -> PatternElement {
372 PatternElement::Token(Token { kind, text: SmolStr::new(text) })
373 }
374 fn placeholder(name: &str) -> PatternElement {
375 PatternElement::Placeholder(Placeholder::new(SmolStr::new(name), Vec::new()))
376 }
377 let result: SsrRule = "foo($a, $b) ==>> bar($b, $a)".parse().unwrap();
378 assert_eq!(
379 result.pattern.tokens,
380 vec![
381 token(SyntaxKind::IDENT, "foo"),
382 token(T!['('], "("),
383 placeholder("a"),
384 token(T![,], ","),
385 token(SyntaxKind::WHITESPACE, " "),
386 placeholder("b"),
387 token(T![')'], ")"),
388 ]
389 );
390 assert_eq!(
391 result.template.tokens,
392 vec![
393 token(SyntaxKind::IDENT, "bar"),
394 token(T!['('], "("),
395 placeholder("b"),
396 token(T![,], ","),
397 token(SyntaxKind::WHITESPACE, " "),
398 placeholder("a"),
399 token(T![')'], ")"),
400 ]
401 );
402 }
403}
diff --git a/crates/ssr/src/replacing.rs b/crates/ssr/src/replacing.rs
new file mode 100644
index 000000000..29284e3f1
--- /dev/null
+++ b/crates/ssr/src/replacing.rs
@@ -0,0 +1,240 @@
1//! Code for applying replacement templates for matches that have previously been found.
2
3use crate::{resolving::ResolvedRule, Match, SsrMatches};
4use itertools::Itertools;
5use rustc_hash::{FxHashMap, FxHashSet};
6use syntax::ast::{self, AstToken};
7use syntax::{SyntaxElement, SyntaxKind, SyntaxNode, SyntaxToken, TextRange, TextSize};
8use test_utils::mark;
9use text_edit::TextEdit;
10
11/// Returns a text edit that will replace each match in `matches` with its corresponding replacement
12/// template. Placeholders in the template will have been substituted with whatever they matched to
13/// in the original code.
14pub(crate) fn matches_to_edit(
15 matches: &SsrMatches,
16 file_src: &str,
17 rules: &[ResolvedRule],
18) -> TextEdit {
19 matches_to_edit_at_offset(matches, file_src, 0.into(), rules)
20}
21
22fn matches_to_edit_at_offset(
23 matches: &SsrMatches,
24 file_src: &str,
25 relative_start: TextSize,
26 rules: &[ResolvedRule],
27) -> TextEdit {
28 let mut edit_builder = TextEdit::builder();
29 for m in &matches.matches {
30 edit_builder.replace(
31 m.range.range.checked_sub(relative_start).unwrap(),
32 render_replace(m, file_src, rules),
33 );
34 }
35 edit_builder.finish()
36}
37
38struct ReplacementRenderer<'a> {
39 match_info: &'a Match,
40 file_src: &'a str,
41 rules: &'a [ResolvedRule],
42 rule: &'a ResolvedRule,
43 out: String,
44 // Map from a range within `out` to a token in `template` that represents a placeholder. This is
45 // used to validate that the generated source code doesn't split any placeholder expansions (see
46 // below).
47 placeholder_tokens_by_range: FxHashMap<TextRange, SyntaxToken>,
48 // Which placeholder tokens need to be wrapped in parenthesis in order to ensure that when `out`
49 // is parsed, placeholders don't get split. e.g. if a template of `$a.to_string()` results in `1
50 // + 2.to_string()` then the placeholder value `1 + 2` was split and needs parenthesis.
51 placeholder_tokens_requiring_parenthesis: FxHashSet<SyntaxToken>,
52}
53
54fn render_replace(match_info: &Match, file_src: &str, rules: &[ResolvedRule]) -> String {
55 let rule = &rules[match_info.rule_index];
56 let template = rule
57 .template
58 .as_ref()
59 .expect("You called MatchFinder::edits after calling MatchFinder::add_search_pattern");
60 let mut renderer = ReplacementRenderer {
61 match_info,
62 file_src,
63 rules,
64 rule,
65 out: String::new(),
66 placeholder_tokens_requiring_parenthesis: FxHashSet::default(),
67 placeholder_tokens_by_range: FxHashMap::default(),
68 };
69 renderer.render_node(&template.node);
70 renderer.maybe_rerender_with_extra_parenthesis(&template.node);
71 for comment in &match_info.ignored_comments {
72 renderer.out.push_str(&comment.syntax().to_string());
73 }
74 renderer.out
75}
76
77impl ReplacementRenderer<'_> {
78 fn render_node_children(&mut self, node: &SyntaxNode) {
79 for node_or_token in node.children_with_tokens() {
80 self.render_node_or_token(&node_or_token);
81 }
82 }
83
84 fn render_node_or_token(&mut self, node_or_token: &SyntaxElement) {
85 match node_or_token {
86 SyntaxElement::Token(token) => {
87 self.render_token(&token);
88 }
89 SyntaxElement::Node(child_node) => {
90 self.render_node(&child_node);
91 }
92 }
93 }
94
95 fn render_node(&mut self, node: &SyntaxNode) {
96 use syntax::ast::AstNode;
97 if let Some(mod_path) = self.match_info.rendered_template_paths.get(&node) {
98 self.out.push_str(&mod_path.to_string());
99 // Emit everything except for the segment's name-ref, since we already effectively
100 // emitted that as part of `mod_path`.
101 if let Some(path) = ast::Path::cast(node.clone()) {
102 if let Some(segment) = path.segment() {
103 for node_or_token in segment.syntax().children_with_tokens() {
104 if node_or_token.kind() != SyntaxKind::NAME_REF {
105 self.render_node_or_token(&node_or_token);
106 }
107 }
108 }
109 }
110 } else {
111 self.render_node_children(&node);
112 }
113 }
114
115 fn render_token(&mut self, token: &SyntaxToken) {
116 if let Some(placeholder) = self.rule.get_placeholder(&token) {
117 if let Some(placeholder_value) =
118 self.match_info.placeholder_values.get(&placeholder.ident)
119 {
120 let range = &placeholder_value.range.range;
121 let mut matched_text =
122 self.file_src[usize::from(range.start())..usize::from(range.end())].to_owned();
123 // If a method call is performed directly on the placeholder, then autoderef and
124 // autoref will apply, so we can just substitute whatever the placeholder matched to
125 // directly. If we're not applying a method call, then we need to add explicitly
126 // deref and ref in order to match whatever was being done implicitly at the match
127 // site.
128 if !token_is_method_call_receiver(token)
129 && (placeholder_value.autoderef_count > 0
130 || placeholder_value.autoref_kind != ast::SelfParamKind::Owned)
131 {
132 mark::hit!(replace_autoref_autoderef_capture);
133 let ref_kind = match placeholder_value.autoref_kind {
134 ast::SelfParamKind::Owned => "",
135 ast::SelfParamKind::Ref => "&",
136 ast::SelfParamKind::MutRef => "&mut ",
137 };
138 matched_text = format!(
139 "{}{}{}",
140 ref_kind,
141 "*".repeat(placeholder_value.autoderef_count),
142 matched_text
143 );
144 }
145 let edit = matches_to_edit_at_offset(
146 &placeholder_value.inner_matches,
147 self.file_src,
148 range.start(),
149 self.rules,
150 );
151 let needs_parenthesis =
152 self.placeholder_tokens_requiring_parenthesis.contains(token);
153 edit.apply(&mut matched_text);
154 if needs_parenthesis {
155 self.out.push('(');
156 }
157 self.placeholder_tokens_by_range.insert(
158 TextRange::new(
159 TextSize::of(&self.out),
160 TextSize::of(&self.out) + TextSize::of(&matched_text),
161 ),
162 token.clone(),
163 );
164 self.out.push_str(&matched_text);
165 if needs_parenthesis {
166 self.out.push(')');
167 }
168 } else {
169 // We validated that all placeholder references were valid before we
170 // started, so this shouldn't happen.
171 panic!(
172 "Internal error: replacement referenced unknown placeholder {}",
173 placeholder.ident
174 );
175 }
176 } else {
177 self.out.push_str(token.text().as_str());
178 }
179 }
180
181 // Checks if the resulting code, when parsed doesn't split any placeholders due to different
182 // order of operations between the search pattern and the replacement template. If any do, then
183 // we rerender the template and wrap the problematic placeholders with parenthesis.
184 fn maybe_rerender_with_extra_parenthesis(&mut self, template: &SyntaxNode) {
185 if let Some(node) = parse_as_kind(&self.out, template.kind()) {
186 self.remove_node_ranges(node);
187 if self.placeholder_tokens_by_range.is_empty() {
188 return;
189 }
190 self.placeholder_tokens_requiring_parenthesis =
191 self.placeholder_tokens_by_range.values().cloned().collect();
192 self.out.clear();
193 self.render_node(template);
194 }
195 }
196
197 fn remove_node_ranges(&mut self, node: SyntaxNode) {
198 self.placeholder_tokens_by_range.remove(&node.text_range());
199 for child in node.children() {
200 self.remove_node_ranges(child);
201 }
202 }
203}
204
205/// Returns whether token is the receiver of a method call. Note, being within the receiver of a
206/// method call doesn't count. e.g. if the token is `$a`, then `$a.foo()` will return true, while
207/// `($a + $b).foo()` or `x.foo($a)` will return false.
208fn token_is_method_call_receiver(token: &SyntaxToken) -> bool {
209 use syntax::ast::AstNode;
210 // Find the first method call among the ancestors of `token`, then check if the only token
211 // within the receiver is `token`.
212 if let Some(receiver) =
213 token.ancestors().find_map(ast::MethodCallExpr::cast).and_then(|call| call.expr())
214 {
215 let tokens = receiver.syntax().descendants_with_tokens().filter_map(|node_or_token| {
216 match node_or_token {
217 SyntaxElement::Token(t) => Some(t),
218 _ => None,
219 }
220 });
221 if let Some((only_token,)) = tokens.collect_tuple() {
222 return only_token == *token;
223 }
224 }
225 false
226}
227
228fn parse_as_kind(code: &str, kind: SyntaxKind) -> Option<SyntaxNode> {
229 use syntax::ast::AstNode;
230 if ast::Expr::can_cast(kind) {
231 if let Ok(expr) = ast::Expr::parse(code) {
232 return Some(expr.syntax().clone());
233 }
234 } else if ast::Item::can_cast(kind) {
235 if let Ok(item) = ast::Item::parse(code) {
236 return Some(item.syntax().clone());
237 }
238 }
239 None
240}
diff --git a/crates/ssr/src/resolving.rs b/crates/ssr/src/resolving.rs
new file mode 100644
index 000000000..b932132d5
--- /dev/null
+++ b/crates/ssr/src/resolving.rs
@@ -0,0 +1,301 @@
1//! This module is responsible for resolving paths within rules.
2
3use crate::errors::error;
4use crate::{parsing, SsrError};
5use base_db::FilePosition;
6use parsing::Placeholder;
7use rustc_hash::FxHashMap;
8use syntax::{ast, SmolStr, SyntaxKind, SyntaxNode, SyntaxToken};
9use test_utils::mark;
10
11pub(crate) struct ResolutionScope<'db> {
12 scope: hir::SemanticsScope<'db>,
13 node: SyntaxNode,
14}
15
16pub(crate) struct ResolvedRule {
17 pub(crate) pattern: ResolvedPattern,
18 pub(crate) template: Option<ResolvedPattern>,
19 pub(crate) index: usize,
20}
21
22pub(crate) struct ResolvedPattern {
23 pub(crate) placeholders_by_stand_in: FxHashMap<SmolStr, parsing::Placeholder>,
24 pub(crate) node: SyntaxNode,
25 // Paths in `node` that we've resolved.
26 pub(crate) resolved_paths: FxHashMap<SyntaxNode, ResolvedPath>,
27 pub(crate) ufcs_function_calls: FxHashMap<SyntaxNode, UfcsCallInfo>,
28 pub(crate) contains_self: bool,
29}
30
31pub(crate) struct ResolvedPath {
32 pub(crate) resolution: hir::PathResolution,
33 /// The depth of the ast::Path that was resolved within the pattern.
34 pub(crate) depth: u32,
35}
36
37pub(crate) struct UfcsCallInfo {
38 pub(crate) call_expr: ast::CallExpr,
39 pub(crate) function: hir::Function,
40 pub(crate) qualifier_type: Option<hir::Type>,
41}
42
43impl ResolvedRule {
44 pub(crate) fn new(
45 rule: parsing::ParsedRule,
46 resolution_scope: &ResolutionScope,
47 index: usize,
48 ) -> Result<ResolvedRule, SsrError> {
49 let resolver =
50 Resolver { resolution_scope, placeholders_by_stand_in: rule.placeholders_by_stand_in };
51 let resolved_template = if let Some(template) = rule.template {
52 Some(resolver.resolve_pattern_tree(template)?)
53 } else {
54 None
55 };
56 Ok(ResolvedRule {
57 pattern: resolver.resolve_pattern_tree(rule.pattern)?,
58 template: resolved_template,
59 index,
60 })
61 }
62
63 pub(crate) fn get_placeholder(&self, token: &SyntaxToken) -> Option<&Placeholder> {
64 if token.kind() != SyntaxKind::IDENT {
65 return None;
66 }
67 self.pattern.placeholders_by_stand_in.get(token.text())
68 }
69}
70
71struct Resolver<'a, 'db> {
72 resolution_scope: &'a ResolutionScope<'db>,
73 placeholders_by_stand_in: FxHashMap<SmolStr, parsing::Placeholder>,
74}
75
76impl Resolver<'_, '_> {
77 fn resolve_pattern_tree(&self, pattern: SyntaxNode) -> Result<ResolvedPattern, SsrError> {
78 use syntax::ast::AstNode;
79 use syntax::{SyntaxElement, T};
80 let mut resolved_paths = FxHashMap::default();
81 self.resolve(pattern.clone(), 0, &mut resolved_paths)?;
82 let ufcs_function_calls = resolved_paths
83 .iter()
84 .filter_map(|(path_node, resolved)| {
85 if let Some(grandparent) = path_node.parent().and_then(|parent| parent.parent()) {
86 if let Some(call_expr) = ast::CallExpr::cast(grandparent.clone()) {
87 if let hir::PathResolution::AssocItem(hir::AssocItem::Function(function)) =
88 resolved.resolution
89 {
90 let qualifier_type = self.resolution_scope.qualifier_type(path_node);
91 return Some((
92 grandparent,
93 UfcsCallInfo { call_expr, function, qualifier_type },
94 ));
95 }
96 }
97 }
98 None
99 })
100 .collect();
101 let contains_self =
102 pattern.descendants_with_tokens().any(|node_or_token| match node_or_token {
103 SyntaxElement::Token(t) => t.kind() == T![self],
104 _ => false,
105 });
106 Ok(ResolvedPattern {
107 node: pattern,
108 resolved_paths,
109 placeholders_by_stand_in: self.placeholders_by_stand_in.clone(),
110 ufcs_function_calls,
111 contains_self,
112 })
113 }
114
115 fn resolve(
116 &self,
117 node: SyntaxNode,
118 depth: u32,
119 resolved_paths: &mut FxHashMap<SyntaxNode, ResolvedPath>,
120 ) -> Result<(), SsrError> {
121 use syntax::ast::AstNode;
122 if let Some(path) = ast::Path::cast(node.clone()) {
123 if is_self(&path) {
124 // Self cannot be resolved like other paths.
125 return Ok(());
126 }
127 // Check if this is an appropriate place in the path to resolve. If the path is
128 // something like `a::B::<i32>::c` then we want to resolve `a::B`. If the path contains
129 // a placeholder. e.g. `a::$b::c` then we want to resolve `a`.
130 if !path_contains_type_arguments(path.qualifier())
131 && !self.path_contains_placeholder(&path)
132 {
133 let resolution = self
134 .resolution_scope
135 .resolve_path(&path)
136 .ok_or_else(|| error!("Failed to resolve path `{}`", node.text()))?;
137 if self.ok_to_use_path_resolution(&resolution) {
138 resolved_paths.insert(node, ResolvedPath { resolution, depth });
139 return Ok(());
140 }
141 }
142 }
143 for node in node.children() {
144 self.resolve(node, depth + 1, resolved_paths)?;
145 }
146 Ok(())
147 }
148
149 /// Returns whether `path` contains a placeholder, but ignores any placeholders within type
150 /// arguments.
151 fn path_contains_placeholder(&self, path: &ast::Path) -> bool {
152 if let Some(segment) = path.segment() {
153 if let Some(name_ref) = segment.name_ref() {
154 if self.placeholders_by_stand_in.contains_key(name_ref.text()) {
155 return true;
156 }
157 }
158 }
159 if let Some(qualifier) = path.qualifier() {
160 return self.path_contains_placeholder(&qualifier);
161 }
162 false
163 }
164
165 fn ok_to_use_path_resolution(&self, resolution: &hir::PathResolution) -> bool {
166 match resolution {
167 hir::PathResolution::AssocItem(hir::AssocItem::Function(function)) => {
168 if function.has_self_param(self.resolution_scope.scope.db) {
169 // If we don't use this path resolution, then we won't be able to match method
170 // calls. e.g. `Foo::bar($s)` should match `x.bar()`.
171 true
172 } else {
173 mark::hit!(replace_associated_trait_default_function_call);
174 false
175 }
176 }
177 hir::PathResolution::AssocItem(_) => {
178 // Not a function. Could be a constant or an associated type.
179 mark::hit!(replace_associated_trait_constant);
180 false
181 }
182 _ => true,
183 }
184 }
185}
186
187impl<'db> ResolutionScope<'db> {
188 pub(crate) fn new(
189 sema: &hir::Semantics<'db, ide_db::RootDatabase>,
190 resolve_context: FilePosition,
191 ) -> ResolutionScope<'db> {
192 use syntax::ast::AstNode;
193 let file = sema.parse(resolve_context.file_id);
194 // Find a node at the requested position, falling back to the whole file.
195 let node = file
196 .syntax()
197 .token_at_offset(resolve_context.offset)
198 .left_biased()
199 .map(|token| token.parent())
200 .unwrap_or_else(|| file.syntax().clone());
201 let node = pick_node_for_resolution(node);
202 let scope = sema.scope(&node);
203 ResolutionScope { scope, node }
204 }
205
206 /// Returns the function in which SSR was invoked, if any.
207 pub(crate) fn current_function(&self) -> Option<SyntaxNode> {
208 self.node.ancestors().find(|node| node.kind() == SyntaxKind::FN).map(|node| node.clone())
209 }
210
211 fn resolve_path(&self, path: &ast::Path) -> Option<hir::PathResolution> {
212 // First try resolving the whole path. This will work for things like
213 // `std::collections::HashMap`, but will fail for things like
214 // `std::collections::HashMap::new`.
215 if let Some(resolution) = self.scope.speculative_resolve(&path) {
216 return Some(resolution);
217 }
218 // Resolution failed, try resolving the qualifier (e.g. `std::collections::HashMap` and if
219 // that succeeds, then iterate through the candidates on the resolved type with the provided
220 // name.
221 let resolved_qualifier = self.scope.speculative_resolve(&path.qualifier()?)?;
222 if let hir::PathResolution::Def(hir::ModuleDef::Adt(adt)) = resolved_qualifier {
223 let name = path.segment()?.name_ref()?;
224 adt.ty(self.scope.db).iterate_path_candidates(
225 self.scope.db,
226 self.scope.module()?.krate(),
227 &self.scope.traits_in_scope(),
228 None,
229 |_ty, assoc_item| {
230 let item_name = assoc_item.name(self.scope.db)?;
231 if item_name.to_string().as_str() == name.text().as_str() {
232 Some(hir::PathResolution::AssocItem(assoc_item))
233 } else {
234 None
235 }
236 },
237 )
238 } else {
239 None
240 }
241 }
242
243 fn qualifier_type(&self, path: &SyntaxNode) -> Option<hir::Type> {
244 use syntax::ast::AstNode;
245 if let Some(path) = ast::Path::cast(path.clone()) {
246 if let Some(qualifier) = path.qualifier() {
247 if let Some(resolved_qualifier) = self.resolve_path(&qualifier) {
248 if let hir::PathResolution::Def(hir::ModuleDef::Adt(adt)) = resolved_qualifier {
249 return Some(adt.ty(self.scope.db));
250 }
251 }
252 }
253 }
254 None
255 }
256}
257
258fn is_self(path: &ast::Path) -> bool {
259 path.segment().map(|segment| segment.self_token().is_some()).unwrap_or(false)
260}
261
262/// Returns a suitable node for resolving paths in the current scope. If we create a scope based on
263/// a statement node, then we can't resolve local variables that were defined in the current scope
264/// (only in parent scopes). So we find another node, ideally a child of the statement where local
265/// variable resolution is permitted.
266fn pick_node_for_resolution(node: SyntaxNode) -> SyntaxNode {
267 match node.kind() {
268 SyntaxKind::EXPR_STMT => {
269 if let Some(n) = node.first_child() {
270 mark::hit!(cursor_after_semicolon);
271 return n;
272 }
273 }
274 SyntaxKind::LET_STMT | SyntaxKind::IDENT_PAT => {
275 if let Some(next) = node.next_sibling() {
276 return pick_node_for_resolution(next);
277 }
278 }
279 SyntaxKind::NAME => {
280 if let Some(parent) = node.parent() {
281 return pick_node_for_resolution(parent);
282 }
283 }
284 _ => {}
285 }
286 node
287}
288
289/// Returns whether `path` or any of its qualifiers contains type arguments.
290fn path_contains_type_arguments(path: Option<ast::Path>) -> bool {
291 if let Some(path) = path {
292 if let Some(segment) = path.segment() {
293 if segment.generic_arg_list().is_some() {
294 mark::hit!(type_arguments_within_path);
295 return true;
296 }
297 }
298 return path_contains_type_arguments(path.qualifier());
299 }
300 false
301}
diff --git a/crates/ssr/src/search.rs b/crates/ssr/src/search.rs
new file mode 100644
index 000000000..8509cfa4d
--- /dev/null
+++ b/crates/ssr/src/search.rs
@@ -0,0 +1,282 @@
1//! Searching for matches.
2
3use crate::{
4 matching,
5 resolving::{ResolvedPath, ResolvedPattern, ResolvedRule},
6 Match, MatchFinder,
7};
8use base_db::{FileId, FileRange};
9use ide_db::{
10 defs::Definition,
11 search::{Reference, SearchScope},
12};
13use rustc_hash::FxHashSet;
14use syntax::{ast, AstNode, SyntaxKind, SyntaxNode};
15use test_utils::mark;
16
17/// A cache for the results of find_usages. This is for when we have multiple patterns that have the
18/// same path. e.g. if the pattern was `foo::Bar` that can parse as a path, an expression, a type
19/// and as a pattern. In each, the usages of `foo::Bar` are the same and we'd like to avoid finding
20/// them more than once.
21#[derive(Default)]
22pub(crate) struct UsageCache {
23 usages: Vec<(Definition, Vec<Reference>)>,
24}
25
26impl<'db> MatchFinder<'db> {
27 /// Adds all matches for `rule` to `matches_out`. Matches may overlap in ways that make
28 /// replacement impossible, so further processing is required in order to properly nest matches
29 /// and remove overlapping matches. This is done in the `nesting` module.
30 pub(crate) fn find_matches_for_rule(
31 &self,
32 rule: &ResolvedRule,
33 usage_cache: &mut UsageCache,
34 matches_out: &mut Vec<Match>,
35 ) {
36 if rule.pattern.contains_self {
37 // If the pattern contains `self` we restrict the scope of the search to just the
38 // current method. No other method can reference the same `self`. This makes the
39 // behavior of `self` consistent with other variables.
40 if let Some(current_function) = self.resolution_scope.current_function() {
41 self.slow_scan_node(&current_function, rule, &None, matches_out);
42 }
43 return;
44 }
45 if pick_path_for_usages(&rule.pattern).is_none() {
46 self.slow_scan(rule, matches_out);
47 return;
48 }
49 self.find_matches_for_pattern_tree(rule, &rule.pattern, usage_cache, matches_out);
50 }
51
52 fn find_matches_for_pattern_tree(
53 &self,
54 rule: &ResolvedRule,
55 pattern: &ResolvedPattern,
56 usage_cache: &mut UsageCache,
57 matches_out: &mut Vec<Match>,
58 ) {
59 if let Some(resolved_path) = pick_path_for_usages(pattern) {
60 let definition: Definition = resolved_path.resolution.clone().into();
61 for reference in self.find_usages(usage_cache, definition) {
62 if let Some(node_to_match) = self.find_node_to_match(resolved_path, reference) {
63 if !is_search_permitted_ancestors(&node_to_match) {
64 mark::hit!(use_declaration_with_braces);
65 continue;
66 }
67 self.try_add_match(rule, &node_to_match, &None, matches_out);
68 }
69 }
70 }
71 }
72
73 fn find_node_to_match(
74 &self,
75 resolved_path: &ResolvedPath,
76 reference: &Reference,
77 ) -> Option<SyntaxNode> {
78 let file = self.sema.parse(reference.file_range.file_id);
79 let depth = resolved_path.depth as usize;
80 let offset = reference.file_range.range.start();
81 if let Some(path) =
82 self.sema.find_node_at_offset_with_descend::<ast::Path>(file.syntax(), offset)
83 {
84 self.sema.ancestors_with_macros(path.syntax().clone()).skip(depth).next()
85 } else if let Some(path) =
86 self.sema.find_node_at_offset_with_descend::<ast::MethodCallExpr>(file.syntax(), offset)
87 {
88 // If the pattern contained a path and we found a reference to that path that wasn't
89 // itself a path, but was a method call, then we need to adjust how far up to try
90 // matching by how deep the path was within a CallExpr. The structure would have been
91 // CallExpr, PathExpr, Path - i.e. a depth offset of 2. We don't need to check if the
92 // path was part of a CallExpr because if it wasn't then all that will happen is we'll
93 // fail to match, which is the desired behavior.
94 const PATH_DEPTH_IN_CALL_EXPR: usize = 2;
95 if depth < PATH_DEPTH_IN_CALL_EXPR {
96 return None;
97 }
98 self.sema
99 .ancestors_with_macros(path.syntax().clone())
100 .skip(depth - PATH_DEPTH_IN_CALL_EXPR)
101 .next()
102 } else {
103 None
104 }
105 }
106
107 fn find_usages<'a>(
108 &self,
109 usage_cache: &'a mut UsageCache,
110 definition: Definition,
111 ) -> &'a [Reference] {
112 // Logically if a lookup succeeds we should just return it. Unfortunately returning it would
113 // extend the lifetime of the borrow, then we wouldn't be able to do the insertion on a
114 // cache miss. This is a limitation of NLL and is fixed with Polonius. For now we do two
115 // lookups in the case of a cache hit.
116 if usage_cache.find(&definition).is_none() {
117 let usages = definition.find_usages(&self.sema, Some(self.search_scope()));
118 usage_cache.usages.push((definition, usages));
119 return &usage_cache.usages.last().unwrap().1;
120 }
121 usage_cache.find(&definition).unwrap()
122 }
123
124 /// Returns the scope within which we want to search. We don't want un unrestricted search
125 /// scope, since we don't want to find references in external dependencies.
126 fn search_scope(&self) -> SearchScope {
127 // FIXME: We should ideally have a test that checks that we edit local roots and not library
128 // roots. This probably would require some changes to fixtures, since currently everything
129 // seems to get put into a single source root.
130 let mut files = Vec::new();
131 self.search_files_do(|file_id| {
132 files.push(file_id);
133 });
134 SearchScope::files(&files)
135 }
136
137 fn slow_scan(&self, rule: &ResolvedRule, matches_out: &mut Vec<Match>) {
138 self.search_files_do(|file_id| {
139 let file = self.sema.parse(file_id);
140 let code = file.syntax();
141 self.slow_scan_node(code, rule, &None, matches_out);
142 })
143 }
144
145 fn search_files_do(&self, mut callback: impl FnMut(FileId)) {
146 if self.restrict_ranges.is_empty() {
147 // Unrestricted search.
148 use base_db::SourceDatabaseExt;
149 use ide_db::symbol_index::SymbolsDatabase;
150 for &root in self.sema.db.local_roots().iter() {
151 let sr = self.sema.db.source_root(root);
152 for file_id in sr.iter() {
153 callback(file_id);
154 }
155 }
156 } else {
157 // Search is restricted, deduplicate file IDs (generally only one).
158 let mut files = FxHashSet::default();
159 for range in &self.restrict_ranges {
160 if files.insert(range.file_id) {
161 callback(range.file_id);
162 }
163 }
164 }
165 }
166
167 fn slow_scan_node(
168 &self,
169 code: &SyntaxNode,
170 rule: &ResolvedRule,
171 restrict_range: &Option<FileRange>,
172 matches_out: &mut Vec<Match>,
173 ) {
174 if !is_search_permitted(code) {
175 return;
176 }
177 self.try_add_match(rule, &code, restrict_range, matches_out);
178 // If we've got a macro call, we already tried matching it pre-expansion, which is the only
179 // way to match the whole macro, now try expanding it and matching the expansion.
180 if let Some(macro_call) = ast::MacroCall::cast(code.clone()) {
181 if let Some(expanded) = self.sema.expand(&macro_call) {
182 if let Some(tt) = macro_call.token_tree() {
183 // When matching within a macro expansion, we only want to allow matches of
184 // nodes that originated entirely from within the token tree of the macro call.
185 // i.e. we don't want to match something that came from the macro itself.
186 self.slow_scan_node(
187 &expanded,
188 rule,
189 &Some(self.sema.original_range(tt.syntax())),
190 matches_out,
191 );
192 }
193 }
194 }
195 for child in code.children() {
196 self.slow_scan_node(&child, rule, restrict_range, matches_out);
197 }
198 }
199
200 fn try_add_match(
201 &self,
202 rule: &ResolvedRule,
203 code: &SyntaxNode,
204 restrict_range: &Option<FileRange>,
205 matches_out: &mut Vec<Match>,
206 ) {
207 if !self.within_range_restrictions(code) {
208 mark::hit!(replace_nonpath_within_selection);
209 return;
210 }
211 if let Ok(m) = matching::get_match(false, rule, code, restrict_range, &self.sema) {
212 matches_out.push(m);
213 }
214 }
215
216 /// Returns whether `code` is within one of our range restrictions if we have any. No range
217 /// restrictions is considered unrestricted and always returns true.
218 fn within_range_restrictions(&self, code: &SyntaxNode) -> bool {
219 if self.restrict_ranges.is_empty() {
220 // There is no range restriction.
221 return true;
222 }
223 let node_range = self.sema.original_range(code);
224 for range in &self.restrict_ranges {
225 if range.file_id == node_range.file_id && range.range.contains_range(node_range.range) {
226 return true;
227 }
228 }
229 false
230 }
231}
232
233/// Returns whether we support matching within `node` and all of its ancestors.
234fn is_search_permitted_ancestors(node: &SyntaxNode) -> bool {
235 if let Some(parent) = node.parent() {
236 if !is_search_permitted_ancestors(&parent) {
237 return false;
238 }
239 }
240 is_search_permitted(node)
241}
242
243/// Returns whether we support matching within this kind of node.
244fn is_search_permitted(node: &SyntaxNode) -> bool {
245 // FIXME: Properly handle use declarations. At the moment, if our search pattern is `foo::bar`
246 // and the code is `use foo::{baz, bar}`, we'll match `bar`, since it resolves to `foo::bar`.
247 // However we'll then replace just the part we matched `bar`. We probably need to instead remove
248 // `bar` and insert a new use declaration.
249 node.kind() != SyntaxKind::USE
250}
251
252impl UsageCache {
253 fn find(&mut self, definition: &Definition) -> Option<&[Reference]> {
254 // We expect a very small number of cache entries (generally 1), so a linear scan should be
255 // fast enough and avoids the need to implement Hash for Definition.
256 for (d, refs) in &self.usages {
257 if d == definition {
258 return Some(refs);
259 }
260 }
261 None
262 }
263}
264
265/// Returns a path that's suitable for path resolution. We exclude builtin types, since they aren't
266/// something that we can find references to. We then somewhat arbitrarily pick the path that is the
267/// longest as this is hopefully more likely to be less common, making it faster to find.
268fn pick_path_for_usages(pattern: &ResolvedPattern) -> Option<&ResolvedPath> {
269 // FIXME: Take the scope of the resolved path into account. e.g. if there are any paths that are
270 // private to the current module, then we definitely would want to pick them over say a path
271 // from std. Possibly we should go further than this and intersect the search scopes for all
272 // resolved paths then search only in that scope.
273 pattern
274 .resolved_paths
275 .iter()
276 .filter(|(_, p)| {
277 !matches!(p.resolution, hir::PathResolution::Def(hir::ModuleDef::BuiltinType(_)))
278 })
279 .map(|(node, resolved)| (node.text().len(), resolved))
280 .max_by(|(a, _), (b, _)| a.cmp(b))
281 .map(|(_, resolved)| resolved)
282}
diff --git a/crates/ssr/src/tests.rs b/crates/ssr/src/tests.rs
new file mode 100644
index 000000000..e45c88864
--- /dev/null
+++ b/crates/ssr/src/tests.rs
@@ -0,0 +1,1281 @@
1use crate::{MatchFinder, SsrRule};
2use base_db::{salsa::Durability, FileId, FilePosition, FileRange, SourceDatabaseExt};
3use expect::{expect, Expect};
4use rustc_hash::FxHashSet;
5use std::sync::Arc;
6use test_utils::{mark, RangeOrOffset};
7
8fn parse_error_text(query: &str) -> String {
9 format!("{}", query.parse::<SsrRule>().unwrap_err())
10}
11
12#[test]
13fn parser_empty_query() {
14 assert_eq!(parse_error_text(""), "Parse error: Cannot find delimiter `==>>`");
15}
16
17#[test]
18fn parser_no_delimiter() {
19 assert_eq!(parse_error_text("foo()"), "Parse error: Cannot find delimiter `==>>`");
20}
21
22#[test]
23fn parser_two_delimiters() {
24 assert_eq!(
25 parse_error_text("foo() ==>> a ==>> b "),
26 "Parse error: More than one delimiter found"
27 );
28}
29
30#[test]
31fn parser_repeated_name() {
32 assert_eq!(
33 parse_error_text("foo($a, $a) ==>>"),
34 "Parse error: Placeholder `$a` repeats more than once"
35 );
36}
37
38#[test]
39fn parser_invalid_pattern() {
40 assert_eq!(
41 parse_error_text(" ==>> ()"),
42 "Parse error: Not a valid Rust expression, type, item, path or pattern"
43 );
44}
45
46#[test]
47fn parser_invalid_template() {
48 assert_eq!(
49 parse_error_text("() ==>> )"),
50 "Parse error: Not a valid Rust expression, type, item, path or pattern"
51 );
52}
53
54#[test]
55fn parser_undefined_placeholder_in_replacement() {
56 assert_eq!(
57 parse_error_text("42 ==>> $a"),
58 "Parse error: Replacement contains undefined placeholders: $a"
59 );
60}
61
62/// `code` may optionally contain a cursor marker `<|>`. If it doesn't, then the position will be
63/// the start of the file. If there's a second cursor marker, then we'll return a single range.
64pub(crate) fn single_file(code: &str) -> (ide_db::RootDatabase, FilePosition, Vec<FileRange>) {
65 use base_db::fixture::WithFixture;
66 use ide_db::symbol_index::SymbolsDatabase;
67 let (mut db, file_id, range_or_offset) = if code.contains(test_utils::CURSOR_MARKER) {
68 ide_db::RootDatabase::with_range_or_offset(code)
69 } else {
70 let (db, file_id) = ide_db::RootDatabase::with_single_file(code);
71 (db, file_id, RangeOrOffset::Offset(0.into()))
72 };
73 let selections;
74 let position;
75 match range_or_offset {
76 RangeOrOffset::Range(range) => {
77 position = FilePosition { file_id, offset: range.start() };
78 selections = vec![FileRange { file_id, range: range }];
79 }
80 RangeOrOffset::Offset(offset) => {
81 position = FilePosition { file_id, offset };
82 selections = vec![];
83 }
84 }
85 let mut local_roots = FxHashSet::default();
86 local_roots.insert(base_db::fixture::WORKSPACE);
87 db.set_local_roots_with_durability(Arc::new(local_roots), Durability::HIGH);
88 (db, position, selections)
89}
90
91fn assert_ssr_transform(rule: &str, input: &str, expected: Expect) {
92 assert_ssr_transforms(&[rule], input, expected);
93}
94
95fn assert_ssr_transforms(rules: &[&str], input: &str, expected: Expect) {
96 let (db, position, selections) = single_file(input);
97 let mut match_finder = MatchFinder::in_context(&db, position, selections);
98 for rule in rules {
99 let rule: SsrRule = rule.parse().unwrap();
100 match_finder.add_rule(rule).unwrap();
101 }
102 let edits = match_finder.edits();
103 if edits.is_empty() {
104 panic!("No edits were made");
105 }
106 assert_eq!(edits[0].file_id, position.file_id);
107 // Note, db.file_text is not necessarily the same as `input`, since fixture parsing alters
108 // stuff.
109 let mut actual = db.file_text(position.file_id).to_string();
110 edits[0].edit.apply(&mut actual);
111 expected.assert_eq(&actual);
112}
113
114fn print_match_debug_info(match_finder: &MatchFinder, file_id: FileId, snippet: &str) {
115 let debug_info = match_finder.debug_where_text_equal(file_id, snippet);
116 println!(
117 "Match debug info: {} nodes had text exactly equal to '{}'",
118 debug_info.len(),
119 snippet
120 );
121 for (index, d) in debug_info.iter().enumerate() {
122 println!("Node #{}\n{:#?}\n", index, d);
123 }
124}
125
126fn assert_matches(pattern: &str, code: &str, expected: &[&str]) {
127 let (db, position, selections) = single_file(code);
128 let mut match_finder = MatchFinder::in_context(&db, position, selections);
129 match_finder.add_search_pattern(pattern.parse().unwrap()).unwrap();
130 let matched_strings: Vec<String> =
131 match_finder.matches().flattened().matches.iter().map(|m| m.matched_text()).collect();
132 if matched_strings != expected && !expected.is_empty() {
133 print_match_debug_info(&match_finder, position.file_id, &expected[0]);
134 }
135 assert_eq!(matched_strings, expected);
136}
137
138fn assert_no_match(pattern: &str, code: &str) {
139 let (db, position, selections) = single_file(code);
140 let mut match_finder = MatchFinder::in_context(&db, position, selections);
141 match_finder.add_search_pattern(pattern.parse().unwrap()).unwrap();
142 let matches = match_finder.matches().flattened().matches;
143 if !matches.is_empty() {
144 print_match_debug_info(&match_finder, position.file_id, &matches[0].matched_text());
145 panic!("Got {} matches when we expected none: {:#?}", matches.len(), matches);
146 }
147}
148
149fn assert_match_failure_reason(pattern: &str, code: &str, snippet: &str, expected_reason: &str) {
150 let (db, position, selections) = single_file(code);
151 let mut match_finder = MatchFinder::in_context(&db, position, selections);
152 match_finder.add_search_pattern(pattern.parse().unwrap()).unwrap();
153 let mut reasons = Vec::new();
154 for d in match_finder.debug_where_text_equal(position.file_id, snippet) {
155 if let Some(reason) = d.match_failure_reason() {
156 reasons.push(reason.to_owned());
157 }
158 }
159 assert_eq!(reasons, vec![expected_reason]);
160}
161
162#[test]
163fn ssr_function_to_method() {
164 assert_ssr_transform(
165 "my_function($a, $b) ==>> ($a).my_method($b)",
166 "fn my_function() {} fn main() { loop { my_function( other_func(x, y), z + w) } }",
167 expect![["fn my_function() {} fn main() { loop { (other_func(x, y)).my_method(z + w) } }"]],
168 )
169}
170
171#[test]
172fn ssr_nested_function() {
173 assert_ssr_transform(
174 "foo($a, $b, $c) ==>> bar($c, baz($a, $b))",
175 r#"
176 //- /lib.rs crate:foo
177 fn foo() {}
178 fn bar() {}
179 fn baz() {}
180 fn main { foo (x + value.method(b), x+y-z, true && false) }
181 "#,
182 expect![[r#"
183 fn foo() {}
184 fn bar() {}
185 fn baz() {}
186 fn main { bar(true && false, baz(x + value.method(b), x+y-z)) }
187 "#]],
188 )
189}
190
191#[test]
192fn ssr_expected_spacing() {
193 assert_ssr_transform(
194 "foo($x) + bar() ==>> bar($x)",
195 "fn foo() {} fn bar() {} fn main() { foo(5) + bar() }",
196 expect![["fn foo() {} fn bar() {} fn main() { bar(5) }"]],
197 );
198}
199
200#[test]
201fn ssr_with_extra_space() {
202 assert_ssr_transform(
203 "foo($x ) + bar() ==>> bar($x)",
204 "fn foo() {} fn bar() {} fn main() { foo( 5 ) +bar( ) }",
205 expect![["fn foo() {} fn bar() {} fn main() { bar(5) }"]],
206 );
207}
208
209#[test]
210fn ssr_keeps_nested_comment() {
211 assert_ssr_transform(
212 "foo($x) ==>> bar($x)",
213 "fn foo() {} fn bar() {} fn main() { foo(other(5 /* using 5 */)) }",
214 expect![["fn foo() {} fn bar() {} fn main() { bar(other(5 /* using 5 */)) }"]],
215 )
216}
217
218#[test]
219fn ssr_keeps_comment() {
220 assert_ssr_transform(
221 "foo($x) ==>> bar($x)",
222 "fn foo() {} fn bar() {} fn main() { foo(5 /* using 5 */) }",
223 expect![["fn foo() {} fn bar() {} fn main() { bar(5)/* using 5 */ }"]],
224 )
225}
226
227#[test]
228fn ssr_struct_lit() {
229 assert_ssr_transform(
230 "Foo{a: $a, b: $b} ==>> Foo::new($a, $b)",
231 r#"
232 struct Foo() {}
233 impl Foo { fn new() {} }
234 fn main() { Foo{b:2, a:1} }
235 "#,
236 expect![[r#"
237 struct Foo() {}
238 impl Foo { fn new() {} }
239 fn main() { Foo::new(1, 2) }
240 "#]],
241 )
242}
243
244#[test]
245fn ignores_whitespace() {
246 assert_matches("1+2", "fn f() -> i32 {1 + 2}", &["1 + 2"]);
247 assert_matches("1 + 2", "fn f() -> i32 {1+2}", &["1+2"]);
248}
249
250#[test]
251fn no_match() {
252 assert_no_match("1 + 3", "fn f() -> i32 {1 + 2}");
253}
254
255#[test]
256fn match_fn_definition() {
257 assert_matches("fn $a($b: $t) {$c}", "fn f(a: i32) {bar()}", &["fn f(a: i32) {bar()}"]);
258}
259
260#[test]
261fn match_struct_definition() {
262 let code = r#"
263 struct Option<T> {}
264 struct Bar {}
265 struct Foo {name: Option<String>}"#;
266 assert_matches("struct $n {$f: Option<String>}", code, &["struct Foo {name: Option<String>}"]);
267}
268
269#[test]
270fn match_expr() {
271 let code = r#"
272 fn foo() {}
273 fn f() -> i32 {foo(40 + 2, 42)}"#;
274 assert_matches("foo($a, $b)", code, &["foo(40 + 2, 42)"]);
275 assert_no_match("foo($a, $b, $c)", code);
276 assert_no_match("foo($a)", code);
277}
278
279#[test]
280fn match_nested_method_calls() {
281 assert_matches(
282 "$a.z().z().z()",
283 "fn f() {h().i().j().z().z().z().d().e()}",
284 &["h().i().j().z().z().z()"],
285 );
286}
287
288// Make sure that our node matching semantics don't differ within macro calls.
289#[test]
290fn match_nested_method_calls_with_macro_call() {
291 assert_matches(
292 "$a.z().z().z()",
293 r#"
294 macro_rules! m1 { ($a:expr) => {$a}; }
295 fn f() {m1!(h().i().j().z().z().z().d().e())}"#,
296 &["h().i().j().z().z().z()"],
297 );
298}
299
300#[test]
301fn match_complex_expr() {
302 let code = r#"
303 fn foo() {} fn bar() {}
304 fn f() -> i32 {foo(bar(40, 2), 42)}"#;
305 assert_matches("foo($a, $b)", code, &["foo(bar(40, 2), 42)"]);
306 assert_no_match("foo($a, $b, $c)", code);
307 assert_no_match("foo($a)", code);
308 assert_matches("bar($a, $b)", code, &["bar(40, 2)"]);
309}
310
311// Trailing commas in the code should be ignored.
312#[test]
313fn match_with_trailing_commas() {
314 // Code has comma, pattern doesn't.
315 assert_matches("foo($a, $b)", "fn foo() {} fn f() {foo(1, 2,);}", &["foo(1, 2,)"]);
316 assert_matches("Foo{$a, $b}", "struct Foo {} fn f() {Foo{1, 2,};}", &["Foo{1, 2,}"]);
317
318 // Pattern has comma, code doesn't.
319 assert_matches("foo($a, $b,)", "fn foo() {} fn f() {foo(1, 2);}", &["foo(1, 2)"]);
320 assert_matches("Foo{$a, $b,}", "struct Foo {} fn f() {Foo{1, 2};}", &["Foo{1, 2}"]);
321}
322
323#[test]
324fn match_type() {
325 assert_matches("i32", "fn f() -> i32 {1 + 2}", &["i32"]);
326 assert_matches(
327 "Option<$a>",
328 "struct Option<T> {} fn f() -> Option<i32> {42}",
329 &["Option<i32>"],
330 );
331 assert_no_match(
332 "Option<$a>",
333 "struct Option<T> {} struct Result<T, E> {} fn f() -> Result<i32, ()> {42}",
334 );
335}
336
337#[test]
338fn match_struct_instantiation() {
339 let code = r#"
340 struct Foo {bar: i32, baz: i32}
341 fn f() {Foo {bar: 1, baz: 2}}"#;
342 assert_matches("Foo {bar: 1, baz: 2}", code, &["Foo {bar: 1, baz: 2}"]);
343 // Now with placeholders for all parts of the struct.
344 assert_matches("Foo {$a: $b, $c: $d}", code, &["Foo {bar: 1, baz: 2}"]);
345 assert_matches("Foo {}", "struct Foo {} fn f() {Foo {}}", &["Foo {}"]);
346}
347
348#[test]
349fn match_path() {
350 let code = r#"
351 mod foo {
352 pub fn bar() {}
353 }
354 fn f() {foo::bar(42)}"#;
355 assert_matches("foo::bar", code, &["foo::bar"]);
356 assert_matches("$a::bar", code, &["foo::bar"]);
357 assert_matches("foo::$b", code, &["foo::bar"]);
358}
359
360#[test]
361fn match_pattern() {
362 assert_matches("Some($a)", "struct Some(); fn f() {if let Some(x) = foo() {}}", &["Some(x)"]);
363}
364
365// If our pattern has a full path, e.g. a::b::c() and the code has c(), but c resolves to
366// a::b::c, then we should match.
367#[test]
368fn match_fully_qualified_fn_path() {
369 let code = r#"
370 mod a {
371 pub mod b {
372 pub fn c(_: i32) {}
373 }
374 }
375 use a::b::c;
376 fn f1() {
377 c(42);
378 }
379 "#;
380 assert_matches("a::b::c($a)", code, &["c(42)"]);
381}
382
383#[test]
384fn match_resolved_type_name() {
385 let code = r#"
386 mod m1 {
387 pub mod m2 {
388 pub trait Foo<T> {}
389 }
390 }
391 mod m3 {
392 trait Foo<T> {}
393 fn f1(f: Option<&dyn Foo<bool>>) {}
394 }
395 mod m4 {
396 use crate::m1::m2::Foo;
397 fn f1(f: Option<&dyn Foo<i32>>) {}
398 }
399 "#;
400 assert_matches("m1::m2::Foo<$t>", code, &["Foo<i32>"]);
401}
402
403#[test]
404fn type_arguments_within_path() {
405 mark::check!(type_arguments_within_path);
406 let code = r#"
407 mod foo {
408 pub struct Bar<T> {t: T}
409 impl<T> Bar<T> {
410 pub fn baz() {}
411 }
412 }
413 fn f1() {foo::Bar::<i32>::baz();}
414 "#;
415 assert_no_match("foo::Bar::<i64>::baz()", code);
416 assert_matches("foo::Bar::<i32>::baz()", code, &["foo::Bar::<i32>::baz()"]);
417}
418
419#[test]
420fn literal_constraint() {
421 mark::check!(literal_constraint);
422 let code = r#"
423 enum Option<T> { Some(T), None }
424 use Option::Some;
425 fn f1() {
426 let x1 = Some(42);
427 let x2 = Some("foo");
428 let x3 = Some(x1);
429 let x4 = Some(40 + 2);
430 let x5 = Some(true);
431 }
432 "#;
433 assert_matches("Some(${a:kind(literal)})", code, &["Some(42)", "Some(\"foo\")", "Some(true)"]);
434 assert_matches("Some(${a:not(kind(literal))})", code, &["Some(x1)", "Some(40 + 2)"]);
435}
436
437#[test]
438fn match_reordered_struct_instantiation() {
439 assert_matches(
440 "Foo {aa: 1, b: 2, ccc: 3}",
441 "struct Foo {} fn f() {Foo {b: 2, ccc: 3, aa: 1}}",
442 &["Foo {b: 2, ccc: 3, aa: 1}"],
443 );
444 assert_no_match("Foo {a: 1}", "struct Foo {} fn f() {Foo {b: 1}}");
445 assert_no_match("Foo {a: 1}", "struct Foo {} fn f() {Foo {a: 2}}");
446 assert_no_match("Foo {a: 1, b: 2}", "struct Foo {} fn f() {Foo {a: 1}}");
447 assert_no_match("Foo {a: 1, b: 2}", "struct Foo {} fn f() {Foo {b: 2}}");
448 assert_no_match("Foo {a: 1, }", "struct Foo {} fn f() {Foo {a: 1, b: 2}}");
449 assert_no_match("Foo {a: 1, z: 9}", "struct Foo {} fn f() {Foo {a: 1}}");
450}
451
452#[test]
453fn match_macro_invocation() {
454 assert_matches(
455 "foo!($a)",
456 "macro_rules! foo {() => {}} fn() {foo(foo!(foo()))}",
457 &["foo!(foo())"],
458 );
459 assert_matches(
460 "foo!(41, $a, 43)",
461 "macro_rules! foo {() => {}} fn() {foo!(41, 42, 43)}",
462 &["foo!(41, 42, 43)"],
463 );
464 assert_no_match("foo!(50, $a, 43)", "macro_rules! foo {() => {}} fn() {foo!(41, 42, 43}");
465 assert_no_match("foo!(41, $a, 50)", "macro_rules! foo {() => {}} fn() {foo!(41, 42, 43}");
466 assert_matches(
467 "foo!($a())",
468 "macro_rules! foo {() => {}} fn() {foo!(bar())}",
469 &["foo!(bar())"],
470 );
471}
472
473// When matching within a macro expansion, we only allow matches of nodes that originated from
474// the macro call, not from the macro definition.
475#[test]
476fn no_match_expression_from_macro() {
477 assert_no_match(
478 "$a.clone()",
479 r#"
480 macro_rules! m1 {
481 () => {42.clone()}
482 }
483 fn f1() {m1!()}
484 "#,
485 );
486}
487
488// We definitely don't want to allow matching of an expression that part originates from the
489// macro call `42` and part from the macro definition `.clone()`.
490#[test]
491fn no_match_split_expression() {
492 assert_no_match(
493 "$a.clone()",
494 r#"
495 macro_rules! m1 {
496 ($x:expr) => {$x.clone()}
497 }
498 fn f1() {m1!(42)}
499 "#,
500 );
501}
502
503#[test]
504fn replace_function_call() {
505 // This test also makes sure that we ignore empty-ranges.
506 assert_ssr_transform(
507 "foo() ==>> bar()",
508 "fn foo() {<|><|>} fn bar() {} fn f1() {foo(); foo();}",
509 expect![["fn foo() {} fn bar() {} fn f1() {bar(); bar();}"]],
510 );
511}
512
513#[test]
514fn replace_function_call_with_placeholders() {
515 assert_ssr_transform(
516 "foo($a, $b) ==>> bar($b, $a)",
517 "fn foo() {} fn bar() {} fn f1() {foo(5, 42)}",
518 expect![["fn foo() {} fn bar() {} fn f1() {bar(42, 5)}"]],
519 );
520}
521
522#[test]
523fn replace_nested_function_calls() {
524 assert_ssr_transform(
525 "foo($a) ==>> bar($a)",
526 "fn foo() {} fn bar() {} fn f1() {foo(foo(42))}",
527 expect![["fn foo() {} fn bar() {} fn f1() {bar(bar(42))}"]],
528 );
529}
530
531#[test]
532fn replace_associated_function_call() {
533 assert_ssr_transform(
534 "Foo::new() ==>> Bar::new()",
535 r#"
536 struct Foo {}
537 impl Foo { fn new() {} }
538 struct Bar {}
539 impl Bar { fn new() {} }
540 fn f1() {Foo::new();}
541 "#,
542 expect![[r#"
543 struct Foo {}
544 impl Foo { fn new() {} }
545 struct Bar {}
546 impl Bar { fn new() {} }
547 fn f1() {Bar::new();}
548 "#]],
549 );
550}
551
552#[test]
553fn replace_associated_trait_default_function_call() {
554 mark::check!(replace_associated_trait_default_function_call);
555 assert_ssr_transform(
556 "Bar2::foo() ==>> Bar2::foo2()",
557 r#"
558 trait Foo { fn foo() {} }
559 pub struct Bar {}
560 impl Foo for Bar {}
561 pub struct Bar2 {}
562 impl Foo for Bar2 {}
563 impl Bar2 { fn foo2() {} }
564 fn main() {
565 Bar::foo();
566 Bar2::foo();
567 }
568 "#,
569 expect![[r#"
570 trait Foo { fn foo() {} }
571 pub struct Bar {}
572 impl Foo for Bar {}
573 pub struct Bar2 {}
574 impl Foo for Bar2 {}
575 impl Bar2 { fn foo2() {} }
576 fn main() {
577 Bar::foo();
578 Bar2::foo2();
579 }
580 "#]],
581 );
582}
583
584#[test]
585fn replace_associated_trait_constant() {
586 mark::check!(replace_associated_trait_constant);
587 assert_ssr_transform(
588 "Bar2::VALUE ==>> Bar2::VALUE_2222",
589 r#"
590 trait Foo { const VALUE: i32; const VALUE_2222: i32; }
591 pub struct Bar {}
592 impl Foo for Bar { const VALUE: i32 = 1; const VALUE_2222: i32 = 2; }
593 pub struct Bar2 {}
594 impl Foo for Bar2 { const VALUE: i32 = 1; const VALUE_2222: i32 = 2; }
595 impl Bar2 { fn foo2() {} }
596 fn main() {
597 Bar::VALUE;
598 Bar2::VALUE;
599 }
600 "#,
601 expect![[r#"
602 trait Foo { const VALUE: i32; const VALUE_2222: i32; }
603 pub struct Bar {}
604 impl Foo for Bar { const VALUE: i32 = 1; const VALUE_2222: i32 = 2; }
605 pub struct Bar2 {}
606 impl Foo for Bar2 { const VALUE: i32 = 1; const VALUE_2222: i32 = 2; }
607 impl Bar2 { fn foo2() {} }
608 fn main() {
609 Bar::VALUE;
610 Bar2::VALUE_2222;
611 }
612 "#]],
613 );
614}
615
616#[test]
617fn replace_path_in_different_contexts() {
618 // Note the <|> inside module a::b which marks the point where the rule is interpreted. We
619 // replace foo with bar, but both need different path qualifiers in different contexts. In f4,
620 // foo is unqualified because of a use statement, however the replacement needs to be fully
621 // qualified.
622 assert_ssr_transform(
623 "c::foo() ==>> c::bar()",
624 r#"
625 mod a {
626 pub mod b {<|>
627 pub mod c {
628 pub fn foo() {}
629 pub fn bar() {}
630 fn f1() { foo() }
631 }
632 fn f2() { c::foo() }
633 }
634 fn f3() { b::c::foo() }
635 }
636 use a::b::c::foo;
637 fn f4() { foo() }
638 "#,
639 expect![[r#"
640 mod a {
641 pub mod b {
642 pub mod c {
643 pub fn foo() {}
644 pub fn bar() {}
645 fn f1() { bar() }
646 }
647 fn f2() { c::bar() }
648 }
649 fn f3() { b::c::bar() }
650 }
651 use a::b::c::foo;
652 fn f4() { a::b::c::bar() }
653 "#]],
654 );
655}
656
657#[test]
658fn replace_associated_function_with_generics() {
659 assert_ssr_transform(
660 "c::Foo::<$a>::new() ==>> d::Bar::<$a>::default()",
661 r#"
662 mod c {
663 pub struct Foo<T> {v: T}
664 impl<T> Foo<T> { pub fn new() {} }
665 fn f1() {
666 Foo::<i32>::new();
667 }
668 }
669 mod d {
670 pub struct Bar<T> {v: T}
671 impl<T> Bar<T> { pub fn default() {} }
672 fn f1() {
673 super::c::Foo::<i32>::new();
674 }
675 }
676 "#,
677 expect![[r#"
678 mod c {
679 pub struct Foo<T> {v: T}
680 impl<T> Foo<T> { pub fn new() {} }
681 fn f1() {
682 crate::d::Bar::<i32>::default();
683 }
684 }
685 mod d {
686 pub struct Bar<T> {v: T}
687 impl<T> Bar<T> { pub fn default() {} }
688 fn f1() {
689 Bar::<i32>::default();
690 }
691 }
692 "#]],
693 );
694}
695
696#[test]
697fn replace_type() {
698 assert_ssr_transform(
699 "Result<(), $a> ==>> Option<$a>",
700 "struct Result<T, E> {} struct Option<T> {} fn f1() -> Result<(), Vec<Error>> {foo()}",
701 expect![[
702 "struct Result<T, E> {} struct Option<T> {} fn f1() -> Option<Vec<Error>> {foo()}"
703 ]],
704 );
705}
706
707#[test]
708fn replace_macro_invocations() {
709 assert_ssr_transform(
710 "try!($a) ==>> $a?",
711 "macro_rules! try {() => {}} fn f1() -> Result<(), E> {bar(try!(foo()));}",
712 expect![["macro_rules! try {() => {}} fn f1() -> Result<(), E> {bar(foo()?);}"]],
713 );
714 assert_ssr_transform(
715 "foo!($a($b)) ==>> foo($b, $a)",
716 "macro_rules! foo {() => {}} fn f1() {foo!(abc(def() + 2));}",
717 expect![["macro_rules! foo {() => {}} fn f1() {foo(def() + 2, abc);}"]],
718 );
719}
720
721#[test]
722fn replace_binary_op() {
723 assert_ssr_transform(
724 "$a + $b ==>> $b + $a",
725 "fn f() {2 * 3 + 4 * 5}",
726 expect![["fn f() {4 * 5 + 2 * 3}"]],
727 );
728 assert_ssr_transform(
729 "$a + $b ==>> $b + $a",
730 "fn f() {1 + 2 + 3 + 4}",
731 expect![[r#"fn f() {4 + (3 + (2 + 1))}"#]],
732 );
733}
734
735#[test]
736fn match_binary_op() {
737 assert_matches("$a + $b", "fn f() {1 + 2 + 3 + 4}", &["1 + 2", "1 + 2 + 3", "1 + 2 + 3 + 4"]);
738}
739
740#[test]
741fn multiple_rules() {
742 assert_ssr_transforms(
743 &["$a + 1 ==>> add_one($a)", "$a + $b ==>> add($a, $b)"],
744 "fn add() {} fn add_one() {} fn f() -> i32 {3 + 2 + 1}",
745 expect![["fn add() {} fn add_one() {} fn f() -> i32 {add_one(add(3, 2))}"]],
746 )
747}
748
749#[test]
750fn multiple_rules_with_nested_matches() {
751 assert_ssr_transforms(
752 &["foo1($a) ==>> bar1($a)", "foo2($a) ==>> bar2($a)"],
753 r#"
754 fn foo1() {} fn foo2() {} fn bar1() {} fn bar2() {}
755 fn f() {foo1(foo2(foo1(foo2(foo1(42)))))}
756 "#,
757 expect![[r#"
758 fn foo1() {} fn foo2() {} fn bar1() {} fn bar2() {}
759 fn f() {bar1(bar2(bar1(bar2(bar1(42)))))}
760 "#]],
761 )
762}
763
764#[test]
765fn match_within_macro_invocation() {
766 let code = r#"
767 macro_rules! foo {
768 ($a:stmt; $b:expr) => {
769 $b
770 };
771 }
772 struct A {}
773 impl A {
774 fn bar() {}
775 }
776 fn f1() {
777 let aaa = A {};
778 foo!(macro_ignores_this(); aaa.bar());
779 }
780 "#;
781 assert_matches("$a.bar()", code, &["aaa.bar()"]);
782}
783
784#[test]
785fn replace_within_macro_expansion() {
786 assert_ssr_transform(
787 "$a.foo() ==>> bar($a)",
788 r#"
789 macro_rules! macro1 {
790 ($a:expr) => {$a}
791 }
792 fn bar() {}
793 fn f() {macro1!(5.x().foo().o2())}
794 "#,
795 expect![[r#"
796 macro_rules! macro1 {
797 ($a:expr) => {$a}
798 }
799 fn bar() {}
800 fn f() {macro1!(bar(5.x()).o2())}
801 "#]],
802 )
803}
804
805#[test]
806fn replace_outside_and_within_macro_expansion() {
807 assert_ssr_transform(
808 "foo($a) ==>> bar($a)",
809 r#"
810 fn foo() {} fn bar() {}
811 macro_rules! macro1 {
812 ($a:expr) => {$a}
813 }
814 fn f() {foo(foo(macro1!(foo(foo(42)))))}
815 "#,
816 expect![[r#"
817 fn foo() {} fn bar() {}
818 macro_rules! macro1 {
819 ($a:expr) => {$a}
820 }
821 fn f() {bar(bar(macro1!(bar(bar(42)))))}
822 "#]],
823 )
824}
825
826#[test]
827fn preserves_whitespace_within_macro_expansion() {
828 assert_ssr_transform(
829 "$a + $b ==>> $b - $a",
830 r#"
831 macro_rules! macro1 {
832 ($a:expr) => {$a}
833 }
834 fn f() {macro1!(1 * 2 + 3 + 4}
835 "#,
836 expect![[r#"
837 macro_rules! macro1 {
838 ($a:expr) => {$a}
839 }
840 fn f() {macro1!(4 - (3 - 1 * 2)}
841 "#]],
842 )
843}
844
845#[test]
846fn add_parenthesis_when_necessary() {
847 assert_ssr_transform(
848 "foo($a) ==>> $a.to_string()",
849 r#"
850 fn foo(_: i32) {}
851 fn bar3(v: i32) {
852 foo(1 + 2);
853 foo(-v);
854 }
855 "#,
856 expect![[r#"
857 fn foo(_: i32) {}
858 fn bar3(v: i32) {
859 (1 + 2).to_string();
860 (-v).to_string();
861 }
862 "#]],
863 )
864}
865
866#[test]
867fn match_failure_reasons() {
868 let code = r#"
869 fn bar() {}
870 macro_rules! foo {
871 ($a:expr) => {
872 1 + $a + 2
873 };
874 }
875 fn f1() {
876 bar(1, 2);
877 foo!(5 + 43.to_string() + 5);
878 }
879 "#;
880 assert_match_failure_reason(
881 "bar($a, 3)",
882 code,
883 "bar(1, 2)",
884 r#"Pattern wanted token '3' (INT_NUMBER), but code had token '2' (INT_NUMBER)"#,
885 );
886 assert_match_failure_reason(
887 "42.to_string()",
888 code,
889 "43.to_string()",
890 r#"Pattern wanted token '42' (INT_NUMBER), but code had token '43' (INT_NUMBER)"#,
891 );
892}
893
894#[test]
895fn overlapping_possible_matches() {
896 // There are three possible matches here, however the middle one, `foo(foo(foo(42)))` shouldn't
897 // match because it overlaps with the outer match. The inner match is permitted since it's is
898 // contained entirely within the placeholder of the outer match.
899 assert_matches(
900 "foo(foo($a))",
901 "fn foo() {} fn main() {foo(foo(foo(foo(42))))}",
902 &["foo(foo(42))", "foo(foo(foo(foo(42))))"],
903 );
904}
905
906#[test]
907fn use_declaration_with_braces() {
908 // It would be OK for a path rule to match and alter a use declaration. We shouldn't mess it up
909 // though. In particular, we must not change `use foo::{baz, bar}` to `use foo::{baz,
910 // foo2::bar2}`.
911 mark::check!(use_declaration_with_braces);
912 assert_ssr_transform(
913 "foo::bar ==>> foo2::bar2",
914 r#"
915 mod foo { pub fn bar() {} pub fn baz() {} }
916 mod foo2 { pub fn bar2() {} }
917 use foo::{baz, bar};
918 fn main() { bar() }
919 "#,
920 expect![["
921 mod foo { pub fn bar() {} pub fn baz() {} }
922 mod foo2 { pub fn bar2() {} }
923 use foo::{baz, bar};
924 fn main() { foo2::bar2() }
925 "]],
926 )
927}
928
929#[test]
930fn ufcs_matches_method_call() {
931 let code = r#"
932 struct Foo {}
933 impl Foo {
934 fn new(_: i32) -> Foo { Foo {} }
935 fn do_stuff(&self, _: i32) {}
936 }
937 struct Bar {}
938 impl Bar {
939 fn new(_: i32) -> Bar { Bar {} }
940 fn do_stuff(&self, v: i32) {}
941 }
942 fn main() {
943 let b = Bar {};
944 let f = Foo {};
945 b.do_stuff(1);
946 f.do_stuff(2);
947 Foo::new(4).do_stuff(3);
948 // Too many / too few args - should never match
949 f.do_stuff(2, 10);
950 f.do_stuff();
951 }
952 "#;
953 assert_matches("Foo::do_stuff($a, $b)", code, &["f.do_stuff(2)", "Foo::new(4).do_stuff(3)"]);
954 // The arguments needs special handling in the case of a function call matching a method call
955 // and the first argument is different.
956 assert_matches("Foo::do_stuff($a, 2)", code, &["f.do_stuff(2)"]);
957 assert_matches("Foo::do_stuff(Foo::new(4), $b)", code, &["Foo::new(4).do_stuff(3)"]);
958
959 assert_ssr_transform(
960 "Foo::do_stuff(Foo::new($a), $b) ==>> Bar::new($b).do_stuff($a)",
961 code,
962 expect![[r#"
963 struct Foo {}
964 impl Foo {
965 fn new(_: i32) -> Foo { Foo {} }
966 fn do_stuff(&self, _: i32) {}
967 }
968 struct Bar {}
969 impl Bar {
970 fn new(_: i32) -> Bar { Bar {} }
971 fn do_stuff(&self, v: i32) {}
972 }
973 fn main() {
974 let b = Bar {};
975 let f = Foo {};
976 b.do_stuff(1);
977 f.do_stuff(2);
978 Bar::new(3).do_stuff(4);
979 // Too many / too few args - should never match
980 f.do_stuff(2, 10);
981 f.do_stuff();
982 }
983 "#]],
984 );
985}
986
987#[test]
988fn pattern_is_a_single_segment_path() {
989 mark::check!(pattern_is_a_single_segment_path);
990 // The first function should not be altered because the `foo` in scope at the cursor position is
991 // a different `foo`. This case is special because "foo" can be parsed as a pattern (IDENT_PAT ->
992 // NAME -> IDENT), which contains no path. If we're not careful we'll end up matching the `foo`
993 // in `let foo` from the first function. Whether we should match the `let foo` in the second
994 // function is less clear. At the moment, we don't. Doing so sounds like a rename operation,
995 // which isn't really what SSR is for, especially since the replacement `bar` must be able to be
996 // resolved, which means if we rename `foo` we'll get a name collision.
997 assert_ssr_transform(
998 "foo ==>> bar",
999 r#"
1000 fn f1() -> i32 {
1001 let foo = 1;
1002 let bar = 2;
1003 foo
1004 }
1005 fn f1() -> i32 {
1006 let foo = 1;
1007 let bar = 2;
1008 foo<|>
1009 }
1010 "#,
1011 expect![[r#"
1012 fn f1() -> i32 {
1013 let foo = 1;
1014 let bar = 2;
1015 foo
1016 }
1017 fn f1() -> i32 {
1018 let foo = 1;
1019 let bar = 2;
1020 bar
1021 }
1022 "#]],
1023 );
1024}
1025
1026#[test]
1027fn replace_local_variable_reference() {
1028 // The pattern references a local variable `foo` in the block containing the cursor. We should
1029 // only replace references to this variable `foo`, not other variables that just happen to have
1030 // the same name.
1031 mark::check!(cursor_after_semicolon);
1032 assert_ssr_transform(
1033 "foo + $a ==>> $a - foo",
1034 r#"
1035 fn bar1() -> i32 {
1036 let mut res = 0;
1037 let foo = 5;
1038 res += foo + 1;
1039 let foo = 10;
1040 res += foo + 2;<|>
1041 res += foo + 3;
1042 let foo = 15;
1043 res += foo + 4;
1044 res
1045 }
1046 "#,
1047 expect![[r#"
1048 fn bar1() -> i32 {
1049 let mut res = 0;
1050 let foo = 5;
1051 res += foo + 1;
1052 let foo = 10;
1053 res += 2 - foo;
1054 res += 3 - foo;
1055 let foo = 15;
1056 res += foo + 4;
1057 res
1058 }
1059 "#]],
1060 )
1061}
1062
1063#[test]
1064fn replace_path_within_selection() {
1065 assert_ssr_transform(
1066 "foo ==>> bar",
1067 r#"
1068 fn main() {
1069 let foo = 41;
1070 let bar = 42;
1071 do_stuff(foo);
1072 do_stuff(foo);<|>
1073 do_stuff(foo);
1074 do_stuff(foo);<|>
1075 do_stuff(foo);
1076 }"#,
1077 expect![[r#"
1078 fn main() {
1079 let foo = 41;
1080 let bar = 42;
1081 do_stuff(foo);
1082 do_stuff(foo);
1083 do_stuff(bar);
1084 do_stuff(bar);
1085 do_stuff(foo);
1086 }"#]],
1087 );
1088}
1089
1090#[test]
1091fn replace_nonpath_within_selection() {
1092 mark::check!(replace_nonpath_within_selection);
1093 assert_ssr_transform(
1094 "$a + $b ==>> $b * $a",
1095 r#"
1096 fn main() {
1097 let v = 1 + 2;<|>
1098 let v2 = 3 + 3;
1099 let v3 = 4 + 5;<|>
1100 let v4 = 6 + 7;
1101 }"#,
1102 expect![[r#"
1103 fn main() {
1104 let v = 1 + 2;
1105 let v2 = 3 * 3;
1106 let v3 = 5 * 4;
1107 let v4 = 6 + 7;
1108 }"#]],
1109 );
1110}
1111
1112#[test]
1113fn replace_self() {
1114 // `foo(self)` occurs twice in the code, however only the first occurrence is the `self` that's
1115 // in scope where the rule is invoked.
1116 assert_ssr_transform(
1117 "foo(self) ==>> bar(self)",
1118 r#"
1119 struct S1 {}
1120 fn foo(_: &S1) {}
1121 fn bar(_: &S1) {}
1122 impl S1 {
1123 fn f1(&self) {
1124 foo(self)<|>
1125 }
1126 fn f2(&self) {
1127 foo(self)
1128 }
1129 }
1130 "#,
1131 expect![[r#"
1132 struct S1 {}
1133 fn foo(_: &S1) {}
1134 fn bar(_: &S1) {}
1135 impl S1 {
1136 fn f1(&self) {
1137 bar(self)
1138 }
1139 fn f2(&self) {
1140 foo(self)
1141 }
1142 }
1143 "#]],
1144 );
1145}
1146
1147#[test]
1148fn match_trait_method_call() {
1149 // `Bar::foo` and `Bar2::foo` resolve to the same function. Make sure we only match if the type
1150 // matches what's in the pattern. Also checks that we handle autoderef.
1151 let code = r#"
1152 pub struct Bar {}
1153 pub struct Bar2 {}
1154 pub trait Foo {
1155 fn foo(&self, _: i32) {}
1156 }
1157 impl Foo for Bar {}
1158 impl Foo for Bar2 {}
1159 fn main() {
1160 let v1 = Bar {};
1161 let v2 = Bar2 {};
1162 let v1_ref = &v1;
1163 let v2_ref = &v2;
1164 v1.foo(1);
1165 v2.foo(2);
1166 Bar::foo(&v1, 3);
1167 Bar2::foo(&v2, 4);
1168 v1_ref.foo(5);
1169 v2_ref.foo(6);
1170 }
1171 "#;
1172 assert_matches("Bar::foo($a, $b)", code, &["v1.foo(1)", "Bar::foo(&v1, 3)", "v1_ref.foo(5)"]);
1173 assert_matches("Bar2::foo($a, $b)", code, &["v2.foo(2)", "Bar2::foo(&v2, 4)", "v2_ref.foo(6)"]);
1174}
1175
1176#[test]
1177fn replace_autoref_autoderef_capture() {
1178 // Here we have several calls to `$a.foo()`. In the first case autoref is applied, in the
1179 // second, we already have a reference, so it isn't. When $a is used in a context where autoref
1180 // doesn't apply, we need to prefix it with `&`. Finally, we have some cases where autoderef
1181 // needs to be applied.
1182 mark::check!(replace_autoref_autoderef_capture);
1183 let code = r#"
1184 struct Foo {}
1185 impl Foo {
1186 fn foo(&self) {}
1187 fn foo2(&self) {}
1188 }
1189 fn bar(_: &Foo) {}
1190 fn main() {
1191 let f = Foo {};
1192 let fr = &f;
1193 let fr2 = &fr;
1194 let fr3 = &fr2;
1195 f.foo();
1196 fr.foo();
1197 fr2.foo();
1198 fr3.foo();
1199 }
1200 "#;
1201 assert_ssr_transform(
1202 "Foo::foo($a) ==>> bar($a)",
1203 code,
1204 expect![[r#"
1205 struct Foo {}
1206 impl Foo {
1207 fn foo(&self) {}
1208 fn foo2(&self) {}
1209 }
1210 fn bar(_: &Foo) {}
1211 fn main() {
1212 let f = Foo {};
1213 let fr = &f;
1214 let fr2 = &fr;
1215 let fr3 = &fr2;
1216 bar(&f);
1217 bar(&*fr);
1218 bar(&**fr2);
1219 bar(&***fr3);
1220 }
1221 "#]],
1222 );
1223 // If the placeholder is used as the receiver of another method call, then we don't need to
1224 // explicitly autoderef or autoref.
1225 assert_ssr_transform(
1226 "Foo::foo($a) ==>> $a.foo2()",
1227 code,
1228 expect![[r#"
1229 struct Foo {}
1230 impl Foo {
1231 fn foo(&self) {}
1232 fn foo2(&self) {}
1233 }
1234 fn bar(_: &Foo) {}
1235 fn main() {
1236 let f = Foo {};
1237 let fr = &f;
1238 let fr2 = &fr;
1239 let fr3 = &fr2;
1240 f.foo2();
1241 fr.foo2();
1242 fr2.foo2();
1243 fr3.foo2();
1244 }
1245 "#]],
1246 );
1247}
1248
1249#[test]
1250fn replace_autoref_mut() {
1251 let code = r#"
1252 struct Foo {}
1253 impl Foo {
1254 fn foo(&mut self) {}
1255 }
1256 fn bar(_: &mut Foo) {}
1257 fn main() {
1258 let mut f = Foo {};
1259 f.foo();
1260 let fr = &mut f;
1261 fr.foo();
1262 }
1263 "#;
1264 assert_ssr_transform(
1265 "Foo::foo($a) ==>> bar($a)",
1266 code,
1267 expect![[r#"
1268 struct Foo {}
1269 impl Foo {
1270 fn foo(&mut self) {}
1271 }
1272 fn bar(_: &mut Foo) {}
1273 fn main() {
1274 let mut f = Foo {};
1275 bar(&mut f);
1276 let fr = &mut f;
1277 bar(&mut *fr);
1278 }
1279 "#]],
1280 );
1281}