1
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
9
use crate :: {
4
10
db:: DefDatabase ,
5
11
item_scope:: ItemInNs ,
6
12
path:: { ModPath , PathKind } ,
7
13
visibility:: Visibility ,
8
14
CrateId , ModuleDefId , ModuleId ,
9
15
} ;
10
- use hir_expand:: name:: { known, AsName , Name } ;
11
- use std:: sync:: Arc ;
12
- use test_utils:: tested_by;
16
+
17
+ // FIXME: handle local items
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
26
const MAX_PATH_LEN : usize = 15 ;
15
27
16
28
impl ModPath {
17
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
33
// When std library is present, paths starting with `std::`
22
34
// should be preferred over paths starting with `core::` and `alloc::`
23
35
fn can_start_with_std ( & self ) -> bool {
24
- self . segments
25
- . first ( )
26
- . filter ( |& first_segment| {
27
- first_segment == & known:: alloc || first_segment == & known:: core
28
- } )
29
- . is_some ( )
36
+ let first_segment = self . segments . first ( ) ;
37
+ first_segment == Some ( & known:: alloc) || first_segment == Some ( & known:: core)
30
38
}
31
39
32
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
52
fn find_path_inner (
54
53
db : & dyn DefDatabase ,
55
54
item : ItemInNs ,
@@ -215,11 +214,12 @@ fn find_importable_locations(
215
214
///
216
215
/// Note that the crate doesn't need to be the one in which the item is defined;
217
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
218
db : & dyn DefDatabase ,
220
219
item : ItemInNs ,
221
220
krate : CrateId ,
222
221
) -> Arc < [ ( ModuleId , Name , Visibility ) ] > {
222
+ let _p = profile ( "importable_locations_of_query" ) ;
223
223
let def_map = db. crate_def_map ( krate) ;
224
224
let mut result = Vec :: new ( ) ;
225
225
for ( local_id, data) in def_map. modules . iter ( ) {
0 commit comments