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 ++++++++++++++++++++++++++++++---- 1 file changed, 43 insertions(+), 5 deletions(-) (limited to 'crates/ra_ide/src') 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( -- cgit v1.2.3