@@ -16,8 +16,8 @@ use syntax::ast;
1616
1717use crate :: {
1818 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 ,
2121} ;
2222
2323#[ derive( Debug , Default ) ]
@@ -55,7 +55,7 @@ pub enum ImportOrDef {
5555 ExternCrate ( ExternCrateId ) ,
5656 Def ( ModuleDefId ) ,
5757}
58- #[ derive( Debug , Copy , Clone , PartialEq , Eq , Hash ) ]
58+ #[ derive( Debug , Clone , Copy , PartialEq , Eq , Hash , Ord , PartialOrd ) ]
5959pub struct ImportId {
6060 pub import : UseId ,
6161 pub idx : Idx < ast:: UseTree > ,
@@ -142,11 +142,77 @@ impl ItemScope {
142142 . chain ( self . values . keys ( ) )
143143 . chain ( self . macros . keys ( ) )
144144 . chain ( self . unresolved . iter ( ) )
145- . sorted ( )
146145 . unique ( )
146+ . sorted ( )
147147 . map ( move |name| ( name, self . get ( name) ) )
148148 }
149149
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+
150216 pub fn declarations ( & self ) -> impl Iterator < Item = ModuleDefId > + ' _ {
151217 self . declarations . iter ( ) . copied ( )
152218 }
0 commit comments