diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2020-07-24 13:46:55 +0100 |
---|---|---|
committer | GitHub <[email protected]> | 2020-07-24 13:46:55 +0100 |
commit | c3defe2532ba6ffd12a13bcbc8fdeda037665efc (patch) | |
tree | 831bf4dd44ec83d927face4ba17e57dcdeab7fbe /crates/ra_ssr/src/nester.rs | |
parent | 0e5095d3cac11d4b569c6e1594bd07937556c812 (diff) | |
parent | 58680cb08ea535e1fb567416fa3466a744a01b99 (diff) |
Merge #5518
5518: Use resolved paths in SSR rules r=matklad a=davidlattimore
The main user-visible changes are:
* SSR now matches paths based on whether they resolve to the same thing instead of whether they're written the same.
* So `foo()` won't match `foo()` if it's a different function `foo()`, but will match `bar::foo()` if it's the same `foo`.
* Paths in the replacement will now be rendered with appropriate qualification for their context.
* For example `foo::Bar` will render as just `Bar` inside the module `foo`, but might render as `baz::foo::Bar` from elsewhere.
* This means that all paths in the search pattern and replacement template must be able to be resolved.
* It now also matters where you invoke SSR from, since paths are resolved relative to wherever that is.
* Search now uses find-uses on paths to locate places to try matching. This means that when a path is present in the pattern, search will generally be pretty fast.
* Function calls can now match method calls again, but this time only if they resolve to the same function.
Co-authored-by: David Lattimore <[email protected]>
Diffstat (limited to 'crates/ra_ssr/src/nester.rs')
-rw-r--r-- | crates/ra_ssr/src/nester.rs | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/crates/ra_ssr/src/nester.rs b/crates/ra_ssr/src/nester.rs new file mode 100644 index 000000000..b3e20579b --- /dev/null +++ b/crates/ra_ssr/src/nester.rs | |||
@@ -0,0 +1,98 @@ | |||
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 | |||
10 | use crate::{Match, SsrMatches}; | ||
11 | use ra_syntax::SyntaxNode; | ||
12 | use rustc_hash::FxHashMap; | ||
13 | |||
14 | pub(crate) fn nest_and_remove_collisions( | ||
15 | mut matches: Vec<Match>, | ||
16 | sema: &hir::Semantics<ra_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)] | ||
31 | struct MatchCollector { | ||
32 | matches_by_node: FxHashMap<SyntaxNode, Match>, | ||
33 | } | ||
34 | |||
35 | impl 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<ra_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`. | ||
56 | fn try_add_sub_match( | ||
57 | m: Match, | ||
58 | existing: &mut Match, | ||
59 | sema: &hir::Semantics<ra_ide_db::RootDatabase>, | ||
60 | ) { | ||
61 | for p in existing.placeholder_values.values_mut() { | ||
62 | // Note, no need to check if p.range.file is equal to m.range.file, since we | ||
63 | // already know we're within `existing`. | ||
64 | if p.range.range.contains_range(m.range.range) { | ||
65 | // Convert the inner matches in `p` into a temporary MatchCollector. When | ||
66 | // we're done, we then convert it back into an SsrMatches. If we expected | ||
67 | // lots of inner matches, it might be worthwhile keeping a MatchCollector | ||
68 | // around for each placeholder match. However we expect most placeholder | ||
69 | // will have 0 and a few will have 1. More than that should hopefully be | ||
70 | // exceptional. | ||
71 | let mut collector = MatchCollector::default(); | ||
72 | for m in std::mem::replace(&mut p.inner_matches.matches, Vec::new()) { | ||
73 | collector.matches_by_node.insert(m.matched_node.clone(), m); | ||
74 | } | ||
75 | collector.add_match(m, sema); | ||
76 | p.inner_matches = collector.into(); | ||
77 | break; | ||
78 | } | ||
79 | } | ||
80 | } | ||
81 | |||
82 | impl From<MatchCollector> for SsrMatches { | ||
83 | fn from(mut match_collector: MatchCollector) -> Self { | ||
84 | let mut matches = SsrMatches::default(); | ||
85 | for (_, m) in match_collector.matches_by_node.drain() { | ||
86 | matches.matches.push(m); | ||
87 | } | ||
88 | matches.matches.sort_by(|a, b| { | ||
89 | // Order matches by file_id then by start range. This should be sufficient since ranges | ||
90 | // shouldn't be overlapping. | ||
91 | a.range | ||
92 | .file_id | ||
93 | .cmp(&b.range.file_id) | ||
94 | .then_with(|| a.range.range.start().cmp(&b.range.range.start())) | ||
95 | }); | ||
96 | matches | ||
97 | } | ||
98 | } | ||