@@ -32,6 +32,7 @@ pub enum Definition {
32
32
Field ( Field ) ,
33
33
TupleField ( TupleField ) ,
34
34
Module ( Module ) ,
35
+ Crate ( Crate ) ,
35
36
Function ( Function ) ,
36
37
Adt ( Adt ) ,
37
38
Variant ( Variant ) ,
@@ -62,14 +63,19 @@ impl Definition {
62
63
pub fn krate ( & self , db : & RootDatabase ) -> Option < Crate > {
63
64
Some ( match self {
64
65
Definition :: Module ( m) => m. krate ( ) ,
66
+ & Definition :: Crate ( it) => it,
65
67
_ => self . module ( db) ?. krate ( ) ,
66
68
} )
67
69
}
68
70
71
+ /// Returns the module this definition resides in.
72
+ ///
73
+ /// As such, for modules themselves this will return the parent module.
69
74
pub fn module ( & self , db : & RootDatabase ) -> Option < Module > {
70
75
let module = match self {
71
76
Definition :: Macro ( it) => it. module ( db) ,
72
77
Definition :: Module ( it) => it. parent ( db) ?,
78
+ Definition :: Crate ( _) => return None ,
73
79
Definition :: Field ( it) => it. parent_def ( db) . module ( db) ,
74
80
Definition :: Function ( it) => it. module ( db) ,
75
81
Definition :: Adt ( it) => it. module ( db) ,
@@ -108,6 +114,7 @@ impl Definition {
108
114
match self {
109
115
Definition :: Macro ( it) => Some ( it. module ( db) . into ( ) ) ,
110
116
Definition :: Module ( it) => it. parent ( db) . map ( Definition :: Module ) ,
117
+ Definition :: Crate ( _) => None ,
111
118
Definition :: Field ( it) => Some ( it. parent_def ( db) . into ( ) ) ,
112
119
Definition :: Function ( it) => container_to_definition ( it. container ( db) ) ,
113
120
Definition :: Adt ( it) => Some ( it. module ( db) . into ( ) ) ,
@@ -137,6 +144,7 @@ impl Definition {
137
144
let vis = match self {
138
145
Definition :: Field ( sf) => sf. visibility ( db) ,
139
146
Definition :: Module ( it) => it. visibility ( db) ,
147
+ Definition :: Crate ( _) => return None ,
140
148
Definition :: Function ( it) => it. visibility ( db) ,
141
149
Definition :: Adt ( it) => it. visibility ( db) ,
142
150
Definition :: Const ( it) => it. visibility ( db) ,
@@ -146,8 +154,8 @@ impl Definition {
146
154
Definition :: TypeAlias ( it) => it. visibility ( db) ,
147
155
Definition :: Variant ( it) => it. visibility ( db) ,
148
156
Definition :: ExternCrateDecl ( it) => it. visibility ( db) ,
157
+ Definition :: Macro ( it) => it. visibility ( db) ,
149
158
Definition :: BuiltinType ( _) | Definition :: TupleField ( _) => Visibility :: Public ,
150
- Definition :: Macro ( _) => return None ,
151
159
Definition :: BuiltinAttr ( _)
152
160
| Definition :: BuiltinLifetime ( _)
153
161
| Definition :: ToolModule ( _)
@@ -167,6 +175,9 @@ impl Definition {
167
175
Definition :: Macro ( it) => it. name ( db) ,
168
176
Definition :: Field ( it) => it. name ( db) ,
169
177
Definition :: Module ( it) => it. name ( db) ?,
178
+ Definition :: Crate ( it) => {
179
+ Name :: new_symbol_root ( it. display_name ( db) ?. crate_name ( ) . symbol ( ) . clone ( ) )
180
+ }
170
181
Definition :: Function ( it) => it. name ( db) ,
171
182
Definition :: Adt ( it) => it. name ( db) ,
172
183
Definition :: Variant ( it) => it. name ( db) ,
@@ -202,6 +213,7 @@ impl Definition {
202
213
Definition :: Macro ( it) => it. docs ( db) ,
203
214
Definition :: Field ( it) => it. docs ( db) ,
204
215
Definition :: Module ( it) => it. docs ( db) ,
216
+ Definition :: Crate ( it) => it. docs ( db) ,
205
217
Definition :: Function ( it) => it. docs ( db) ,
206
218
Definition :: Adt ( it) => it. docs ( db) ,
207
219
Definition :: Variant ( it) => it. docs ( db) ,
@@ -282,6 +294,7 @@ impl Definition {
282
294
Definition :: Field ( it) => it. display ( db, edition) . to_string ( ) ,
283
295
Definition :: TupleField ( it) => it. display ( db, edition) . to_string ( ) ,
284
296
Definition :: Module ( it) => it. display ( db, edition) . to_string ( ) ,
297
+ Definition :: Crate ( it) => it. display ( db, edition) . to_string ( ) ,
285
298
Definition :: Function ( it) => it. display ( db, edition) . to_string ( ) ,
286
299
Definition :: Adt ( it) => it. display ( db, edition) . to_string ( ) ,
287
300
Definition :: Variant ( it) => it. display ( db, edition) . to_string ( ) ,
@@ -415,7 +428,7 @@ impl IdentClass {
415
428
}
416
429
IdentClass :: NameRefClass ( NameRefClass :: ExternCrateShorthand { decl, krate } ) => {
417
430
res. push ( ( Definition :: ExternCrateDecl ( decl) , None ) ) ;
418
- res. push ( ( Definition :: Module ( krate. root_module ( ) ) , None ) ) ;
431
+ res. push ( ( Definition :: Crate ( krate) , None ) ) ;
419
432
}
420
433
IdentClass :: Operator (
421
434
OperatorClass :: Await ( func)
@@ -456,7 +469,7 @@ impl IdentClass {
456
469
}
457
470
IdentClass :: NameRefClass ( NameRefClass :: ExternCrateShorthand { decl, krate } ) => {
458
471
res. push ( Definition :: ExternCrateDecl ( decl) ) ;
459
- res. push ( Definition :: Module ( krate. root_module ( ) ) ) ;
472
+ res. push ( Definition :: Crate ( krate) ) ;
460
473
}
461
474
IdentClass :: Operator ( _) => ( ) ,
462
475
}
@@ -800,7 +813,7 @@ impl NameRefClass {
800
813
let extern_crate = sema. to_def( & extern_crate_ast) ?;
801
814
let krate = extern_crate. resolved_crate( sema. db) ?;
802
815
Some ( if extern_crate_ast. rename( ) . is_some( ) {
803
- NameRefClass :: Definition ( Definition :: Module ( krate. root_module ( ) ) , None )
816
+ NameRefClass :: Definition ( Definition :: Crate ( krate) , None )
804
817
} else {
805
818
NameRefClass :: ExternCrateShorthand { krate, decl: extern_crate }
806
819
} )
0 commit comments