@@ -48,14 +48,15 @@ use hir_def::{
48
48
layout:: { self , ReprOptions , TargetDataLayout } ,
49
49
macro_id_to_def_id,
50
50
nameres:: { self , diagnostics:: DefDiagnostic } ,
51
+ path:: ImportAlias ,
51
52
per_ns:: PerNs ,
52
53
resolver:: { HasResolver , Resolver } ,
53
54
src:: HasSource as _,
54
- AssocItemId , AssocItemLoc , AttrDefId , ConstId , ConstParamId , DefWithBodyId , EnumId ,
55
- EnumVariantId , FunctionId , GenericDefId , HasModule , ImplId , InTypeConstId , ItemContainerId ,
56
- LifetimeParamId , LocalEnumVariantId , LocalFieldId , Lookup , MacroExpander , MacroId , ModuleId ,
57
- StaticId , StructId , TraitAliasId , TraitId , TypeAliasId , TypeOrConstParamId , TypeParamId ,
58
- UnionId ,
55
+ AssocItemId , AssocItemLoc , AttrDefId , ConstId , ConstParamId , CrateRootModuleId , DefWithBodyId ,
56
+ EnumId , EnumVariantId , ExternCrateId , FunctionId , GenericDefId , HasModule , ImplId ,
57
+ InTypeConstId , ItemContainerId , LifetimeParamId , LocalEnumVariantId , LocalFieldId , Lookup ,
58
+ MacroExpander , MacroId , ModuleId , StaticId , StructId , TraitAliasId , TraitId , TypeAliasId ,
59
+ TypeOrConstParamId , TypeParamId , UnionId ,
59
60
} ;
60
61
use hir_expand:: { name:: name, MacroCallKind } ;
61
62
use hir_ty:: {
@@ -200,9 +201,8 @@ impl Crate {
200
201
db. crate_graph ( ) . transitive_rev_deps ( self . id ) . map ( |id| Crate { id } )
201
202
}
202
203
203
- pub fn root_module ( self , db : & dyn HirDatabase ) -> Module {
204
- let def_map = db. crate_def_map ( self . id ) ;
205
- Module { id : def_map. crate_root ( ) . into ( ) }
204
+ pub fn root_module ( self ) -> Module {
205
+ Module { id : CrateRootModuleId :: from ( self . id ) . into ( ) }
206
206
}
207
207
208
208
pub fn modules ( self , db : & dyn HirDatabase ) -> Vec < Module > {
@@ -247,7 +247,7 @@ impl Crate {
247
247
/// Try to get the root URL of the documentation of a crate.
248
248
pub fn get_html_root_url ( self : & Crate , db : & dyn HirDatabase ) -> Option < String > {
249
249
// Look for #![doc(html_root_url = "...")]
250
- let attrs = db. attrs ( AttrDefId :: ModuleId ( self . root_module ( db ) . into ( ) ) ) ;
250
+ let attrs = db. attrs ( AttrDefId :: ModuleId ( self . root_module ( ) . into ( ) ) ) ;
251
251
let doc_url = attrs. by_key ( "doc" ) . find_string_value_in_tt ( "html_root_url" ) ;
252
252
doc_url. map ( |s| s. trim_matches ( '"' ) . trim_end_matches ( '/' ) . to_owned ( ) + "/" )
253
253
}
@@ -2128,6 +2128,47 @@ impl HasVisibility for Function {
2128
2128
}
2129
2129
}
2130
2130
2131
+ #[ derive( Debug , Clone , Copy , PartialEq , Eq , Hash ) ]
2132
+ pub struct ExternCrateDecl {
2133
+ pub ( crate ) id : ExternCrateId ,
2134
+ }
2135
+
2136
+ impl ExternCrateDecl {
2137
+ pub fn module ( self , db : & dyn HirDatabase ) -> Module {
2138
+ self . id . module ( db. upcast ( ) ) . into ( )
2139
+ }
2140
+
2141
+ pub fn resolved_crate ( self , db : & dyn HirDatabase ) -> Option < Crate > {
2142
+ db. extern_crate_decl_data ( self . id ) . crate_id . map ( Into :: into)
2143
+ }
2144
+
2145
+ pub fn name ( self , db : & dyn HirDatabase ) -> Name {
2146
+ db. extern_crate_decl_data ( self . id ) . name . clone ( )
2147
+ }
2148
+
2149
+ pub fn alias ( self , db : & dyn HirDatabase ) -> Option < ImportAlias > {
2150
+ db. extern_crate_decl_data ( self . id ) . alias . clone ( )
2151
+ }
2152
+
2153
+ /// Returns the name under which this crate is made accessible, taking `_` into account.
2154
+ pub fn alias_or_name ( self , db : & dyn HirDatabase ) -> Option < Name > {
2155
+ let extern_crate_decl_data = db. extern_crate_decl_data ( self . id ) ;
2156
+ match & extern_crate_decl_data. alias {
2157
+ Some ( ImportAlias :: Underscore ) => None ,
2158
+ Some ( ImportAlias :: Alias ( alias) ) => Some ( alias. clone ( ) ) ,
2159
+ None => Some ( extern_crate_decl_data. name . clone ( ) ) ,
2160
+ }
2161
+ }
2162
+ }
2163
+
2164
+ impl HasVisibility for ExternCrateDecl {
2165
+ fn visibility ( & self , db : & dyn HirDatabase ) -> Visibility {
2166
+ db. extern_crate_decl_data ( self . id )
2167
+ . visibility
2168
+ . resolve ( db. upcast ( ) , & self . id . resolver ( db. upcast ( ) ) )
2169
+ }
2170
+ }
2171
+
2131
2172
#[ derive( Debug , Clone , Copy , PartialEq , Eq , Hash ) ]
2132
2173
pub struct InTypeConst {
2133
2174
pub ( crate ) id : InTypeConstId ,
@@ -4715,6 +4756,12 @@ pub trait HasContainer {
4715
4756
fn container ( & self , db : & dyn HirDatabase ) -> ItemContainer ;
4716
4757
}
4717
4758
4759
+ impl HasContainer for ExternCrateDecl {
4760
+ fn container ( & self , db : & dyn HirDatabase ) -> ItemContainer {
4761
+ container_id_to_hir ( self . id . lookup ( db. upcast ( ) ) . container . into ( ) )
4762
+ }
4763
+ }
4764
+
4718
4765
impl HasContainer for Module {
4719
4766
fn container ( & self , db : & dyn HirDatabase ) -> ItemContainer {
4720
4767
// FIXME: handle block expressions as modules (their parent is in a different DefMap)
0 commit comments