mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-14 06:03:58 +00:00
Handle all rename special cases for all record pattern fields
This commit is contained in:
parent
6a07bf6a9f
commit
5e533e5900
4 changed files with 112 additions and 23 deletions
|
@ -1332,9 +1332,71 @@ fn foo(foo: Foo) {
|
||||||
struct Foo { baz: i32 }
|
struct Foo { baz: i32 }
|
||||||
|
|
||||||
fn foo(foo: Foo) {
|
fn foo(foo: Foo) {
|
||||||
let Foo { ref baz @ qux } = foo;
|
let Foo { baz: ref baz @ qux } = foo;
|
||||||
let _ = qux;
|
let _ = qux;
|
||||||
}
|
}
|
||||||
|
"#,
|
||||||
|
);
|
||||||
|
check(
|
||||||
|
"baz",
|
||||||
|
r#"
|
||||||
|
struct Foo { i$0: i32 }
|
||||||
|
|
||||||
|
fn foo(foo: Foo) {
|
||||||
|
let Foo { i: ref baz } = foo;
|
||||||
|
let _ = qux;
|
||||||
|
}
|
||||||
|
"#,
|
||||||
|
r#"
|
||||||
|
struct Foo { baz: i32 }
|
||||||
|
|
||||||
|
fn foo(foo: Foo) {
|
||||||
|
let Foo { ref baz } = foo;
|
||||||
|
let _ = qux;
|
||||||
|
}
|
||||||
|
"#,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_struct_local_pat_into_shorthand() {
|
||||||
|
cov_mark::check!(test_rename_local_put_init_shorthand_pat);
|
||||||
|
check(
|
||||||
|
"field",
|
||||||
|
r#"
|
||||||
|
struct Foo { field: i32 }
|
||||||
|
|
||||||
|
fn foo(foo: Foo) {
|
||||||
|
let Foo { field: qux$0 } = foo;
|
||||||
|
let _ = qux;
|
||||||
|
}
|
||||||
|
"#,
|
||||||
|
r#"
|
||||||
|
struct Foo { field: i32 }
|
||||||
|
|
||||||
|
fn foo(foo: Foo) {
|
||||||
|
let Foo { field } = foo;
|
||||||
|
let _ = field;
|
||||||
|
}
|
||||||
|
"#,
|
||||||
|
);
|
||||||
|
check(
|
||||||
|
"field",
|
||||||
|
r#"
|
||||||
|
struct Foo { field: i32 }
|
||||||
|
|
||||||
|
fn foo(foo: Foo) {
|
||||||
|
let Foo { field: x @ qux$0 } = foo;
|
||||||
|
let _ = qux;
|
||||||
|
}
|
||||||
|
"#,
|
||||||
|
r#"
|
||||||
|
struct Foo { field: i32 }
|
||||||
|
|
||||||
|
fn foo(foo: Foo) {
|
||||||
|
let Foo { field: x @ field } = foo;
|
||||||
|
let _ = field;
|
||||||
|
}
|
||||||
"#,
|
"#,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -1390,7 +1452,7 @@ struct Foo {
|
||||||
i: i32
|
i: i32
|
||||||
}
|
}
|
||||||
|
|
||||||
fn foo(Foo { i }: foo) -> i32 {
|
fn foo(Foo { i }: Foo) -> i32 {
|
||||||
i$0
|
i$0
|
||||||
}
|
}
|
||||||
"#,
|
"#,
|
||||||
|
@ -1399,7 +1461,7 @@ struct Foo {
|
||||||
i: i32
|
i: i32
|
||||||
}
|
}
|
||||||
|
|
||||||
fn foo(Foo { i: bar }: foo) -> i32 {
|
fn foo(Foo { i: bar }: Foo) -> i32 {
|
||||||
bar
|
bar
|
||||||
}
|
}
|
||||||
"#,
|
"#,
|
||||||
|
|
|
@ -120,11 +120,11 @@ impl ImportAssets {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn for_ident_pat(pat: &ast::IdentPat, sema: &Semantics<RootDatabase>) -> Option<Self> {
|
pub fn for_ident_pat(pat: &ast::IdentPat, sema: &Semantics<RootDatabase>) -> Option<Self> {
|
||||||
let name = pat.name()?;
|
|
||||||
let candidate_node = pat.syntax().clone();
|
|
||||||
if !pat.is_simple_ident() {
|
if !pat.is_simple_ident() {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
let name = pat.name()?;
|
||||||
|
let candidate_node = pat.syntax().clone();
|
||||||
Some(Self {
|
Some(Self {
|
||||||
import_candidate: ImportCandidate::for_name(sema, &name)?,
|
import_candidate: ImportCandidate::for_name(sema, &name)?,
|
||||||
module_with_candidate: sema.scope(&candidate_node).module()?,
|
module_with_candidate: sema.scope(&candidate_node).module()?,
|
||||||
|
|
|
@ -320,6 +320,7 @@ pub fn source_edit_from_references(
|
||||||
.unwrap_or_else(|| (reference.range, new_name.to_string()));
|
.unwrap_or_else(|| (reference.range, new_name.to_string()));
|
||||||
edit.replace(range, replacement);
|
edit.replace(range, replacement);
|
||||||
}
|
}
|
||||||
|
|
||||||
edit.finish()
|
edit.finish()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -334,6 +335,7 @@ fn source_edit_from_name(name: &ast::Name, new_name: &str) -> Option<(TextRange,
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -387,7 +389,9 @@ fn source_edit_from_name_ref(
|
||||||
let rcf_pat = record_field.pat();
|
let rcf_pat = record_field.pat();
|
||||||
match (rcf_name_ref, rcf_pat) {
|
match (rcf_name_ref, rcf_pat) {
|
||||||
// field: rename
|
// field: rename
|
||||||
(Some(field_name), Some(ast::Pat::IdentPat(pat))) if field_name == *name_ref => {
|
(Some(field_name), Some(ast::Pat::IdentPat(pat)))
|
||||||
|
if field_name == *name_ref && pat.at_token().is_none() =>
|
||||||
|
{
|
||||||
// field name is being renamed
|
// field name is being renamed
|
||||||
if pat.name().map_or(false, |it| it.text() == new_name) {
|
if pat.name().map_or(false, |it| it.text() == new_name) {
|
||||||
cov_mark::hit!(test_rename_field_put_init_shorthand_pat);
|
cov_mark::hit!(test_rename_field_put_init_shorthand_pat);
|
||||||
|
@ -412,32 +416,52 @@ fn source_edit_from_def(
|
||||||
def: Definition,
|
def: Definition,
|
||||||
new_name: &str,
|
new_name: &str,
|
||||||
) -> Result<(FileId, TextEdit)> {
|
) -> Result<(FileId, TextEdit)> {
|
||||||
let frange = def
|
let FileRange { file_id, range } = def
|
||||||
.range_for_rename(sema)
|
.range_for_rename(sema)
|
||||||
.ok_or_else(|| format_err!("No identifier available to rename"))?;
|
.ok_or_else(|| format_err!("No identifier available to rename"))?;
|
||||||
|
|
||||||
let mut replacement_text = String::new();
|
let mut edit = TextEdit::builder();
|
||||||
let mut repl_range = frange.range;
|
|
||||||
if let Definition::Local(local) = def {
|
if let Definition::Local(local) = def {
|
||||||
if let Either::Left(pat) = local.source(sema.db).value {
|
if let Either::Left(pat) = local.source(sema.db).value {
|
||||||
if matches!(
|
// special cases required for renaming fields/locals in Record patterns
|
||||||
pat.syntax().parent().and_then(ast::RecordPatField::cast),
|
if let Some(pat_field) = pat.syntax().parent().and_then(ast::RecordPatField::cast) {
|
||||||
Some(pat_field) if pat_field.name_ref().is_none()
|
let name_range = pat.name().unwrap().syntax().text_range();
|
||||||
) {
|
if let Some(name_ref) = pat_field.name_ref() {
|
||||||
replacement_text.push_str(": ");
|
if new_name == name_ref.text() && pat.at_token().is_none() {
|
||||||
replacement_text.push_str(new_name);
|
// Foo { field: ref mut local } -> Foo { ref mut field }
|
||||||
repl_range = TextRange::new(
|
// ^^^^^^ delete this
|
||||||
pat.syntax().text_range().end(),
|
// ^^^^^ replace this with `field`
|
||||||
pat.syntax().text_range().end(),
|
cov_mark::hit!(test_rename_local_put_init_shorthand_pat);
|
||||||
);
|
edit.delete(
|
||||||
|
name_ref
|
||||||
|
.syntax()
|
||||||
|
.text_range()
|
||||||
|
.cover_offset(pat.syntax().text_range().start()),
|
||||||
|
);
|
||||||
|
edit.replace(name_range, name_ref.text().to_string());
|
||||||
|
} else {
|
||||||
|
// Foo { field: ref mut local @ local 2} -> Foo { field: ref mut new_name @ local2 }
|
||||||
|
// Foo { field: ref mut local } -> Foo { field: ref mut new_name }
|
||||||
|
// ^^^^^ replace this with `new_name`
|
||||||
|
edit.replace(name_range, new_name.to_string());
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Foo { ref mut field } -> Foo { field: ref mut new_name }
|
||||||
|
// ^ insert `field: `
|
||||||
|
// ^^^^^ replace this with `new_name`
|
||||||
|
edit.insert(
|
||||||
|
pat.syntax().text_range().start(),
|
||||||
|
format!("{}: ", pat_field.field_name().unwrap()),
|
||||||
|
);
|
||||||
|
edit.replace(name_range, new_name.to_string());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if replacement_text.is_empty() {
|
if edit.is_empty() {
|
||||||
replacement_text.push_str(new_name);
|
edit.replace(range, new_name.to_string());
|
||||||
}
|
}
|
||||||
let edit = TextEdit::replace(repl_range, replacement_text);
|
Ok((file_id, edit.finish()))
|
||||||
Ok((frange.file_id, edit))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Copy, Clone, Debug, PartialEq)]
|
#[derive(Copy, Clone, Debug, PartialEq)]
|
||||||
|
|
|
@ -159,6 +159,9 @@ impl<'a> IntoIterator for &'a TextEdit {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TextEditBuilder {
|
impl TextEditBuilder {
|
||||||
|
pub fn is_empty(&self) -> bool {
|
||||||
|
self.indels.is_empty()
|
||||||
|
}
|
||||||
pub fn replace(&mut self, range: TextRange, replace_with: String) {
|
pub fn replace(&mut self, range: TextRange, replace_with: String) {
|
||||||
self.indel(Indel::replace(range, replace_with))
|
self.indel(Indel::replace(range, replace_with))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue