11use rustc_hash:: FxHashSet ;
22use syntax:: {
33 ast:: { self , GenericParamsOwner , NameOwner } ,
4- AstNode , SyntaxKind , TextRange , TextSize ,
4+ AstNode , TextRange , TextSize ,
55} ;
66
77use crate :: { assist_context:: AssistBuilder , AssistContext , AssistId , AssistKind , Assists } ;
@@ -35,13 +35,12 @@ static ASSIST_LABEL: &str = "Introduce named lifetime";
3535// FIXME: How can we handle renaming any one of multiple anonymous lifetimes?
3636// FIXME: should also add support for the case fun(f: &Foo) -> &<|>Foo
3737pub ( crate ) fn introduce_named_lifetime ( acc : & mut Assists , ctx : & AssistContext ) -> Option < ( ) > {
38- let lifetime_token = ctx
39- . find_token_syntax_at_offset ( SyntaxKind :: LIFETIME )
40- . filter ( |lifetime| lifetime. text ( ) == "'_" ) ?;
41- if let Some ( fn_def) = lifetime_token. ancestors ( ) . find_map ( ast:: Fn :: cast) {
42- generate_fn_def_assist ( acc, & fn_def, lifetime_token. text_range ( ) )
43- } else if let Some ( impl_def) = lifetime_token. ancestors ( ) . find_map ( ast:: Impl :: cast) {
44- generate_impl_def_assist ( acc, & impl_def, lifetime_token. text_range ( ) )
38+ let lifetime =
39+ ctx. find_node_at_offset :: < ast:: Lifetime > ( ) . filter ( |lifetime| lifetime. text ( ) == "'_" ) ?;
40+ if let Some ( fn_def) = lifetime. syntax ( ) . ancestors ( ) . find_map ( ast:: Fn :: cast) {
41+ generate_fn_def_assist ( acc, & fn_def, lifetime. lifetime_ident_token ( ) ?. text_range ( ) )
42+ } else if let Some ( impl_def) = lifetime. syntax ( ) . ancestors ( ) . find_map ( ast:: Impl :: cast) {
43+ generate_impl_def_assist ( acc, & impl_def, lifetime. lifetime_ident_token ( ) ?. text_range ( ) )
4544 } else {
4645 None
4746 }
@@ -58,7 +57,7 @@ fn generate_fn_def_assist(
5857 let end_of_fn_ident = fn_def. name ( ) ?. ident_token ( ) ?. text_range ( ) . end ( ) ;
5958 let self_param =
6059 // use the self if it's a reference and has no explicit lifetime
61- param_list. self_param ( ) . filter ( |p| p. lifetime_token ( ) . is_none ( ) && p. amp_token ( ) . is_some ( ) ) ;
60+ param_list. self_param ( ) . filter ( |p| p. lifetime ( ) . is_none ( ) && p. amp_token ( ) . is_some ( ) ) ;
6261 // compute the location which implicitly has the same lifetime as the anonymous lifetime
6362 let loc_needing_lifetime = if let Some ( self_param) = self_param {
6463 // if we have a self reference, use that
@@ -68,9 +67,7 @@ fn generate_fn_def_assist(
6867 let fn_params_without_lifetime: Vec < _ > = param_list
6968 . params ( )
7069 . filter_map ( |param| match param. ty ( ) {
71- Some ( ast:: Type :: RefType ( ascribed_type) )
72- if ascribed_type. lifetime_token ( ) == None =>
73- {
70+ Some ( ast:: Type :: RefType ( ascribed_type) ) if ascribed_type. lifetime ( ) . is_none ( ) => {
7471 Some ( ascribed_type. amp_token ( ) ?. text_range ( ) . end ( ) )
7572 }
7673 _ => None ,
0 commit comments