diff options
Diffstat (limited to 'crates/ra_hir_def')
-rw-r--r-- | crates/ra_hir_def/src/db.rs | 2 | ||||
-rw-r--r-- | crates/ra_hir_def/src/find_path.rs | 40 |
2 files changed, 21 insertions, 21 deletions
diff --git a/crates/ra_hir_def/src/db.rs b/crates/ra_hir_def/src/db.rs index 498a4c917..2f71511ba 100644 --- a/crates/ra_hir_def/src/db.rs +++ b/crates/ra_hir_def/src/db.rs | |||
@@ -112,7 +112,7 @@ pub trait DefDatabase: InternDatabase + AstDatabase + Upcast<dyn AstDatabase> { | |||
112 | #[salsa::invoke(Documentation::documentation_query)] | 112 | #[salsa::invoke(Documentation::documentation_query)] |
113 | fn documentation(&self, def: AttrDefId) -> Option<Documentation>; | 113 | fn documentation(&self, def: AttrDefId) -> Option<Documentation>; |
114 | 114 | ||
115 | #[salsa::invoke(find_path::importable_locations_in_crate)] | 115 | #[salsa::invoke(find_path::importable_locations_of_query)] |
116 | fn importable_locations_of( | 116 | fn importable_locations_of( |
117 | &self, | 117 | &self, |
118 | item: ItemInNs, | 118 | item: ItemInNs, |
diff --git a/crates/ra_hir_def/src/find_path.rs b/crates/ra_hir_def/src/find_path.rs index 1ca20fabd..2eb12ec8f 100644 --- a/crates/ra_hir_def/src/find_path.rs +++ b/crates/ra_hir_def/src/find_path.rs | |||
@@ -1,5 +1,11 @@ | |||
1 | //! An algorithm to find a path to refer to a certain item. | 1 | //! An algorithm to find a path to refer to a certain item. |
2 | 2 | ||
3 | use std::sync::Arc; | ||
4 | |||
5 | use hir_expand::name::{known, AsName, Name}; | ||
6 | use ra_prof::profile; | ||
7 | use test_utils::tested_by; | ||
8 | |||
3 | use crate::{ | 9 | use crate::{ |
4 | db::DefDatabase, | 10 | db::DefDatabase, |
5 | item_scope::ItemInNs, | 11 | item_scope::ItemInNs, |
@@ -7,26 +13,28 @@ use crate::{ | |||
7 | visibility::Visibility, | 13 | visibility::Visibility, |
8 | CrateId, ModuleDefId, ModuleId, | 14 | CrateId, ModuleDefId, ModuleId, |
9 | }; | 15 | }; |
10 | use hir_expand::name::{known, AsName, Name}; | 16 | |
11 | use std::sync::Arc; | 17 | // FIXME: handle local items |
12 | use test_utils::tested_by; | 18 | |
19 | /// Find a path that can be used to refer to a certain item. This can depend on | ||
20 | /// *from where* you're referring to the item, hence the `from` parameter. | ||
21 | pub fn find_path(db: &dyn DefDatabase, item: ItemInNs, from: ModuleId) -> Option<ModPath> { | ||
22 | let _p = profile("find_path"); | ||
23 | find_path_inner(db, item, from, MAX_PATH_LEN) | ||
24 | } | ||
13 | 25 | ||
14 | const MAX_PATH_LEN: usize = 15; | 26 | const MAX_PATH_LEN: usize = 15; |
15 | 27 | ||
16 | impl ModPath { | 28 | impl ModPath { |
17 | fn starts_with_std(&self) -> bool { | 29 | fn starts_with_std(&self) -> bool { |
18 | self.segments.first().filter(|&first_segment| first_segment == &known::std).is_some() | 30 | self.segments.first() == Some(&known::std) |
19 | } | 31 | } |
20 | 32 | ||
21 | // When std library is present, paths starting with `std::` | 33 | // When std library is present, paths starting with `std::` |
22 | // should be preferred over paths starting with `core::` and `alloc::` | 34 | // should be preferred over paths starting with `core::` and `alloc::` |
23 | fn can_start_with_std(&self) -> bool { | 35 | fn can_start_with_std(&self) -> bool { |
24 | self.segments | 36 | let first_segment = self.segments.first(); |
25 | .first() | 37 | first_segment == Some(&known::alloc) || first_segment == Some(&known::core) |
26 | .filter(|&first_segment| { | ||
27 | first_segment == &known::alloc || first_segment == &known::core | ||
28 | }) | ||
29 | .is_some() | ||
30 | } | 38 | } |
31 | 39 | ||
32 | fn len(&self) -> usize { | 40 | fn len(&self) -> usize { |
@@ -41,15 +49,6 @@ impl ModPath { | |||
41 | } | 49 | } |
42 | } | 50 | } |
43 | 51 | ||
44 | // FIXME: handle local items | ||
45 | |||
46 | /// Find a path that can be used to refer to a certain item. This can depend on | ||
47 | /// *from where* you're referring to the item, hence the `from` parameter. | ||
48 | pub fn find_path(db: &dyn DefDatabase, item: ItemInNs, from: ModuleId) -> Option<ModPath> { | ||
49 | let _p = ra_prof::profile("find_path"); | ||
50 | find_path_inner(db, item, from, MAX_PATH_LEN) | ||
51 | } | ||
52 | |||
53 | fn find_path_inner( | 52 | fn find_path_inner( |
54 | db: &dyn DefDatabase, | 53 | db: &dyn DefDatabase, |
55 | item: ItemInNs, | 54 | item: ItemInNs, |
@@ -215,11 +214,12 @@ fn find_importable_locations( | |||
215 | /// | 214 | /// |
216 | /// Note that the crate doesn't need to be the one in which the item is defined; | 215 | /// Note that the crate doesn't need to be the one in which the item is defined; |
217 | /// it might be re-exported in other crates. | 216 | /// it might be re-exported in other crates. |
218 | pub(crate) fn importable_locations_in_crate( | 217 | pub(crate) fn importable_locations_of_query( |
219 | db: &dyn DefDatabase, | 218 | db: &dyn DefDatabase, |
220 | item: ItemInNs, | 219 | item: ItemInNs, |
221 | krate: CrateId, | 220 | krate: CrateId, |
222 | ) -> Arc<[(ModuleId, Name, Visibility)]> { | 221 | ) -> Arc<[(ModuleId, Name, Visibility)]> { |
222 | let _p = profile("importable_locations_of_query"); | ||
223 | let def_map = db.crate_def_map(krate); | 223 | let def_map = db.crate_def_map(krate); |
224 | let mut result = Vec::new(); | 224 | let mut result = Vec::new(); |
225 | for (local_id, data) in def_map.modules.iter() { | 225 | for (local_id, data) in def_map.modules.iter() { |