@@ -206,8 +206,23 @@ impl NameClass {
206
206
207
207
let parent = name. syntax ( ) . parent ( ) ?;
208
208
209
- let def = if let Some ( item) = ast:: Item :: cast ( parent. clone ( ) ) {
210
- match item {
209
+ let definition = match_ast ! {
210
+ match parent {
211
+ ast:: Item ( it) => classify_item( sema, it) ?,
212
+ ast:: IdentPat ( it) => return classify_ident_pat( sema, it) ,
213
+ ast:: Rename ( it) => classify_rename( sema, it) ?,
214
+ ast:: SelfParam ( it) => Definition :: Local ( sema. to_def( & it) ?) ,
215
+ ast:: RecordField ( it) => Definition :: Field ( sema. to_def( & it) ?) ,
216
+ ast:: Variant ( it) => Definition :: Variant ( sema. to_def( & it) ?) ,
217
+ ast:: TypeParam ( it) => Definition :: GenericParam ( sema. to_def( & it) ?. into( ) ) ,
218
+ ast:: ConstParam ( it) => Definition :: GenericParam ( sema. to_def( & it) ?. into( ) ) ,
219
+ _ => return None ,
220
+ }
221
+ } ;
222
+ return Some ( NameClass :: Definition ( definition) ) ;
223
+
224
+ fn classify_item ( sema : & Semantics < RootDatabase > , item : ast:: Item ) -> Option < Definition > {
225
+ let definition = match item {
211
226
ast:: Item :: MacroRules ( it) => {
212
227
Definition :: Macro ( sema. to_def ( & ast:: Macro :: MacroRules ( it) ) ?)
213
228
}
@@ -229,14 +244,20 @@ impl NameClass {
229
244
ast:: Item :: Struct ( it) => Definition :: Adt ( hir:: Adt :: Struct ( sema. to_def ( & it) ?) ) ,
230
245
ast:: Item :: Union ( it) => Definition :: Adt ( hir:: Adt :: Union ( sema. to_def ( & it) ?) ) ,
231
246
_ => return None ,
232
- }
233
- } else if let Some ( it) = ast:: IdentPat :: cast ( parent. clone ( ) ) {
234
- if let Some ( def) = sema. resolve_bind_pat_to_const ( & it) {
247
+ } ;
248
+ Some ( definition)
249
+ }
250
+
251
+ fn classify_ident_pat (
252
+ sema : & Semantics < RootDatabase > ,
253
+ ident_pat : ast:: IdentPat ,
254
+ ) -> Option < NameClass > {
255
+ if let Some ( def) = sema. resolve_bind_pat_to_const ( & ident_pat) {
235
256
return Some ( NameClass :: ConstReference ( Definition :: from ( def) ) ) ;
236
257
}
237
258
238
- let local = sema. to_def ( & it ) ?;
239
- let pat_parent = it . syntax ( ) . parent ( ) ;
259
+ let local = sema. to_def ( & ident_pat ) ?;
260
+ let pat_parent = ident_pat . syntax ( ) . parent ( ) ;
240
261
if let Some ( record_pat_field) = pat_parent. and_then ( ast:: RecordPatField :: cast) {
241
262
if record_pat_field. name_ref ( ) . is_none ( ) {
242
263
if let Some ( field) = sema. resolve_record_pat_field ( & record_pat_field) {
@@ -247,57 +268,23 @@ impl NameClass {
247
268
}
248
269
}
249
270
}
271
+ Some ( NameClass :: Definition ( Definition :: Local ( local) ) )
272
+ }
250
273
251
- Definition :: Local ( local)
252
- } else if let Some ( it) = ast:: Rename :: cast ( parent. clone ( ) ) {
253
- if let Some ( use_tree) = it. syntax ( ) . parent ( ) . and_then ( ast:: UseTree :: cast) {
274
+ fn classify_rename (
275
+ sema : & Semantics < RootDatabase > ,
276
+ rename : ast:: Rename ,
277
+ ) -> Option < Definition > {
278
+ if let Some ( use_tree) = rename. syntax ( ) . parent ( ) . and_then ( ast:: UseTree :: cast) {
254
279
let path = use_tree. path ( ) ?;
255
- let path_segment = path. segment ( ) ?;
256
- let name_ref = path_segment. name_ref ( ) ?;
257
- let name_ref = if name_ref. self_token ( ) . is_some ( ) {
258
- use_tree
259
- . syntax ( )
260
- . parent ( )
261
- . as_ref ( )
262
- // Skip over UseTreeList
263
- . and_then ( |it| {
264
- let use_tree = it. parent ( ) . and_then ( ast:: UseTree :: cast) ?;
265
- let path = use_tree. path ( ) ?;
266
- let path_segment = path. segment ( ) ?;
267
- path_segment. name_ref ( )
268
- } )
269
- . unwrap_or ( name_ref)
270
- } else {
271
- name_ref
272
- } ;
273
- let name_ref_class = NameRefClass :: classify ( sema, & name_ref) ?;
274
-
275
- match name_ref_class {
276
- NameRefClass :: Definition ( def) => def,
277
- NameRefClass :: FieldShorthand { local_ref : _, field_ref } => {
278
- Definition :: Field ( field_ref)
279
- }
280
- }
280
+ sema. resolve_path ( & path) . map ( Definition :: from)
281
281
} else {
282
- let extern_crate = it . syntax ( ) . parent ( ) . and_then ( ast:: ExternCrate :: cast) ?;
282
+ let extern_crate = rename . syntax ( ) . parent ( ) . and_then ( ast:: ExternCrate :: cast) ?;
283
283
let krate = sema. resolve_extern_crate ( & extern_crate) ?;
284
284
let root_module = krate. root_module ( sema. db ) ;
285
- Definition :: Module ( root_module)
286
- }
287
- } else {
288
- match_ast ! {
289
- match parent {
290
- ast:: SelfParam ( it) => Definition :: Local ( sema. to_def( & it) ?) ,
291
- ast:: RecordField ( it) => Definition :: Field ( sema. to_def( & it) ?) ,
292
- ast:: Variant ( it) => Definition :: Variant ( sema. to_def( & it) ?) ,
293
- ast:: TypeParam ( it) => Definition :: GenericParam ( sema. to_def( & it) ?. into( ) ) ,
294
- ast:: ConstParam ( it) => Definition :: GenericParam ( sema. to_def( & it) ?. into( ) ) ,
295
- _ => return None ,
296
- }
285
+ Some ( Definition :: Module ( root_module) )
297
286
}
298
- } ;
299
-
300
- Some ( NameClass :: Definition ( def) )
287
+ }
301
288
}
302
289
303
290
pub fn classify_lifetime (
@@ -307,19 +294,14 @@ impl NameClass {
307
294
let _p = profile:: span ( "classify_lifetime" ) . detail ( || lifetime. to_string ( ) ) ;
308
295
let parent = lifetime. syntax ( ) . parent ( ) ?;
309
296
310
- match_ast ! {
311
- match parent {
312
- ast:: LifetimeParam ( it) => {
313
- let def = sema. to_def( & it) ?;
314
- Some ( NameClass :: Definition ( Definition :: GenericParam ( def. into( ) ) ) )
315
- } ,
316
- ast:: Label ( it) => {
317
- let def = sema. to_def( & it) ?;
318
- Some ( NameClass :: Definition ( Definition :: Label ( def) ) )
319
- } ,
320
- _ => None ,
321
- }
297
+ if let Some ( it) = ast:: LifetimeParam :: cast ( parent. clone ( ) ) {
298
+ sema. to_def ( & it) . map ( Into :: into) . map ( Definition :: GenericParam )
299
+ } else if let Some ( it) = ast:: Label :: cast ( parent. clone ( ) ) {
300
+ sema. to_def ( & it) . map ( Definition :: Label )
301
+ } else {
302
+ None
322
303
}
304
+ . map ( NameClass :: Definition )
323
305
}
324
306
}
325
307
0 commit comments