From ce8121bd65daecd9e92371c244dd1ac2b0e5ecda Mon Sep 17 00:00:00 2001 From: Matt Niemeir Date: Mon, 9 Mar 2020 21:34:33 -0500 Subject: Renaming a local renames struct field shorthand --- crates/ra_ide/src/references/rename.rs | 48 ++++++++++++++++++++++++++++++---- crates/ra_ide_db/src/search.rs | 24 ++++++++++------- 2 files changed, 58 insertions(+), 14 deletions(-) (limited to 'crates') diff --git a/crates/ra_ide/src/references/rename.rs b/crates/ra_ide/src/references/rename.rs index 794a109f3..1fca6de1f 100644 --- a/crates/ra_ide/src/references/rename.rs +++ b/crates/ra_ide/src/references/rename.rs @@ -48,17 +48,26 @@ fn find_name_and_module_at_offset( } fn source_edit_from_reference(reference: Reference, new_name: &str) -> SourceFileEdit { - let mut replacement_text = String::from(new_name); + let mut replacement_text = String::new(); let file_id = reference.file_range.file_id; let range = match reference.kind { - ReferenceKind::StructFieldShorthand => { + ReferenceKind::StructFieldShorthandForField => { + replacement_text.push_str(new_name); replacement_text.push_str(": "); TextRange::from_to( reference.file_range.range.start(), reference.file_range.range.start(), ) } - _ => reference.file_range.range, + ReferenceKind::StructFieldShorthandForLocal => { + replacement_text.push_str(": "); + replacement_text.push_str(new_name); + TextRange::from_to(reference.file_range.range.end(), reference.file_range.range.end()) + } + _ => { + replacement_text.push_str(new_name); + reference.file_range.range + } }; SourceFileEdit { file_id, edit: TextEdit::replace(range, replacement_text) } } @@ -286,7 +295,7 @@ mod tests { } #[test] - fn test_rename_for_struct_field() { + fn test_rename_struct_field() { test_rename( r#" struct Foo { @@ -315,7 +324,7 @@ mod tests { } #[test] - fn test_rename_for_struct_field_shorthand() { + fn test_rename_struct_field_for_shorthand() { test_rename( r#" struct Foo { @@ -343,6 +352,35 @@ mod tests { ); } + #[test] + fn test_rename_local_for_field_shorthand() { + test_rename( + r#" + struct Foo { + i: i32, + } + + impl Foo { + fn new(i<|>: i32) -> Self { + Self { i } + } + } + "#, + "j", + r#" + struct Foo { + i: i32, + } + + impl Foo { + fn new(j: i32) -> Self { + Self { i: j } + } + } + "#, + ); + } + #[test] fn test_rename_mod() { let (analysis, position) = analysis_and_position( diff --git a/crates/ra_ide_db/src/search.rs b/crates/ra_ide_db/src/search.rs index 9436a7562..b843b5b57 100644 --- a/crates/ra_ide_db/src/search.rs +++ b/crates/ra_ide_db/src/search.rs @@ -30,7 +30,8 @@ pub struct Reference { #[derive(Debug, Clone, PartialEq)] pub enum ReferenceKind { - StructFieldShorthand, + StructFieldShorthandForField, + StructFieldShorthandForLocal, StructLiteral, Other, } @@ -238,8 +239,8 @@ impl Definition { // FIXME: reuse sb // See https://github.com/rust-lang/rust/pull/68198#issuecomment-574269098 - match (classify_name_ref(&sema, &name_ref), self) { - (Some(NameRefClass::Definition(def)), _) if &def == self => { + match classify_name_ref(&sema, &name_ref) { + Some(NameRefClass::Definition(def)) if &def == self => { let kind = if is_record_lit_name_ref(&name_ref) || is_call_expr_name_ref(&name_ref) { @@ -255,14 +256,19 @@ impl Definition { access: reference_access(&def, &name_ref), }); } - ( - Some(NameRefClass::FieldShorthand { local, field: _ }), - Definition::StructField(_), - ) => { + Some(NameRefClass::FieldShorthand { local, field: _ }) => { + let kind = match self { + Definition::StructField(_) => { + ReferenceKind::StructFieldShorthandForField + } + Definition::Local(_) => ReferenceKind::StructFieldShorthandForLocal, + _ => continue, + }; + let file_range = sema.original_range(name_ref.syntax()); refs.push(Reference { - file_range: file_range, - kind: ReferenceKind::StructFieldShorthand, + file_range, + kind, access: reference_access(&Definition::Local(local), &name_ref), }); } -- cgit v1.2.3