Skip to content

Commit 1bf93c3

Browse files
Merge pull request #20285 from A4-Tacks/fix-rename-self
Change rename self to parameter use `Self` type
2 parents 97593ea + 9ecd82b commit 1bf93c3

File tree

1 file changed

+44
-31
lines changed

1 file changed

+44
-31
lines changed

crates/ide/src/rename.rs

Lines changed: 44 additions & 31 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,22 @@ 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)?;
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+
}
472475

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");
481477

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))
491479
}
492480

493481
#[cfg(test)]
@@ -2069,7 +2057,7 @@ impl Foo {
20692057
struct Foo { i: i32 }
20702058
20712059
impl Foo {
2072-
fn f(foo: &mut Foo) -> i32 {
2060+
fn f(foo: &mut Self) -> i32 {
20732061
foo.i
20742062
}
20752063
}
@@ -2095,7 +2083,33 @@ impl Foo {
20952083
struct Foo { i: i32 }
20962084
20972085
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 {
20992113
foo.i
21002114
}
21012115
}
@@ -2105,7 +2119,6 @@ impl Foo {
21052119

21062120
#[test]
21072121
fn test_self_outside_of_methods() {
2108-
cov_mark::check!(rename_self_outside_of_methods);
21092122
check(
21102123
"foo",
21112124
r#"
@@ -2114,7 +2127,7 @@ fn f($0self) -> i32 {
21142127
}
21152128
"#,
21162129
r#"
2117-
fn f(foo: _) -> i32 {
2130+
fn f(foo: Self) -> i32 {
21182131
foo.i
21192132
}
21202133
"#,
@@ -2159,7 +2172,7 @@ impl Foo {
21592172
struct Foo { i: i32 }
21602173
21612174
impl Foo {
2162-
fn f(foo: &Foo) -> i32 {
2175+
fn f(foo: &Self) -> i32 {
21632176
let self_var = 1;
21642177
foo.i
21652178
}

0 commit comments

Comments
 (0)