@@ -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,22 @@ 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) ?;
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
+ }
472
475
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 ( ) ) ;
476
+ replacement_text. push_str ( "Self" ) ;
481
477
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
- }
490
- }
478
+ Some ( TextEdit :: replace ( self_param. syntax ( ) . text_range ( ) , replacement_text) )
491
479
}
492
480
493
481
#[ cfg( test) ]
@@ -2069,7 +2057,7 @@ impl Foo {
2069
2057
struct Foo { i: i32 }
2070
2058
2071
2059
impl Foo {
2072
- fn f(foo: &mut Foo ) -> i32 {
2060
+ fn f(foo: &mut Self ) -> i32 {
2073
2061
foo.i
2074
2062
}
2075
2063
}
@@ -2095,7 +2083,33 @@ impl Foo {
2095
2083
struct Foo { i: i32 }
2096
2084
2097
2085
impl Foo {
2098
- fn f(foo: Foo) -> i32 {
2086
+ fn f(foo: Self) -> i32 {
2087
+ foo.i
2088
+ }
2089
+ }
2090
+ "# ,
2091
+ ) ;
2092
+ }
2093
+
2094
+ #[ test]
2095
+ fn test_owned_self_to_parameter_with_lifetime ( ) {
2096
+ cov_mark:: check!( rename_self_to_param) ;
2097
+ check (
2098
+ "foo" ,
2099
+ r#"
2100
+ struct Foo<'a> { i: &'a i32 }
2101
+
2102
+ impl<'a> Foo<'a> {
2103
+ fn f(&'a $0self) -> i32 {
2104
+ self.i
2105
+ }
2106
+ }
2107
+ "# ,
2108
+ r#"
2109
+ struct Foo<'a> { i: &'a i32 }
2110
+
2111
+ impl<'a> Foo<'a> {
2112
+ fn f(foo: &'a Self) -> i32 {
2099
2113
foo.i
2100
2114
}
2101
2115
}
@@ -2105,7 +2119,6 @@ impl Foo {
2105
2119
2106
2120
#[ test]
2107
2121
fn test_self_outside_of_methods ( ) {
2108
- cov_mark:: check!( rename_self_outside_of_methods) ;
2109
2122
check (
2110
2123
"foo" ,
2111
2124
r#"
@@ -2114,7 +2127,7 @@ fn f($0self) -> i32 {
2114
2127
}
2115
2128
"# ,
2116
2129
r#"
2117
- fn f(foo: _ ) -> i32 {
2130
+ fn f(foo: Self ) -> i32 {
2118
2131
foo.i
2119
2132
}
2120
2133
"# ,
@@ -2159,7 +2172,7 @@ impl Foo {
2159
2172
struct Foo { i: i32 }
2160
2173
2161
2174
impl Foo {
2162
- fn f(foo: &Foo ) -> i32 {
2175
+ fn f(foo: &Self ) -> i32 {
2163
2176
let self_var = 1;
2164
2177
foo.i
2165
2178
}
0 commit comments