diff options
author | Lukas Wirth <[email protected]> | 2021-01-11 23:05:07 +0000 |
---|---|---|
committer | Lukas Wirth <[email protected]> | 2021-01-12 00:03:04 +0000 |
commit | fbdb32adfc49e0d69b7fd8e44135bea59382d2cb (patch) | |
tree | 5cffb6d3f0a0a168bfa48d7adcd6fa233e30b3db /crates/ssr/src | |
parent | 52fa926f005890f07dffc789c84c2be57a6bdccc (diff) |
Group references by FileId
Diffstat (limited to 'crates/ssr/src')
-rw-r--r-- | crates/ssr/src/search.rs | 24 |
1 files changed, 14 insertions, 10 deletions
diff --git a/crates/ssr/src/search.rs b/crates/ssr/src/search.rs index 44b5db029..a1d653aff 100644 --- a/crates/ssr/src/search.rs +++ b/crates/ssr/src/search.rs | |||
@@ -5,10 +5,10 @@ use crate::{ | |||
5 | resolving::{ResolvedPath, ResolvedPattern, ResolvedRule}, | 5 | resolving::{ResolvedPath, ResolvedPattern, ResolvedRule}, |
6 | Match, MatchFinder, | 6 | Match, MatchFinder, |
7 | }; | 7 | }; |
8 | use ide_db::base_db::{FileId, FileRange}; | ||
9 | use ide_db::{ | 8 | use ide_db::{ |
9 | base_db::{FileId, FileRange}, | ||
10 | defs::Definition, | 10 | defs::Definition, |
11 | search::{Reference, SearchScope}, | 11 | search::{FileReferences, SearchScope}, |
12 | }; | 12 | }; |
13 | use rustc_hash::FxHashSet; | 13 | use rustc_hash::FxHashSet; |
14 | use syntax::{ast, AstNode, SyntaxKind, SyntaxNode}; | 14 | use syntax::{ast, AstNode, SyntaxKind, SyntaxNode}; |
@@ -20,7 +20,7 @@ use test_utils::mark; | |||
20 | /// them more than once. | 20 | /// them more than once. |
21 | #[derive(Default)] | 21 | #[derive(Default)] |
22 | pub(crate) struct UsageCache { | 22 | pub(crate) struct UsageCache { |
23 | usages: Vec<(Definition, Vec<Reference>)>, | 23 | usages: Vec<(Definition, Vec<FileReferences>)>, |
24 | } | 24 | } |
25 | 25 | ||
26 | impl<'db> MatchFinder<'db> { | 26 | impl<'db> MatchFinder<'db> { |
@@ -58,8 +58,12 @@ impl<'db> MatchFinder<'db> { | |||
58 | ) { | 58 | ) { |
59 | if let Some(resolved_path) = pick_path_for_usages(pattern) { | 59 | if let Some(resolved_path) = pick_path_for_usages(pattern) { |
60 | let definition: Definition = resolved_path.resolution.clone().into(); | 60 | let definition: Definition = resolved_path.resolution.clone().into(); |
61 | for reference in self.find_usages(usage_cache, definition) { | 61 | for file_range in self |
62 | if let Some(node_to_match) = self.find_node_to_match(resolved_path, reference) { | 62 | .find_usages(usage_cache, definition) |
63 | .iter() | ||
64 | .flat_map(FileReferences::file_ranges) | ||
65 | { | ||
66 | if let Some(node_to_match) = self.find_node_to_match(resolved_path, file_range) { | ||
63 | if !is_search_permitted_ancestors(&node_to_match) { | 67 | if !is_search_permitted_ancestors(&node_to_match) { |
64 | mark::hit!(use_declaration_with_braces); | 68 | mark::hit!(use_declaration_with_braces); |
65 | continue; | 69 | continue; |
@@ -73,11 +77,11 @@ impl<'db> MatchFinder<'db> { | |||
73 | fn find_node_to_match( | 77 | fn find_node_to_match( |
74 | &self, | 78 | &self, |
75 | resolved_path: &ResolvedPath, | 79 | resolved_path: &ResolvedPath, |
76 | reference: &Reference, | 80 | file_range: FileRange, |
77 | ) -> Option<SyntaxNode> { | 81 | ) -> Option<SyntaxNode> { |
78 | let file = self.sema.parse(reference.file_range.file_id); | 82 | let file = self.sema.parse(file_range.file_id); |
79 | let depth = resolved_path.depth as usize; | 83 | let depth = resolved_path.depth as usize; |
80 | let offset = reference.file_range.range.start(); | 84 | let offset = file_range.range.start(); |
81 | if let Some(path) = | 85 | if let Some(path) = |
82 | self.sema.find_node_at_offset_with_descend::<ast::Path>(file.syntax(), offset) | 86 | self.sema.find_node_at_offset_with_descend::<ast::Path>(file.syntax(), offset) |
83 | { | 87 | { |
@@ -108,7 +112,7 @@ impl<'db> MatchFinder<'db> { | |||
108 | &self, | 112 | &self, |
109 | usage_cache: &'a mut UsageCache, | 113 | usage_cache: &'a mut UsageCache, |
110 | definition: Definition, | 114 | definition: Definition, |
111 | ) -> &'a [Reference] { | 115 | ) -> &'a [FileReferences] { |
112 | // Logically if a lookup succeeds we should just return it. Unfortunately returning it would | 116 | // 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 | 117 | // 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 | 118 | // cache miss. This is a limitation of NLL and is fixed with Polonius. For now we do two |
@@ -250,7 +254,7 @@ fn is_search_permitted(node: &SyntaxNode) -> bool { | |||
250 | } | 254 | } |
251 | 255 | ||
252 | impl UsageCache { | 256 | impl UsageCache { |
253 | fn find(&mut self, definition: &Definition) -> Option<&[Reference]> { | 257 | fn find(&mut self, definition: &Definition) -> Option<&[FileReferences]> { |
254 | // We expect a very small number of cache entries (generally 1), so a linear scan should be | 258 | // 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. | 259 | // fast enough and avoids the need to implement Hash for Definition. |
256 | for (d, refs) in &self.usages { | 260 | for (d, refs) in &self.usages { |