@@ -12,6 +12,7 @@ use ide_db::{
12
12
source_change:: SourceChangeBuilder ,
13
13
} ;
14
14
use itertools:: Itertools ;
15
+ use std:: fmt:: Write ;
15
16
use stdx:: { always, never} ;
16
17
use syntax:: { AstNode , SyntaxKind , SyntaxNode , TextRange , TextSize , ast} ;
17
18
@@ -459,35 +460,27 @@ fn rename_self_to_param(
459
460
}
460
461
461
462
fn 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 ( ": " ) ;
468
465
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
+ }
481
475
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 ( '_' ) ;
490
481
}
482
+
483
+ Some ( TextEdit :: replace ( self_param. syntax ( ) . text_range ( ) , replacement_text) )
491
484
}
492
485
493
486
#[ cfg( test) ]
@@ -2069,7 +2062,7 @@ impl Foo {
2069
2062
struct Foo { i: i32 }
2070
2063
2071
2064
impl Foo {
2072
- fn f(foo: &mut Foo ) -> i32 {
2065
+ fn f(foo: &mut Self ) -> i32 {
2073
2066
foo.i
2074
2067
}
2075
2068
}
@@ -2095,7 +2088,33 @@ impl Foo {
2095
2088
struct Foo { i: i32 }
2096
2089
2097
2090
impl 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 {
2099
2118
foo.i
2100
2119
}
2101
2120
}
@@ -2159,7 +2178,7 @@ impl Foo {
2159
2178
struct Foo { i: i32 }
2160
2179
2161
2180
impl Foo {
2162
- fn f(foo: &Foo ) -> i32 {
2181
+ fn f(foo: &Self ) -> i32 {
2163
2182
let self_var = 1;
2164
2183
foo.i
2165
2184
}
0 commit comments