diff options
Diffstat (limited to 'crates')
-rw-r--r-- | crates/ra_assists/src/ast_transform.rs | 2 | ||||
-rw-r--r-- | crates/ra_assists/src/handlers/auto_import.rs | 34 | ||||
-rw-r--r-- | crates/ra_assists/src/handlers/fill_match_arms.rs | 5 | ||||
-rw-r--r-- | crates/ra_hir/src/code_model.rs | 31 | ||||
-rw-r--r-- | crates/ra_ide_db/src/imports_locator.rs | 8 |
5 files changed, 46 insertions, 34 deletions
diff --git a/crates/ra_assists/src/ast_transform.rs b/crates/ra_assists/src/ast_transform.rs index 45558c448..34c816f16 100644 --- a/crates/ra_assists/src/ast_transform.rs +++ b/crates/ra_assists/src/ast_transform.rs | |||
@@ -129,7 +129,7 @@ impl<'a> QualifyPaths<'a> { | |||
129 | let resolution = self.source_scope.resolve_hir_path(&hir_path?)?; | 129 | let resolution = self.source_scope.resolve_hir_path(&hir_path?)?; |
130 | match resolution { | 130 | match resolution { |
131 | PathResolution::Def(def) => { | 131 | PathResolution::Def(def) => { |
132 | let found_path = from.find_use_path(self.source_scope.db, def)?; | 132 | let found_path = from.find_use_path(self.source_scope.db, def.into())?; |
133 | let mut path = path_to_ast(found_path); | 133 | let mut path = path_to_ast(found_path); |
134 | 134 | ||
135 | let type_args = p | 135 | let type_args = p |
diff --git a/crates/ra_assists/src/handlers/auto_import.rs b/crates/ra_assists/src/handlers/auto_import.rs index bb280f633..49d8c4c3d 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::{imports_locator::ImportsLocator, RootDatabase}; | 7 | use ra_ide_db::{defs::Definition, 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}, |
@@ -127,14 +127,16 @@ impl AutoImportAssets { | |||
127 | ImportsLocator::new(db) | 127 | ImportsLocator::new(db) |
128 | .find_imports(&self.get_search_query()) | 128 | .find_imports(&self.get_search_query()) |
129 | .into_iter() | 129 | .into_iter() |
130 | .filter_map(|module_def| match &self.import_candidate { | 130 | .filter_map(|definition| match &self.import_candidate { |
131 | ImportCandidate::TraitAssocItem(assoc_item_type, _) => { | 131 | ImportCandidate::TraitAssocItem(assoc_item_type, _) => { |
132 | let located_assoc_item = match module_def { | 132 | let located_assoc_item = match definition { |
133 | ModuleDef::Function(located_function) => located_function | 133 | Definition::ModuleDef(ModuleDef::Function(located_function)) => { |
134 | .as_assoc_item(db) | 134 | located_function |
135 | .map(|assoc| assoc.container(db)) | 135 | .as_assoc_item(db) |
136 | .and_then(Self::assoc_to_trait), | 136 | .map(|assoc| assoc.container(db)) |
137 | ModuleDef::Const(located_const) => located_const | 137 | .and_then(Self::assoc_to_trait) |
138 | } | ||
139 | Definition::ModuleDef(ModuleDef::Const(located_const)) => located_const | ||
138 | .as_assoc_item(db) | 140 | .as_assoc_item(db) |
139 | .map(|assoc| assoc.container(db)) | 141 | .map(|assoc| assoc.container(db)) |
140 | .and_then(Self::assoc_to_trait), | 142 | .and_then(Self::assoc_to_trait), |
@@ -152,11 +154,13 @@ impl AutoImportAssets { | |||
152 | None, | 154 | None, |
153 | |_, assoc| Self::assoc_to_trait(assoc.container(db)), | 155 | |_, assoc| Self::assoc_to_trait(assoc.container(db)), |
154 | ) | 156 | ) |
155 | .map(ModuleDef::from) | 157 | .map(|located_trait| ModuleDef::from(located_trait).into()) |
156 | } | 158 | } |
157 | ImportCandidate::TraitMethod(function_callee, _) => { | 159 | ImportCandidate::TraitMethod(function_callee, _) => { |
158 | let located_assoc_item = | 160 | let located_assoc_item = |
159 | if let ModuleDef::Function(located_function) = module_def { | 161 | if let Definition::ModuleDef(ModuleDef::Function(located_function)) = |
162 | definition | ||
163 | { | ||
160 | located_function | 164 | located_function |
161 | .as_assoc_item(db) | 165 | .as_assoc_item(db) |
162 | .map(|assoc| assoc.container(db)) | 166 | .map(|assoc| assoc.container(db)) |
@@ -178,11 +182,15 @@ impl AutoImportAssets { | |||
178 | Self::assoc_to_trait(function.as_assoc_item(db)?.container(db)) | 182 | Self::assoc_to_trait(function.as_assoc_item(db)?.container(db)) |
179 | }, | 183 | }, |
180 | ) | 184 | ) |
181 | .map(ModuleDef::from) | 185 | .map(|located_trait| ModuleDef::from(located_trait).into()) |
182 | } | 186 | } |
183 | _ => Some(module_def), | 187 | _ => match definition { |
188 | Definition::ModuleDef(module_def) => Some(module_def.into()), | ||
189 | Definition::Macro(macro_def) => Some(macro_def.into()), | ||
190 | _ => None, | ||
191 | }, | ||
184 | }) | 192 | }) |
185 | .filter_map(|module_def| self.module_with_name_to_import.find_use_path(db, module_def)) | 193 | .filter_map(|item| self.module_with_name_to_import.find_use_path(db, item)) |
186 | .filter(|use_path| !use_path.segments.is_empty()) | 194 | .filter(|use_path| !use_path.segments.is_empty()) |
187 | .take(20) | 195 | .take(20) |
188 | .collect::<BTreeSet<_>>() | 196 | .collect::<BTreeSet<_>>() |
diff --git a/crates/ra_assists/src/handlers/fill_match_arms.rs b/crates/ra_assists/src/handlers/fill_match_arms.rs index 7463b2af7..869942b12 100644 --- a/crates/ra_assists/src/handlers/fill_match_arms.rs +++ b/crates/ra_assists/src/handlers/fill_match_arms.rs | |||
@@ -2,7 +2,7 @@ | |||
2 | 2 | ||
3 | use std::iter; | 3 | use std::iter; |
4 | 4 | ||
5 | use hir::{Adt, HasSource, Semantics}; | 5 | use hir::{Adt, HasSource, ModuleDef, Semantics}; |
6 | use itertools::Itertools; | 6 | use itertools::Itertools; |
7 | use ra_ide_db::RootDatabase; | 7 | use ra_ide_db::RootDatabase; |
8 | 8 | ||
@@ -154,7 +154,8 @@ fn resolve_tuple_of_enum_def( | |||
154 | } | 154 | } |
155 | 155 | ||
156 | fn build_pat(db: &RootDatabase, module: hir::Module, var: hir::EnumVariant) -> Option<ast::Pat> { | 156 | fn build_pat(db: &RootDatabase, module: hir::Module, var: hir::EnumVariant) -> Option<ast::Pat> { |
157 | let path = crate::ast_transform::path_to_ast(module.find_use_path(db, var.into())?); | 157 | let path = |
158 | crate::ast_transform::path_to_ast(module.find_use_path(db, ModuleDef::from(var).into())?); | ||
158 | 159 | ||
159 | // FIXME: use HIR for this; it doesn't currently expose struct vs. tuple vs. unit variants though | 160 | // FIXME: use HIR for this; it doesn't currently expose struct vs. tuple vs. unit variants though |
160 | let pat: ast::Pat = match var.source(db).value.kind() { | 161 | let pat: ast::Pat = match var.source(db).value.kind() { |
diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index e91abf6f5..146e7820e 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs | |||
@@ -139,6 +139,17 @@ impl ModuleDef { | |||
139 | } | 139 | } |
140 | } | 140 | } |
141 | 141 | ||
142 | impl From<ModuleDef> for ItemInNs { | ||
143 | fn from(module_def: ModuleDef) -> Self { | ||
144 | match module_def { | ||
145 | ModuleDef::Static(_) | ModuleDef::Const(_) | ModuleDef::Function(_) => { | ||
146 | ItemInNs::Values(module_def.into()) | ||
147 | } | ||
148 | _ => ItemInNs::Types(module_def.into()), | ||
149 | } | ||
150 | } | ||
151 | } | ||
152 | |||
142 | pub use hir_def::{ | 153 | pub use hir_def::{ |
143 | attr::Attrs, item_scope::ItemInNs, visibility::Visibility, AssocItemId, AssocItemLoc, | 154 | attr::Attrs, item_scope::ItemInNs, visibility::Visibility, AssocItemId, AssocItemLoc, |
144 | }; | 155 | }; |
@@ -275,19 +286,9 @@ impl Module { | |||
275 | pub fn find_use_path( | 286 | pub fn find_use_path( |
276 | self, | 287 | self, |
277 | db: &dyn HirDatabase, | 288 | db: &dyn HirDatabase, |
278 | item: ModuleDef, | 289 | item: ItemInNs, |
279 | ) -> Option<hir_def::path::ModPath> { | 290 | ) -> Option<hir_def::path::ModPath> { |
280 | // FIXME expose namespace choice | 291 | hir_def::find_path::find_path(db.upcast(), item, self.into()) |
281 | hir_def::find_path::find_path(db.upcast(), determine_item_namespace(item), self.into()) | ||
282 | } | ||
283 | } | ||
284 | |||
285 | fn determine_item_namespace(module_def: ModuleDef) -> ItemInNs { | ||
286 | match module_def { | ||
287 | ModuleDef::Static(_) | ModuleDef::Const(_) | ModuleDef::Function(_) => { | ||
288 | ItemInNs::Values(module_def.into()) | ||
289 | } | ||
290 | _ => ItemInNs::Types(module_def.into()), | ||
291 | } | 292 | } |
292 | } | 293 | } |
293 | 294 | ||
@@ -759,6 +760,12 @@ impl MacroDef { | |||
759 | } | 760 | } |
760 | } | 761 | } |
761 | 762 | ||
763 | impl From<MacroDef> for ItemInNs { | ||
764 | fn from(macro_def: MacroDef) -> Self { | ||
765 | ItemInNs::Macros(macro_def.into()) | ||
766 | } | ||
767 | } | ||
768 | |||
762 | /// Invariant: `inner.as_assoc_item(db).is_some()` | 769 | /// Invariant: `inner.as_assoc_item(db).is_some()` |
763 | /// We do not actively enforce this invariant. | 770 | /// We do not actively enforce this invariant. |
764 | #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] | 771 | #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] |
diff --git a/crates/ra_ide_db/src/imports_locator.rs b/crates/ra_ide_db/src/imports_locator.rs index c96351982..24b6e4ad0 100644 --- a/crates/ra_ide_db/src/imports_locator.rs +++ b/crates/ra_ide_db/src/imports_locator.rs | |||
@@ -1,7 +1,7 @@ | |||
1 | //! This module contains an import search funcionality that is provided to the ra_assists module. | 1 | //! This module contains an import search funcionality that is provided to the ra_assists module. |
2 | //! Later, this should be moved away to a separate crate that is accessible from the ra_assists module. | 2 | //! Later, this should be moved away to a separate crate that is accessible from the ra_assists module. |
3 | 3 | ||
4 | use hir::{ModuleDef, Semantics}; | 4 | use hir::Semantics; |
5 | use ra_prof::profile; | 5 | use ra_prof::profile; |
6 | use ra_syntax::{ast, AstNode, SyntaxKind::NAME}; | 6 | use ra_syntax::{ast, AstNode, SyntaxKind::NAME}; |
7 | 7 | ||
@@ -20,7 +20,7 @@ impl<'a> ImportsLocator<'a> { | |||
20 | Self { sema: Semantics::new(db) } | 20 | Self { sema: Semantics::new(db) } |
21 | } | 21 | } |
22 | 22 | ||
23 | pub fn find_imports(&mut self, name_to_import: &str) -> Vec<ModuleDef> { | 23 | pub fn find_imports(&mut self, name_to_import: &str) -> Vec<Definition> { |
24 | let _p = profile("search_for_imports"); | 24 | let _p = profile("search_for_imports"); |
25 | let db = self.sema.db; | 25 | let db = self.sema.db; |
26 | 26 | ||
@@ -42,10 +42,6 @@ impl<'a> ImportsLocator<'a> { | |||
42 | .into_iter() | 42 | .into_iter() |
43 | .chain(lib_results.into_iter()) | 43 | .chain(lib_results.into_iter()) |
44 | .filter_map(|import_candidate| self.get_name_definition(&import_candidate)) | 44 | .filter_map(|import_candidate| self.get_name_definition(&import_candidate)) |
45 | .filter_map(|name_definition_to_import| match name_definition_to_import { | ||
46 | Definition::ModuleDef(module_def) => Some(module_def), | ||
47 | _ => None, | ||
48 | }) | ||
49 | .collect() | 45 | .collect() |
50 | } | 46 | } |
51 | 47 | ||