aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ssr/src/search.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_ssr/src/search.rs')
-rw-r--r--crates/ra_ssr/src/search.rs273
1 files changed, 273 insertions, 0 deletions
diff --git a/crates/ra_ssr/src/search.rs b/crates/ra_ssr/src/search.rs
new file mode 100644
index 000000000..213dc494f
--- /dev/null
+++ b/crates/ra_ssr/src/search.rs
@@ -0,0 +1,273 @@
1//! Searching for matches.
2
3use crate::{
4 matching,
5 resolving::{ResolvedPath, ResolvedPattern, ResolvedRule},
6 Match, MatchFinder,
7};
8use ra_db::{FileId, FileRange};
9use ra_ide_db::{
10 defs::Definition,
11 search::{Reference, SearchScope},
12};
13use ra_syntax::{ast, AstNode, SyntaxKind, SyntaxNode};
14use rustc_hash::FxHashSet;
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 pick_path_for_usages(&rule.pattern).is_none() {
37 self.slow_scan(rule, matches_out);
38 return;
39 }
40 self.find_matches_for_pattern_tree(rule, &rule.pattern, usage_cache, matches_out);
41 }
42
43 fn find_matches_for_pattern_tree(
44 &self,
45 rule: &ResolvedRule,
46 pattern: &ResolvedPattern,
47 usage_cache: &mut UsageCache,
48 matches_out: &mut Vec<Match>,
49 ) {
50 if let Some(resolved_path) = pick_path_for_usages(pattern) {
51 let definition: Definition = resolved_path.resolution.clone().into();
52 for reference in self.find_usages(usage_cache, definition) {
53 if let Some(node_to_match) = self.find_node_to_match(resolved_path, reference) {
54 if !is_search_permitted_ancestors(&node_to_match) {
55 mark::hit!(use_declaration_with_braces);
56 continue;
57 }
58 self.try_add_match(rule, &node_to_match, &None, matches_out);
59 }
60 }
61 }
62 }
63
64 fn find_node_to_match(
65 &self,
66 resolved_path: &ResolvedPath,
67 reference: &Reference,
68 ) -> Option<SyntaxNode> {
69 let file = self.sema.parse(reference.file_range.file_id);
70 let depth = resolved_path.depth as usize;
71 let offset = reference.file_range.range.start();
72 if let Some(path) =
73 self.sema.find_node_at_offset_with_descend::<ast::Path>(file.syntax(), offset)
74 {
75 self.sema.ancestors_with_macros(path.syntax().clone()).skip(depth).next()
76 } else if let Some(path) =
77 self.sema.find_node_at_offset_with_descend::<ast::MethodCallExpr>(file.syntax(), offset)
78 {
79 // If the pattern contained a path and we found a reference to that path that wasn't
80 // itself a path, but was a method call, then we need to adjust how far up to try
81 // matching by how deep the path was within a CallExpr. The structure would have been
82 // CallExpr, PathExpr, Path - i.e. a depth offset of 2. We don't need to check if the
83 // path was part of a CallExpr because if it wasn't then all that will happen is we'll
84 // fail to match, which is the desired behavior.
85 const PATH_DEPTH_IN_CALL_EXPR: usize = 2;
86 if depth < PATH_DEPTH_IN_CALL_EXPR {
87 return None;
88 }
89 self.sema
90 .ancestors_with_macros(path.syntax().clone())
91 .skip(depth - PATH_DEPTH_IN_CALL_EXPR)
92 .next()
93 } else {
94 None
95 }
96 }
97
98 fn find_usages<'a>(
99 &self,
100 usage_cache: &'a mut UsageCache,
101 definition: Definition,
102 ) -> &'a [Reference] {
103 // Logically if a lookup succeeds we should just return it. Unfortunately returning it would
104 // extend the lifetime of the borrow, then we wouldn't be able to do the insertion on a
105 // cache miss. This is a limitation of NLL and is fixed with Polonius. For now we do two
106 // lookups in the case of a cache hit.
107 if usage_cache.find(&definition).is_none() {
108 let usages = definition.find_usages(&self.sema, Some(self.search_scope()));
109 usage_cache.usages.push((definition, usages));
110 return &usage_cache.usages.last().unwrap().1;
111 }
112 usage_cache.find(&definition).unwrap()
113 }
114
115 /// Returns the scope within which we want to search. We don't want un unrestricted search
116 /// scope, since we don't want to find references in external dependencies.
117 fn search_scope(&self) -> SearchScope {
118 // FIXME: We should ideally have a test that checks that we edit local roots and not library
119 // roots. This probably would require some changes to fixtures, since currently everything
120 // seems to get put into a single source root.
121 let mut files = Vec::new();
122 self.search_files_do(|file_id| {
123 files.push(file_id);
124 });
125 SearchScope::files(&files)
126 }
127
128 fn slow_scan(&self, rule: &ResolvedRule, matches_out: &mut Vec<Match>) {
129 self.search_files_do(|file_id| {
130 let file = self.sema.parse(file_id);
131 let code = file.syntax();
132 self.slow_scan_node(code, rule, &None, matches_out);
133 })
134 }
135
136 fn search_files_do(&self, mut callback: impl FnMut(FileId)) {
137 if self.restrict_ranges.is_empty() {
138 // Unrestricted search.
139 use ra_db::SourceDatabaseExt;
140 use ra_ide_db::symbol_index::SymbolsDatabase;
141 for &root in self.sema.db.local_roots().iter() {
142 let sr = self.sema.db.source_root(root);
143 for file_id in sr.iter() {
144 callback(file_id);
145 }
146 }
147 } else {
148 // Search is restricted, deduplicate file IDs (generally only one).
149 let mut files = FxHashSet::default();
150 for range in &self.restrict_ranges {
151 if files.insert(range.file_id) {
152 callback(range.file_id);
153 }
154 }
155 }
156 }
157
158 fn slow_scan_node(
159 &self,
160 code: &SyntaxNode,
161 rule: &ResolvedRule,
162 restrict_range: &Option<FileRange>,
163 matches_out: &mut Vec<Match>,
164 ) {
165 if !is_search_permitted(code) {
166 return;
167 }
168 self.try_add_match(rule, &code, restrict_range, matches_out);
169 // If we've got a macro call, we already tried matching it pre-expansion, which is the only
170 // way to match the whole macro, now try expanding it and matching the expansion.
171 if let Some(macro_call) = ast::MacroCall::cast(code.clone()) {
172 if let Some(expanded) = self.sema.expand(&macro_call) {
173 if let Some(tt) = macro_call.token_tree() {
174 // When matching within a macro expansion, we only want to allow matches of
175 // nodes that originated entirely from within the token tree of the macro call.
176 // i.e. we don't want to match something that came from the macro itself.
177 self.slow_scan_node(
178 &expanded,
179 rule,
180 &Some(self.sema.original_range(tt.syntax())),
181 matches_out,
182 );
183 }
184 }
185 }
186 for child in code.children() {
187 self.slow_scan_node(&child, rule, restrict_range, matches_out);
188 }
189 }
190
191 fn try_add_match(
192 &self,
193 rule: &ResolvedRule,
194 code: &SyntaxNode,
195 restrict_range: &Option<FileRange>,
196 matches_out: &mut Vec<Match>,
197 ) {
198 if !self.within_range_restrictions(code) {
199 mark::hit!(replace_nonpath_within_selection);
200 return;
201 }
202 if let Ok(m) = matching::get_match(false, rule, code, restrict_range, &self.sema) {
203 matches_out.push(m);
204 }
205 }
206
207 /// Returns whether `code` is within one of our range restrictions if we have any. No range
208 /// restrictions is considered unrestricted and always returns true.
209 fn within_range_restrictions(&self, code: &SyntaxNode) -> bool {
210 if self.restrict_ranges.is_empty() {
211 // There is no range restriction.
212 return true;
213 }
214 let node_range = self.sema.original_range(code);
215 for range in &self.restrict_ranges {
216 if range.file_id == node_range.file_id && range.range.contains_range(node_range.range) {
217 return true;
218 }
219 }
220 false
221 }
222}
223
224/// Returns whether we support matching within `node` and all of its ancestors.
225fn is_search_permitted_ancestors(node: &SyntaxNode) -> bool {
226 if let Some(parent) = node.parent() {
227 if !is_search_permitted_ancestors(&parent) {
228 return false;
229 }
230 }
231 is_search_permitted(node)
232}
233
234/// Returns whether we support matching within this kind of node.
235fn is_search_permitted(node: &SyntaxNode) -> bool {
236 // FIXME: Properly handle use declarations. At the moment, if our search pattern is `foo::bar`
237 // and the code is `use foo::{baz, bar}`, we'll match `bar`, since it resolves to `foo::bar`.
238 // However we'll then replace just the part we matched `bar`. We probably need to instead remove
239 // `bar` and insert a new use declaration.
240 node.kind() != SyntaxKind::USE
241}
242
243impl UsageCache {
244 fn find(&mut self, definition: &Definition) -> Option<&[Reference]> {
245 // We expect a very small number of cache entries (generally 1), so a linear scan should be
246 // fast enough and avoids the need to implement Hash for Definition.
247 for (d, refs) in &self.usages {
248 if d == definition {
249 return Some(refs);
250 }
251 }
252 None
253 }
254}
255
256/// Returns a path that's suitable for path resolution. We exclude builtin types, since they aren't
257/// something that we can find references to. We then somewhat arbitrarily pick the path that is the
258/// longest as this is hopefully more likely to be less common, making it faster to find.
259fn pick_path_for_usages(pattern: &ResolvedPattern) -> Option<&ResolvedPath> {
260 // FIXME: Take the scope of the resolved path into account. e.g. if there are any paths that are
261 // private to the current module, then we definitely would want to pick them over say a path
262 // from std. Possibly we should go further than this and intersect the search scopes for all
263 // resolved paths then search only in that scope.
264 pattern
265 .resolved_paths
266 .iter()
267 .filter(|(_, p)| {
268 !matches!(p.resolution, hir::PathResolution::Def(hir::ModuleDef::BuiltinType(_)))
269 })
270 .map(|(node, resolved)| (node.text().len(), resolved))
271 .max_by(|(a, _), (b, _)| a.cmp(b))
272 .map(|(_, resolved)| resolved)
273}