Skip to content

Commit 4b32a49

Browse files
committed
Change rename self to parameter use Self type
And add `&self` lifetime support Example === Rename to `this` ```rust struct Foo<T>(T); impl Foo<i32> { fn foo(&'static self$0) {} } ``` Old: ```rust struct Foo<T>(T); impl Foo<i32> { fn foo(this: &Foo) {} } ``` Fixes: ```rust struct Foo<T>(T); impl Foo<i32> { fn foo(this: &'static Self) {} } ```
1 parent a54351e commit 4b32a49

File tree

1 file changed

+48
-29
lines changed

1 file changed

+48
-29
lines changed

crates/ide/src/rename.rs

Lines changed: 48 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use ide_db::{
1212
source_change::SourceChangeBuilder,
1313
};
1414
use itertools::Itertools;
15+
use std::fmt::Write;
1516
use stdx::{always, never};
1617
use syntax::{AstNode, SyntaxKind, SyntaxNode, TextRange, TextSize, ast};
1718

@@ -459,35 +460,27 @@ fn rename_self_to_param(
459460
}
460461

461462
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(": ");
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 {
20692062
struct Foo { i: i32 }
20702063
20712064
impl 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 {
20952088
struct Foo { i: i32 }
20962089
20972090
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 {
20992118
foo.i
21002119
}
21012120
}
@@ -2159,7 +2178,7 @@ impl Foo {
21592178
struct Foo { i: i32 }
21602179
21612180
impl 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

Comments
 (0)