@@ -16,8 +16,8 @@ use syntax::ast;
16
16
17
17
use crate :: {
18
18
db:: DefDatabase , per_ns:: PerNs , visibility:: Visibility , AdtId , BuiltinType , ConstId ,
19
- ExternCrateId , HasModule , ImplId , LocalModuleId , MacroId , ModuleDefId , ModuleId , TraitId ,
20
- UseId ,
19
+ ExternCrateId , HasModule , ImplId , LocalModuleId , Lookup , MacroId , ModuleDefId , ModuleId ,
20
+ TraitId , UseId ,
21
21
} ;
22
22
23
23
#[ derive( Debug , Default ) ]
@@ -55,7 +55,7 @@ pub enum ImportOrDef {
55
55
ExternCrate ( ExternCrateId ) ,
56
56
Def ( ModuleDefId ) ,
57
57
}
58
- #[ derive( Debug , Copy , Clone , PartialEq , Eq , Hash ) ]
58
+ #[ derive( Debug , Clone , Copy , PartialEq , Eq , Hash , Ord , PartialOrd ) ]
59
59
pub struct ImportId {
60
60
pub import : UseId ,
61
61
pub idx : Idx < ast:: UseTree > ,
@@ -142,11 +142,77 @@ impl ItemScope {
142
142
. chain ( self . values . keys ( ) )
143
143
. chain ( self . macros . keys ( ) )
144
144
. chain ( self . unresolved . iter ( ) )
145
- . sorted ( )
146
145
. unique ( )
146
+ . sorted ( )
147
147
. map ( move |name| ( name, self . get ( name) ) )
148
148
}
149
149
150
+ pub fn imports ( & self ) -> impl Iterator < Item = ImportId > + ' _ {
151
+ self . use_imports_types
152
+ . keys ( )
153
+ . copied ( )
154
+ . filter_map ( ImportOrExternCrate :: into_import)
155
+ . chain ( self . use_imports_values . keys ( ) . copied ( ) )
156
+ . chain ( self . use_imports_macros . keys ( ) . copied ( ) )
157
+ . unique ( )
158
+ . sorted ( )
159
+ }
160
+
161
+ pub fn fully_resolve_import ( & self , db : & dyn DefDatabase , mut import : ImportId ) -> PerNs {
162
+ let mut res = PerNs :: none ( ) ;
163
+
164
+ let mut def_map;
165
+ let mut scope = self ;
166
+ while let Some ( & m) = scope. use_imports_macros . get ( & import) {
167
+ match m {
168
+ ImportOrDef :: Import ( i) => {
169
+ let module_id = i. import . lookup ( db) . container ;
170
+ def_map = module_id. def_map ( db) ;
171
+ scope = & def_map[ module_id. local_id ] . scope ;
172
+ import = i;
173
+ }
174
+ ImportOrDef :: Def ( ModuleDefId :: MacroId ( def) ) => {
175
+ res. macros = Some ( ( def, Visibility :: Public , None ) ) ;
176
+ break ;
177
+ }
178
+ _ => break ,
179
+ }
180
+ }
181
+ let mut scope = self ;
182
+ while let Some ( & m) = scope. use_imports_types . get ( & ImportOrExternCrate :: Import ( import) ) {
183
+ match m {
184
+ ImportOrDef :: Import ( i) => {
185
+ let module_id = i. import . lookup ( db) . container ;
186
+ def_map = module_id. def_map ( db) ;
187
+ scope = & def_map[ module_id. local_id ] . scope ;
188
+ import = i;
189
+ }
190
+ ImportOrDef :: Def ( def) => {
191
+ res. types = Some ( ( def, Visibility :: Public , None ) ) ;
192
+ break ;
193
+ }
194
+ _ => break ,
195
+ }
196
+ }
197
+ let mut scope = self ;
198
+ while let Some ( & m) = scope. use_imports_values . get ( & import) {
199
+ match m {
200
+ ImportOrDef :: Import ( i) => {
201
+ let module_id = i. import . lookup ( db) . container ;
202
+ def_map = module_id. def_map ( db) ;
203
+ scope = & def_map[ module_id. local_id ] . scope ;
204
+ import = i;
205
+ }
206
+ ImportOrDef :: Def ( def) => {
207
+ res. values = Some ( ( def, Visibility :: Public , None ) ) ;
208
+ break ;
209
+ }
210
+ _ => break ,
211
+ }
212
+ }
213
+ res
214
+ }
215
+
150
216
pub fn declarations ( & self ) -> impl Iterator < Item = ModuleDefId > + ' _ {
151
217
self . declarations . iter ( ) . copied ( )
152
218
}
0 commit comments