@@ -32,6 +32,7 @@ pub enum Definition {
3232 Field ( Field ) ,
3333 TupleField ( TupleField ) ,
3434 Module ( Module ) ,
35+ Crate ( Crate ) ,
3536 Function ( Function ) ,
3637 Adt ( Adt ) ,
3738 Variant ( Variant ) ,
@@ -62,14 +63,19 @@ impl Definition {
6263 pub fn krate ( & self , db : & RootDatabase ) -> Option < Crate > {
6364 Some ( match self {
6465 Definition :: Module ( m) => m. krate ( ) ,
66+ & Definition :: Crate ( it) => it,
6567 _ => self . module ( db) ?. krate ( ) ,
6668 } )
6769 }
6870
71+ /// Returns the module this definition resides in.
72+ ///
73+ /// As such, for modules themselves this will return the parent module.
6974 pub fn module ( & self , db : & RootDatabase ) -> Option < Module > {
7075 let module = match self {
7176 Definition :: Macro ( it) => it. module ( db) ,
7277 Definition :: Module ( it) => it. parent ( db) ?,
78+ Definition :: Crate ( _) => return None ,
7379 Definition :: Field ( it) => it. parent_def ( db) . module ( db) ,
7480 Definition :: Function ( it) => it. module ( db) ,
7581 Definition :: Adt ( it) => it. module ( db) ,
@@ -108,6 +114,7 @@ impl Definition {
108114 match self {
109115 Definition :: Macro ( it) => Some ( it. module ( db) . into ( ) ) ,
110116 Definition :: Module ( it) => it. parent ( db) . map ( Definition :: Module ) ,
117+ Definition :: Crate ( _) => None ,
111118 Definition :: Field ( it) => Some ( it. parent_def ( db) . into ( ) ) ,
112119 Definition :: Function ( it) => container_to_definition ( it. container ( db) ) ,
113120 Definition :: Adt ( it) => Some ( it. module ( db) . into ( ) ) ,
@@ -137,6 +144,7 @@ impl Definition {
137144 let vis = match self {
138145 Definition :: Field ( sf) => sf. visibility ( db) ,
139146 Definition :: Module ( it) => it. visibility ( db) ,
147+ Definition :: Crate ( _) => return None ,
140148 Definition :: Function ( it) => it. visibility ( db) ,
141149 Definition :: Adt ( it) => it. visibility ( db) ,
142150 Definition :: Const ( it) => it. visibility ( db) ,
@@ -146,8 +154,8 @@ impl Definition {
146154 Definition :: TypeAlias ( it) => it. visibility ( db) ,
147155 Definition :: Variant ( it) => it. visibility ( db) ,
148156 Definition :: ExternCrateDecl ( it) => it. visibility ( db) ,
157+ Definition :: Macro ( it) => it. visibility ( db) ,
149158 Definition :: BuiltinType ( _) | Definition :: TupleField ( _) => Visibility :: Public ,
150- Definition :: Macro ( _) => return None ,
151159 Definition :: BuiltinAttr ( _)
152160 | Definition :: BuiltinLifetime ( _)
153161 | Definition :: ToolModule ( _)
@@ -167,6 +175,9 @@ impl Definition {
167175 Definition :: Macro ( it) => it. name ( db) ,
168176 Definition :: Field ( it) => it. name ( db) ,
169177 Definition :: Module ( it) => it. name ( db) ?,
178+ Definition :: Crate ( it) => {
179+ Name :: new_symbol_root ( it. display_name ( db) ?. crate_name ( ) . symbol ( ) . clone ( ) )
180+ }
170181 Definition :: Function ( it) => it. name ( db) ,
171182 Definition :: Adt ( it) => it. name ( db) ,
172183 Definition :: Variant ( it) => it. name ( db) ,
@@ -202,6 +213,7 @@ impl Definition {
202213 Definition :: Macro ( it) => it. docs ( db) ,
203214 Definition :: Field ( it) => it. docs ( db) ,
204215 Definition :: Module ( it) => it. docs ( db) ,
216+ Definition :: Crate ( it) => it. docs ( db) ,
205217 Definition :: Function ( it) => it. docs ( db) ,
206218 Definition :: Adt ( it) => it. docs ( db) ,
207219 Definition :: Variant ( it) => it. docs ( db) ,
@@ -282,6 +294,7 @@ impl Definition {
282294 Definition :: Field ( it) => it. display ( db, edition) . to_string ( ) ,
283295 Definition :: TupleField ( it) => it. display ( db, edition) . to_string ( ) ,
284296 Definition :: Module ( it) => it. display ( db, edition) . to_string ( ) ,
297+ Definition :: Crate ( it) => it. display ( db, edition) . to_string ( ) ,
285298 Definition :: Function ( it) => it. display ( db, edition) . to_string ( ) ,
286299 Definition :: Adt ( it) => it. display ( db, edition) . to_string ( ) ,
287300 Definition :: Variant ( it) => it. display ( db, edition) . to_string ( ) ,
@@ -415,7 +428,7 @@ impl IdentClass {
415428 }
416429 IdentClass :: NameRefClass ( NameRefClass :: ExternCrateShorthand { decl, krate } ) => {
417430 res. push ( ( Definition :: ExternCrateDecl ( decl) , None ) ) ;
418- res. push ( ( Definition :: Module ( krate. root_module ( ) ) , None ) ) ;
431+ res. push ( ( Definition :: Crate ( krate) , None ) ) ;
419432 }
420433 IdentClass :: Operator (
421434 OperatorClass :: Await ( func)
@@ -456,7 +469,7 @@ impl IdentClass {
456469 }
457470 IdentClass :: NameRefClass ( NameRefClass :: ExternCrateShorthand { decl, krate } ) => {
458471 res. push ( Definition :: ExternCrateDecl ( decl) ) ;
459- res. push ( Definition :: Module ( krate. root_module ( ) ) ) ;
472+ res. push ( Definition :: Crate ( krate) ) ;
460473 }
461474 IdentClass :: Operator ( _) => ( ) ,
462475 }
@@ -800,7 +813,7 @@ impl NameRefClass {
800813 let extern_crate = sema. to_def( & extern_crate_ast) ?;
801814 let krate = extern_crate. resolved_crate( sema. db) ?;
802815 Some ( if extern_crate_ast. rename( ) . is_some( ) {
803- NameRefClass :: Definition ( Definition :: Module ( krate. root_module ( ) ) , None )
816+ NameRefClass :: Definition ( Definition :: Crate ( krate) , None )
804817 } else {
805818 NameRefClass :: ExternCrateShorthand { krate, decl: extern_crate }
806819 } )
0 commit comments