1
1
use rustc_hash:: FxHashSet ;
2
2
use syntax:: {
3
3
ast:: { self , GenericParamsOwner , NameOwner } ,
4
- AstNode , SyntaxKind , TextRange , TextSize ,
4
+ AstNode , TextRange , TextSize ,
5
5
} ;
6
6
7
7
use crate :: { assist_context:: AssistBuilder , AssistContext , AssistId , AssistKind , Assists } ;
@@ -35,13 +35,12 @@ static ASSIST_LABEL: &str = "Introduce named lifetime";
35
35
// FIXME: How can we handle renaming any one of multiple anonymous lifetimes?
36
36
// FIXME: should also add support for the case fun(f: &Foo) -> &<|>Foo
37
37
pub ( 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 ( ) )
45
44
} else {
46
45
None
47
46
}
@@ -58,7 +57,7 @@ fn generate_fn_def_assist(
58
57
let end_of_fn_ident = fn_def. name ( ) ?. ident_token ( ) ?. text_range ( ) . end ( ) ;
59
58
let self_param =
60
59
// 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 ( ) ) ;
62
61
// compute the location which implicitly has the same lifetime as the anonymous lifetime
63
62
let loc_needing_lifetime = if let Some ( self_param) = self_param {
64
63
// if we have a self reference, use that
@@ -68,9 +67,7 @@ fn generate_fn_def_assist(
68
67
let fn_params_without_lifetime: Vec < _ > = param_list
69
68
. params ( )
70
69
. 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 ( ) => {
74
71
Some ( ascribed_type. amp_token ( ) ?. text_range ( ) . end ( ) )
75
72
}
76
73
_ => None ,
0 commit comments