diff options
Diffstat (limited to 'crates')
-rw-r--r-- | crates/ra_assists/src/handlers/auto_import.rs | 12 | ||||
-rw-r--r-- | crates/ra_ide_db/src/imports_locator.rs | 88 |
2 files changed, 48 insertions, 52 deletions
diff --git a/crates/ra_assists/src/handlers/auto_import.rs b/crates/ra_assists/src/handlers/auto_import.rs index e8060a491..7b6499a08 100644 --- a/crates/ra_assists/src/handlers/auto_import.rs +++ b/crates/ra_assists/src/handlers/auto_import.rs | |||
@@ -5,7 +5,7 @@ use hir::{ | |||
5 | AsAssocItem, AssocItemContainer, ModPath, Module, ModuleDef, PathResolution, Semantics, Trait, | 5 | AsAssocItem, AssocItemContainer, ModPath, Module, ModuleDef, PathResolution, Semantics, Trait, |
6 | Type, | 6 | Type, |
7 | }; | 7 | }; |
8 | use ra_ide_db::{imports_locator::ImportsLocator, RootDatabase}; | 8 | use ra_ide_db::{imports_locator, RootDatabase}; |
9 | use ra_prof::profile; | 9 | use ra_prof::profile; |
10 | use ra_syntax::{ | 10 | use ra_syntax::{ |
11 | ast::{self, AstNode}, | 11 | ast::{self, AstNode}, |
@@ -35,8 +35,8 @@ use crate::{utils::insert_use_statement, AssistContext, AssistId, Assists, Group | |||
35 | // # pub mod std { pub mod collections { pub struct HashMap { } } } | 35 | // # pub mod std { pub mod collections { pub struct HashMap { } } } |
36 | // ``` | 36 | // ``` |
37 | pub(crate) fn auto_import(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { | 37 | pub(crate) fn auto_import(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { |
38 | let auto_import_assets = AutoImportAssets::new(&ctx)?; | 38 | let auto_import_assets = AutoImportAssets::new(ctx)?; |
39 | let proposed_imports = auto_import_assets.search_for_imports(ctx.db()); | 39 | let proposed_imports = auto_import_assets.search_for_imports(ctx); |
40 | if proposed_imports.is_empty() { | 40 | if proposed_imports.is_empty() { |
41 | return None; | 41 | return None; |
42 | } | 42 | } |
@@ -127,11 +127,11 @@ impl AutoImportAssets { | |||
127 | GroupLabel(name) | 127 | GroupLabel(name) |
128 | } | 128 | } |
129 | 129 | ||
130 | fn search_for_imports(&self, db: &RootDatabase) -> BTreeSet<ModPath> { | 130 | fn search_for_imports(&self, ctx: &AssistContext) -> BTreeSet<ModPath> { |
131 | let _p = profile("auto_import::search_for_imports"); | 131 | let _p = profile("auto_import::search_for_imports"); |
132 | let db = ctx.db(); | ||
132 | let current_crate = self.module_with_name_to_import.krate(); | 133 | let current_crate = self.module_with_name_to_import.krate(); |
133 | ImportsLocator::new(db, current_crate) | 134 | imports_locator::find_imports(&ctx.sema, current_crate, &self.get_search_query()) |
134 | .find_imports(&self.get_search_query()) | ||
135 | .into_iter() | 135 | .into_iter() |
136 | .filter_map(|candidate| match &self.import_candidate { | 136 | .filter_map(|candidate| match &self.import_candidate { |
137 | ImportCandidate::TraitAssocItem(assoc_item_type, _) => { | 137 | ImportCandidate::TraitAssocItem(assoc_item_type, _) => { |
diff --git a/crates/ra_ide_db/src/imports_locator.rs b/crates/ra_ide_db/src/imports_locator.rs index fff112e66..1fba71ff8 100644 --- a/crates/ra_ide_db/src/imports_locator.rs +++ b/crates/ra_ide_db/src/imports_locator.rs | |||
@@ -13,57 +13,53 @@ use crate::{ | |||
13 | use either::Either; | 13 | use either::Either; |
14 | use rustc_hash::FxHashSet; | 14 | use rustc_hash::FxHashSet; |
15 | 15 | ||
16 | pub struct ImportsLocator<'a> { | 16 | pub fn find_imports<'a>( |
17 | sema: Semantics<'a, RootDatabase>, | 17 | sema: &Semantics<'a, RootDatabase>, |
18 | krate: Crate, | 18 | krate: Crate, |
19 | } | 19 | name_to_import: &str, |
20 | 20 | ) -> Vec<Either<ModuleDef, MacroDef>> { | |
21 | impl<'a> ImportsLocator<'a> { | 21 | let _p = profile("search_for_imports"); |
22 | pub fn new(db: &'a RootDatabase, krate: Crate) -> Self { | 22 | let db = sema.db; |
23 | Self { sema: Semantics::new(db), krate } | ||
24 | } | ||
25 | 23 | ||
26 | pub fn find_imports(&mut self, name_to_import: &str) -> Vec<Either<ModuleDef, MacroDef>> { | 24 | // Query dependencies first. |
27 | let _p = profile("search_for_imports"); | 25 | let mut candidates: FxHashSet<_> = |
28 | let db = self.sema.db; | 26 | krate.query_external_importables(db, name_to_import).collect(); |
29 | 27 | ||
30 | // Query dependencies first. | 28 | // Query the local crate using the symbol index. |
31 | let mut candidates: FxHashSet<_> = | 29 | let local_results = { |
32 | self.krate.query_external_importables(db, name_to_import).collect(); | 30 | let mut query = Query::new(name_to_import.to_string()); |
31 | query.exact(); | ||
32 | query.limit(40); | ||
33 | symbol_index::crate_symbols(db, krate.into(), query) | ||
34 | }; | ||
33 | 35 | ||
34 | // Query the local crate using the symbol index. | 36 | candidates.extend( |
35 | let local_results = { | 37 | local_results |
36 | let mut query = Query::new(name_to_import.to_string()); | 38 | .into_iter() |
37 | query.exact(); | 39 | .filter_map(|import_candidate| get_name_definition(sema, &import_candidate)) |
38 | query.limit(40); | 40 | .filter_map(|name_definition_to_import| match name_definition_to_import { |
39 | symbol_index::crate_symbols(db, self.krate.into(), query) | 41 | Definition::ModuleDef(module_def) => Some(Either::Left(module_def)), |
40 | }; | 42 | Definition::Macro(macro_def) => Some(Either::Right(macro_def)), |
43 | _ => None, | ||
44 | }), | ||
45 | ); | ||
41 | 46 | ||
42 | candidates.extend( | 47 | candidates.into_iter().collect() |
43 | local_results | 48 | } |
44 | .into_iter() | ||
45 | .filter_map(|import_candidate| self.get_name_definition(&import_candidate)) | ||
46 | .filter_map(|name_definition_to_import| match name_definition_to_import { | ||
47 | Definition::ModuleDef(module_def) => Some(Either::Left(module_def)), | ||
48 | Definition::Macro(macro_def) => Some(Either::Right(macro_def)), | ||
49 | _ => None, | ||
50 | }), | ||
51 | ); | ||
52 | |||
53 | candidates.into_iter().collect() | ||
54 | } | ||
55 | 49 | ||
56 | fn get_name_definition(&mut self, import_candidate: &FileSymbol) -> Option<Definition> { | 50 | fn get_name_definition<'a>( |
57 | let _p = profile("get_name_definition"); | 51 | sema: &Semantics<'a, RootDatabase>, |
58 | let file_id = import_candidate.file_id; | 52 | import_candidate: &FileSymbol, |
53 | ) -> Option<Definition> { | ||
54 | let _p = profile("get_name_definition"); | ||
55 | let file_id = import_candidate.file_id; | ||
59 | 56 | ||
60 | let candidate_node = import_candidate.ptr.to_node(self.sema.parse(file_id).syntax()); | 57 | let candidate_node = import_candidate.ptr.to_node(sema.parse(file_id).syntax()); |
61 | let candidate_name_node = if candidate_node.kind() != NAME { | 58 | let candidate_name_node = if candidate_node.kind() != NAME { |
62 | candidate_node.children().find(|it| it.kind() == NAME)? | 59 | candidate_node.children().find(|it| it.kind() == NAME)? |
63 | } else { | 60 | } else { |
64 | candidate_node | 61 | candidate_node |
65 | }; | 62 | }; |
66 | let name = ast::Name::cast(candidate_name_node)?; | 63 | let name = ast::Name::cast(candidate_name_node)?; |
67 | classify_name(&self.sema, &name)?.into_definition() | 64 | classify_name(sema, &name)?.into_definition() |
68 | } | ||
69 | } | 65 | } |