aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorKirill Bulatov <[email protected]>2020-03-23 11:34:56 +0000
committerKirill Bulatov <[email protected]>2020-03-24 08:43:22 +0000
commit944f28fe5bf2b8e4316cc67bf5f824333fc4f180 (patch)
tree8deb8e01871b17968611832b795abcd1c3fd24e8 /crates
parentd5e11b33a36755b139367e1f91a52f5ec27193f6 (diff)
Use more generic public api
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_assists/Cargo.toml1
-rw-r--r--crates/ra_assists/src/ast_transform.rs2
-rw-r--r--crates/ra_assists/src/handlers/auto_import.rs44
-rw-r--r--crates/ra_assists/src/handlers/fill_match_arms.rs3
-rw-r--r--crates/ra_hir/src/code_model.rs21
-rw-r--r--crates/ra_hir/src/from_id.rs21
-rw-r--r--crates/ra_ide_db/Cargo.toml1
-rw-r--r--crates/ra_ide_db/src/imports_locator.rs10
8 files changed, 56 insertions, 47 deletions
diff --git a/crates/ra_assists/Cargo.toml b/crates/ra_assists/Cargo.toml
index 707746ad5..a87f4052a 100644
--- a/crates/ra_assists/Cargo.toml
+++ b/crates/ra_assists/Cargo.toml
@@ -12,6 +12,7 @@ format-buf = "1.0.0"
12join_to_string = "0.1.3" 12join_to_string = "0.1.3"
13rustc-hash = "1.1.0" 13rustc-hash = "1.1.0"
14itertools = "0.9.0" 14itertools = "0.9.0"
15either = "1.5.3"
15 16
16ra_syntax = { path = "../ra_syntax" } 17ra_syntax = { path = "../ra_syntax" }
17ra_text_edit = { path = "../ra_text_edit" } 18ra_text_edit = { path = "../ra_text_edit" }
diff --git a/crates/ra_assists/src/ast_transform.rs b/crates/ra_assists/src/ast_transform.rs
index 34c816f16..45558c448 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.into())?; 132 let found_path = from.find_use_path(self.source_scope.db, def)?;
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 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};
7use ra_ide_db::{defs::Definition, imports_locator::ImportsLocator, RootDatabase}; 7use ra_ide_db::{imports_locator::ImportsLocator, RootDatabase};
8use ra_prof::profile; 8use ra_prof::profile;
9use ra_syntax::{ 9use 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};
20use 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<_>>()
diff --git a/crates/ra_assists/src/handlers/fill_match_arms.rs b/crates/ra_assists/src/handlers/fill_match_arms.rs
index 869942b12..88b4c8926 100644
--- a/crates/ra_assists/src/handlers/fill_match_arms.rs
+++ b/crates/ra_assists/src/handlers/fill_match_arms.rs
@@ -154,8 +154,7 @@ fn resolve_tuple_of_enum_def(
154} 154}
155 155
156fn build_pat(db: &RootDatabase, module: hir::Module, var: hir::EnumVariant) -> Option<ast::Pat> { 156fn build_pat(db: &RootDatabase, module: hir::Module, var: hir::EnumVariant) -> Option<ast::Pat> {
157 let path = 157 let path = crate::ast_transform::path_to_ast(module.find_use_path(db, ModuleDef::from(var))?);
158 crate::ast_transform::path_to_ast(module.find_use_path(db, ModuleDef::from(var).into())?);
159 158
160 // FIXME: use HIR for this; it doesn't currently expose struct vs. tuple vs. unit variants though 159 // FIXME: use HIR for this; it doesn't currently expose struct vs. tuple vs. unit variants though
161 let pat: ast::Pat = match var.source(db).value.kind() { 160 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 9e4aa6022..c5cfd875f 100644
--- a/crates/ra_hir/src/code_model.rs
+++ b/crates/ra_hir/src/code_model.rs
@@ -143,17 +143,6 @@ impl ModuleDef {
143 } 143 }
144} 144}
145 145
146impl From<ModuleDef> for ItemInNs {
147 fn from(module_def: ModuleDef) -> Self {
148 match module_def {
149 ModuleDef::Static(_) | ModuleDef::Const(_) | ModuleDef::Function(_) => {
150 ItemInNs::Values(module_def.into())
151 }
152 _ => ItemInNs::Types(module_def.into()),
153 }
154 }
155}
156
157pub use hir_def::{ 146pub use hir_def::{
158 attr::Attrs, item_scope::ItemInNs, visibility::Visibility, AssocItemId, AssocItemLoc, 147 attr::Attrs, item_scope::ItemInNs, visibility::Visibility, AssocItemId, AssocItemLoc,
159}; 148};
@@ -290,9 +279,9 @@ impl Module {
290 pub fn find_use_path( 279 pub fn find_use_path(
291 self, 280 self,
292 db: &dyn DefDatabase, 281 db: &dyn DefDatabase,
293 item: ItemInNs, 282 item: impl Into<ItemInNs>,
294 ) -> Option<hir_def::path::ModPath> { 283 ) -> Option<hir_def::path::ModPath> {
295 hir_def::find_path::find_path(db, item, self.into()) 284 hir_def::find_path::find_path(db, item.into(), self.into())
296 } 285 }
297} 286}
298 287
@@ -764,12 +753,6 @@ impl MacroDef {
764 } 753 }
765} 754}
766 755
767impl From<MacroDef> for ItemInNs {
768 fn from(macro_def: MacroDef) -> Self {
769 ItemInNs::Macros(macro_def.into())
770 }
771}
772
773/// Invariant: `inner.as_assoc_item(db).is_some()` 756/// Invariant: `inner.as_assoc_item(db).is_some()`
774/// We do not actively enforce this invariant. 757/// We do not actively enforce this invariant.
775#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] 758#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
diff --git a/crates/ra_hir/src/from_id.rs b/crates/ra_hir/src/from_id.rs
index c179b13c6..62fb52e72 100644
--- a/crates/ra_hir/src/from_id.rs
+++ b/crates/ra_hir/src/from_id.rs
@@ -9,8 +9,8 @@ use hir_def::{
9}; 9};
10 10
11use crate::{ 11use crate::{
12 Adt, AssocItem, AttrDef, DefWithBody, EnumVariant, GenericDef, Local, ModuleDef, StructField, 12 code_model::ItemInNs, Adt, AssocItem, AttrDef, DefWithBody, EnumVariant, GenericDef, Local,
13 VariantDef, 13 MacroDef, ModuleDef, StructField, VariantDef,
14}; 14};
15 15
16macro_rules! from_id { 16macro_rules! from_id {
@@ -228,3 +228,20 @@ impl From<(DefWithBodyId, PatId)> for Local {
228 Local { parent, pat_id } 228 Local { parent, pat_id }
229 } 229 }
230} 230}
231
232impl From<MacroDef> for ItemInNs {
233 fn from(macro_def: MacroDef) -> Self {
234 ItemInNs::Macros(macro_def.into())
235 }
236}
237
238impl From<ModuleDef> for ItemInNs {
239 fn from(module_def: ModuleDef) -> Self {
240 match module_def {
241 ModuleDef::Static(_) | ModuleDef::Const(_) | ModuleDef::Function(_) => {
242 ItemInNs::Values(module_def.into())
243 }
244 _ => ItemInNs::Types(module_def.into()),
245 }
246 }
247}
diff --git a/crates/ra_ide_db/Cargo.toml b/crates/ra_ide_db/Cargo.toml
index de4f5bce0..c3921bd40 100644
--- a/crates/ra_ide_db/Cargo.toml
+++ b/crates/ra_ide_db/Cargo.toml
@@ -17,6 +17,7 @@ fst = { version = "0.4", default-features = false }
17rustc-hash = "1.1.0" 17rustc-hash = "1.1.0"
18superslice = "1.0.0" 18superslice = "1.0.0"
19once_cell = "1.3.1" 19once_cell = "1.3.1"
20either = "1.5.3"
20 21
21ra_syntax = { path = "../ra_syntax" } 22ra_syntax = { path = "../ra_syntax" }
22ra_text_edit = { path = "../ra_text_edit" } 23ra_text_edit = { path = "../ra_text_edit" }
diff --git a/crates/ra_ide_db/src/imports_locator.rs b/crates/ra_ide_db/src/imports_locator.rs
index 24b6e4ad0..bf0d8db60 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
4use hir::Semantics; 4use hir::{MacroDef, ModuleDef, Semantics};
5use ra_prof::profile; 5use ra_prof::profile;
6use ra_syntax::{ast, AstNode, SyntaxKind::NAME}; 6use ra_syntax::{ast, AstNode, SyntaxKind::NAME};
7 7
@@ -10,6 +10,7 @@ use crate::{
10 symbol_index::{self, FileSymbol, Query}, 10 symbol_index::{self, FileSymbol, Query},
11 RootDatabase, 11 RootDatabase,
12}; 12};
13use either::Either;
13 14
14pub struct ImportsLocator<'a> { 15pub struct ImportsLocator<'a> {
15 sema: Semantics<'a, RootDatabase>, 16 sema: Semantics<'a, RootDatabase>,
@@ -20,7 +21,7 @@ impl<'a> ImportsLocator<'a> {
20 Self { sema: Semantics::new(db) } 21 Self { sema: Semantics::new(db) }
21 } 22 }
22 23
23 pub fn find_imports(&mut self, name_to_import: &str) -> Vec<Definition> { 24 pub fn find_imports(&mut self, name_to_import: &str) -> Vec<Either<ModuleDef, MacroDef>> {
24 let _p = profile("search_for_imports"); 25 let _p = profile("search_for_imports");
25 let db = self.sema.db; 26 let db = self.sema.db;
26 27
@@ -42,6 +43,11 @@ impl<'a> ImportsLocator<'a> {
42 .into_iter() 43 .into_iter()
43 .chain(lib_results.into_iter()) 44 .chain(lib_results.into_iter())
44 .filter_map(|import_candidate| self.get_name_definition(&import_candidate)) 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 })
45 .collect() 51 .collect()
46 } 52 }
47 53