@@ -4,17 +4,16 @@ use std::{cmp::Ordering, fmt, hash::BuildHasherDefault, sync::Arc};
4
4
5
5
use base_db:: CrateId ;
6
6
use fst:: { self , Streamer } ;
7
+ use hir_expand:: name:: Name ;
7
8
use indexmap:: { map:: Entry , IndexMap } ;
9
+ use itertools:: Itertools ;
8
10
use rustc_hash:: { FxHashMap , FxHasher } ;
9
11
use smallvec:: SmallVec ;
10
12
use syntax:: SmolStr ;
11
13
12
14
use crate :: {
13
- db:: DefDatabase ,
14
- item_scope:: ItemInNs ,
15
- path:: { ModPath , PathKind } ,
16
- visibility:: Visibility ,
17
- AssocItemId , ModuleDefId , ModuleId , TraitId ,
15
+ db:: DefDatabase , item_scope:: ItemInNs , visibility:: Visibility , AssocItemId , ModuleDefId ,
16
+ ModuleId , TraitId ,
18
17
} ;
19
18
20
19
type FxIndexMap < K , V > = IndexMap < K , V , BuildHasherDefault < FxHasher > > ;
@@ -23,11 +22,28 @@ type FxIndexMap<K, V> = IndexMap<K, V, BuildHasherDefault<FxHasher>>;
23
22
#[ derive( Debug , Clone , Eq , PartialEq ) ]
24
23
pub struct ImportInfo {
25
24
/// A path that can be used to import the item, relative to the crate's root.
26
- pub path : ModPath ,
25
+ pub path : ImportPath ,
27
26
/// The module containing this item.
28
27
pub container : ModuleId ,
29
28
}
30
29
30
+ #[ derive( Debug , Clone , Eq , PartialEq ) ]
31
+ pub struct ImportPath {
32
+ pub segments : Vec < Name > ,
33
+ }
34
+
35
+ impl fmt:: Display for ImportPath {
36
+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
37
+ fmt:: Display :: fmt ( & self . segments . iter ( ) . format ( "::" ) , f)
38
+ }
39
+ }
40
+
41
+ impl ImportPath {
42
+ fn len ( & self ) -> usize {
43
+ self . segments . len ( )
44
+ }
45
+ }
46
+
31
47
/// A map from publicly exported items to the path needed to import/name them from a downstream
32
48
/// crate.
33
49
///
@@ -61,7 +77,7 @@ impl ImportMap {
61
77
let mut import_map = Self :: default ( ) ;
62
78
63
79
// We look only into modules that are public(ly reexported), starting with the crate root.
64
- let empty = ModPath { kind : PathKind :: Plain , segments : vec ! [ ] } ;
80
+ let empty = ImportPath { segments : vec ! [ ] } ;
65
81
let root = ModuleId { krate, local_id : def_map. root } ;
66
82
let mut worklist = vec ! [ ( root, empty) ] ;
67
83
while let Some ( ( module, mod_path) ) = worklist. pop ( ) {
@@ -152,8 +168,8 @@ impl ImportMap {
152
168
}
153
169
154
170
/// Returns the `ModPath` needed to import/mention `item`, relative to this crate's root.
155
- pub fn path_of ( & self , item : ItemInNs ) -> Option < & ModPath > {
156
- Some ( & self . map . get ( & item) ? . path )
171
+ pub fn path_of ( & self , item : ItemInNs ) -> Option < & ImportPath > {
172
+ self . import_info_for ( item) . map ( |it| & it . path )
157
173
}
158
174
159
175
pub fn import_info_for ( & self , item : ItemInNs ) -> Option < & ImportInfo > {
@@ -197,7 +213,7 @@ impl fmt::Debug for ImportMap {
197
213
}
198
214
}
199
215
200
- fn fst_path ( path : & ModPath ) -> String {
216
+ fn fst_path ( path : & ImportPath ) -> String {
201
217
let mut s = path. to_string ( ) ;
202
218
s. make_ascii_lowercase ( ) ;
203
219
s
0 commit comments