diff options
author | Kirill Bulatov <[email protected]> | 2020-03-23 11:34:56 +0000 |
---|---|---|
committer | Kirill Bulatov <[email protected]> | 2020-03-24 08:43:22 +0000 |
commit | 944f28fe5bf2b8e4316cc67bf5f824333fc4f180 (patch) | |
tree | 8deb8e01871b17968611832b795abcd1c3fd24e8 /crates/ra_assists/src/handlers/auto_import.rs | |
parent | d5e11b33a36755b139367e1f91a52f5ec27193f6 (diff) |
Use more generic public api
Diffstat (limited to 'crates/ra_assists/src/handlers/auto_import.rs')
-rw-r--r-- | crates/ra_assists/src/handlers/auto_import.rs | 44 |
1 files changed, 23 insertions, 21 deletions
diff --git a/crates/ra_assists/src/handlers/auto_import.rs b/crates/ra_assists/src/handlers/auto_import.rs index 49d8c4c3d..443eeaaf0 100644 --- a/crates/ra_assists/src/handlers/auto_import.rs +++ b/crates/ra_assists/src/handlers/auto_import.rs | |||
@@ -4,7 +4,7 @@ use hir::{ | |||
4 | AsAssocItem, AssocItemContainer, ModPath, Module, ModuleDef, PathResolution, Semantics, Trait, | 4 | AsAssocItem, AssocItemContainer, ModPath, Module, ModuleDef, PathResolution, Semantics, Trait, |
5 | Type, | 5 | Type, |
6 | }; | 6 | }; |
7 | use ra_ide_db::{defs::Definition, imports_locator::ImportsLocator, RootDatabase}; | 7 | use ra_ide_db::{imports_locator::ImportsLocator, RootDatabase}; |
8 | use ra_prof::profile; | 8 | use ra_prof::profile; |
9 | use ra_syntax::{ | 9 | use ra_syntax::{ |
10 | ast::{self, AstNode}, | 10 | ast::{self, AstNode}, |
@@ -17,6 +17,7 @@ use crate::{ | |||
17 | utils::insert_use_statement, | 17 | utils::insert_use_statement, |
18 | AssistId, | 18 | AssistId, |
19 | }; | 19 | }; |
20 | use either::Either; | ||
20 | 21 | ||
21 | // Assist: auto_import | 22 | // Assist: auto_import |
22 | // | 23 | // |
@@ -127,16 +128,14 @@ impl AutoImportAssets { | |||
127 | ImportsLocator::new(db) | 128 | ImportsLocator::new(db) |
128 | .find_imports(&self.get_search_query()) | 129 | .find_imports(&self.get_search_query()) |
129 | .into_iter() | 130 | .into_iter() |
130 | .filter_map(|definition| match &self.import_candidate { | 131 | .filter_map(|candidate| match &self.import_candidate { |
131 | ImportCandidate::TraitAssocItem(assoc_item_type, _) => { | 132 | ImportCandidate::TraitAssocItem(assoc_item_type, _) => { |
132 | let located_assoc_item = match definition { | 133 | let located_assoc_item = match candidate { |
133 | Definition::ModuleDef(ModuleDef::Function(located_function)) => { | 134 | Either::Left(ModuleDef::Function(located_function)) => located_function |
134 | located_function | 135 | .as_assoc_item(db) |
135 | .as_assoc_item(db) | 136 | .map(|assoc| assoc.container(db)) |
136 | .map(|assoc| assoc.container(db)) | 137 | .and_then(Self::assoc_to_trait), |
137 | .and_then(Self::assoc_to_trait) | 138 | Either::Left(ModuleDef::Const(located_const)) => located_const |
138 | } | ||
139 | Definition::ModuleDef(ModuleDef::Const(located_const)) => located_const | ||
140 | .as_assoc_item(db) | 139 | .as_assoc_item(db) |
141 | .map(|assoc| assoc.container(db)) | 140 | .map(|assoc| assoc.container(db)) |
142 | .and_then(Self::assoc_to_trait), | 141 | .and_then(Self::assoc_to_trait), |
@@ -154,13 +153,12 @@ impl AutoImportAssets { | |||
154 | None, | 153 | None, |
155 | |_, assoc| Self::assoc_to_trait(assoc.container(db)), | 154 | |_, assoc| Self::assoc_to_trait(assoc.container(db)), |
156 | ) | 155 | ) |
157 | .map(|located_trait| ModuleDef::from(located_trait).into()) | 156 | .map(ModuleDef::from) |
157 | .map(Either::Left) | ||
158 | } | 158 | } |
159 | ImportCandidate::TraitMethod(function_callee, _) => { | 159 | ImportCandidate::TraitMethod(function_callee, _) => { |
160 | let located_assoc_item = | 160 | let located_assoc_item = |
161 | if let Definition::ModuleDef(ModuleDef::Function(located_function)) = | 161 | if let Either::Left(ModuleDef::Function(located_function)) = candidate { |
162 | definition | ||
163 | { | ||
164 | located_function | 162 | located_function |
165 | .as_assoc_item(db) | 163 | .as_assoc_item(db) |
166 | .map(|assoc| assoc.container(db)) | 164 | .map(|assoc| assoc.container(db)) |
@@ -182,15 +180,19 @@ impl AutoImportAssets { | |||
182 | Self::assoc_to_trait(function.as_assoc_item(db)?.container(db)) | 180 | Self::assoc_to_trait(function.as_assoc_item(db)?.container(db)) |
183 | }, | 181 | }, |
184 | ) | 182 | ) |
185 | .map(|located_trait| ModuleDef::from(located_trait).into()) | 183 | .map(ModuleDef::from) |
184 | .map(Either::Left) | ||
185 | } | ||
186 | _ => Some(candidate), | ||
187 | }) | ||
188 | .filter_map(|candidate| match candidate { | ||
189 | Either::Left(module_def) => { | ||
190 | self.module_with_name_to_import.find_use_path(db, module_def) | ||
191 | } | ||
192 | Either::Right(macro_def) => { | ||
193 | self.module_with_name_to_import.find_use_path(db, macro_def) | ||
186 | } | 194 | } |
187 | _ => match definition { | ||
188 | Definition::ModuleDef(module_def) => Some(module_def.into()), | ||
189 | Definition::Macro(macro_def) => Some(macro_def.into()), | ||
190 | _ => None, | ||
191 | }, | ||
192 | }) | 195 | }) |
193 | .filter_map(|item| self.module_with_name_to_import.find_use_path(db, item)) | ||
194 | .filter(|use_path| !use_path.segments.is_empty()) | 196 | .filter(|use_path| !use_path.segments.is_empty()) |
195 | .take(20) | 197 | .take(20) |
196 | .collect::<BTreeSet<_>>() | 198 | .collect::<BTreeSet<_>>() |