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