mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-27 13:33:31 +00:00
feat: handle self-param outside of methods when renaming
This commit is contained in:
parent
d7628c0a8b
commit
1e8a03a56e
1 changed files with 40 additions and 11 deletions
|
@ -421,7 +421,8 @@ fn text_edit_from_self_param(self_param: &ast::SelfParam, new_name: &str) -> Opt
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
let impl_def = self_param.syntax().ancestors().find_map(ast::Impl::cast)?;
|
match self_param.syntax().ancestors().find_map(ast::Impl::cast) {
|
||||||
|
Some(impl_def) => {
|
||||||
let type_name = target_type_name(&impl_def)?;
|
let type_name = target_type_name(&impl_def)?;
|
||||||
|
|
||||||
let mut replacement_text = String::from(new_name);
|
let mut replacement_text = String::from(new_name);
|
||||||
|
@ -435,6 +436,14 @@ fn text_edit_from_self_param(self_param: &ast::SelfParam, new_name: &str) -> Opt
|
||||||
|
|
||||||
Some(TextEdit::replace(self_param.syntax().text_range(), replacement_text))
|
Some(TextEdit::replace(self_param.syntax().text_range(), replacement_text))
|
||||||
}
|
}
|
||||||
|
None => {
|
||||||
|
cov_mark::hit!(rename_self_outside_of_methods);
|
||||||
|
let mut replacement_text = String::from(new_name);
|
||||||
|
replacement_text.push_str(": _");
|
||||||
|
Some(TextEdit::replace(self_param.syntax().text_range(), replacement_text))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
@ -1977,6 +1986,26 @@ impl Foo {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_self_outside_of_methods() {
|
||||||
|
cov_mark::check!(rename_self_outside_of_methods);
|
||||||
|
check(
|
||||||
|
"foo",
|
||||||
|
r#"
|
||||||
|
fn f($0self) -> i32 {
|
||||||
|
use self as _;
|
||||||
|
self.i
|
||||||
|
}
|
||||||
|
"#,
|
||||||
|
r#"
|
||||||
|
fn f(foo: _) -> i32 {
|
||||||
|
use self as _;
|
||||||
|
foo.i
|
||||||
|
}
|
||||||
|
"#,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_self_in_path_to_parameter() {
|
fn test_self_in_path_to_parameter() {
|
||||||
check(
|
check(
|
||||||
|
|
Loading…
Reference in a new issue