@@ -12,6 +12,7 @@ use ide_db::{
1212 source_change:: SourceChangeBuilder ,
1313} ;
1414use itertools:: Itertools ;
15+ use std:: fmt:: Write ;
1516use stdx:: { always, never} ;
1617use syntax:: { AstNode , SyntaxKind , SyntaxNode , TextRange , TextSize , ast} ;
1718
@@ -459,35 +460,27 @@ fn rename_self_to_param(
459460}
460461
461462fn text_edit_from_self_param ( self_param : & ast:: SelfParam , new_name : String ) -> Option < TextEdit > {
462- fn target_type_name ( impl_def : & ast:: Impl ) -> Option < String > {
463- if let Some ( ast:: Type :: PathType ( p) ) = impl_def. self_ty ( ) {
464- return Some ( p. path ( ) ?. segment ( ) ?. name_ref ( ) ?. text ( ) . to_string ( ) ) ;
465- }
466- None
467- }
463+ let mut replacement_text = new_name;
464+ replacement_text. push_str ( ": " ) ;
468465
469- match self_param. syntax ( ) . ancestors ( ) . find_map ( ast:: Impl :: cast) {
470- Some ( impl_def) => {
471- let type_name = target_type_name ( & impl_def) ?;
472-
473- let mut replacement_text = new_name;
474- replacement_text. push_str ( ": " ) ;
475- match ( self_param. amp_token ( ) , self_param. mut_token ( ) ) {
476- ( Some ( _) , None ) => replacement_text. push ( '&' ) ,
477- ( Some ( _) , Some ( _) ) => replacement_text. push_str ( "&mut " ) ,
478- ( _, _) => ( ) ,
479- } ;
480- replacement_text. push_str ( type_name. as_str ( ) ) ;
466+ if self_param. amp_token ( ) . is_some ( ) {
467+ replacement_text. push ( '&' ) ;
468+ }
469+ if let Some ( lifetime) = self_param. lifetime ( ) {
470+ write ! ( replacement_text, "{lifetime} " ) . unwrap ( ) ;
471+ }
472+ if self_param. amp_token ( ) . and ( self_param. mut_token ( ) ) . is_some ( ) {
473+ replacement_text. push_str ( "mut " ) ;
474+ }
481475
482- Some ( TextEdit :: replace ( self_param. syntax ( ) . text_range ( ) , replacement_text) )
483- }
484- None => {
485- cov_mark:: hit!( rename_self_outside_of_methods) ;
486- let mut replacement_text = new_name;
487- replacement_text. push_str ( ": _" ) ;
488- Some ( TextEdit :: replace ( self_param. syntax ( ) . text_range ( ) , replacement_text) )
489- }
476+ if self_param. syntax ( ) . ancestors ( ) . find_map ( ast:: Impl :: cast) . is_some ( ) {
477+ replacement_text. push_str ( "Self" ) ;
478+ } else {
479+ cov_mark:: hit!( rename_self_outside_of_methods) ;
480+ replacement_text. push ( '_' ) ;
490481 }
482+
483+ Some ( TextEdit :: replace ( self_param. syntax ( ) . text_range ( ) , replacement_text) )
491484}
492485
493486#[ cfg( test) ]
@@ -2069,7 +2062,7 @@ impl Foo {
20692062struct Foo { i: i32 }
20702063
20712064impl Foo {
2072- fn f(foo: &mut Foo ) -> i32 {
2065+ fn f(foo: &mut Self ) -> i32 {
20732066 foo.i
20742067 }
20752068}
@@ -2095,7 +2088,33 @@ impl Foo {
20952088struct Foo { i: i32 }
20962089
20972090impl Foo {
2098- fn f(foo: Foo) -> i32 {
2091+ fn f(foo: Self) -> i32 {
2092+ foo.i
2093+ }
2094+ }
2095+ "# ,
2096+ ) ;
2097+ }
2098+
2099+ #[ test]
2100+ fn test_owned_self_to_parameter_with_lifetime ( ) {
2101+ cov_mark:: check!( rename_self_to_param) ;
2102+ check (
2103+ "foo" ,
2104+ r#"
2105+ struct Foo<'a> { i: &'a i32 }
2106+
2107+ impl<'a> Foo<'a> {
2108+ fn f(&'a $0self) -> i32 {
2109+ self.i
2110+ }
2111+ }
2112+ "# ,
2113+ r#"
2114+ struct Foo<'a> { i: &'a i32 }
2115+
2116+ impl<'a> Foo<'a> {
2117+ fn f(foo: &'a Self) -> i32 {
20992118 foo.i
21002119 }
21012120}
@@ -2159,7 +2178,7 @@ impl Foo {
21592178struct Foo { i: i32 }
21602179
21612180impl Foo {
2162- fn f(foo: &Foo ) -> i32 {
2181+ fn f(foo: &Self ) -> i32 {
21632182 let self_var = 1;
21642183 foo.i
21652184 }
0 commit comments