From a9b6aec8a788d33ebe9667d53d127be59d93e555 Mon Sep 17 00:00:00 2001 From: Matt Niemeir Date: Mon, 9 Mar 2020 21:18:55 -0500 Subject: Struct field rename renames field in constructor field shorthand --- crates/ra_ide/src/references/rename.rs | 100 ++++++++++++++++++++++++++------- 1 file changed, 79 insertions(+), 21 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 5b4bcf434..794a109f3 100644 --- a/crates/ra_ide/src/references/rename.rs +++ b/crates/ra_ide/src/references/rename.rs @@ -9,7 +9,8 @@ use ra_syntax::{ use ra_text_edit::TextEdit; use crate::{ - FileId, FilePosition, FileSystemEdit, RangeInfo, SourceChange, SourceFileEdit, TextRange, + FilePosition, FileSystemEdit, RangeInfo, Reference, ReferenceKind, SourceChange, + SourceFileEdit, TextRange, }; use super::find_all_refs; @@ -46,12 +47,20 @@ fn find_name_and_module_at_offset( Some((ast_name, ast_module)) } -fn source_edit_from_file_id_range( - file_id: FileId, - range: TextRange, - new_name: &str, -) -> SourceFileEdit { - SourceFileEdit { file_id, edit: TextEdit::replace(range, new_name.into()) } +fn source_edit_from_reference(reference: Reference, new_name: &str) -> SourceFileEdit { + let mut replacement_text = String::from(new_name); + let file_id = reference.file_range.file_id; + let range = match reference.kind { + ReferenceKind::StructFieldShorthand => { + replacement_text.push_str(": "); + TextRange::from_to( + reference.file_range.range.start(), + reference.file_range.range.start(), + ) + } + _ => reference.file_range.range, + }; + SourceFileEdit { file_id, edit: TextEdit::replace(range, replacement_text) } } fn rename_mod( @@ -99,13 +108,10 @@ fn rename_mod( source_file_edits.push(edit); if let Some(RangeInfo { range: _, info: refs }) = find_all_refs(sema.db, position, None) { - let ref_edits = refs.references.into_iter().map(|reference| { - source_edit_from_file_id_range( - reference.file_range.file_id, - reference.file_range.range, - new_name, - ) - }); + let ref_edits = refs + .references + .into_iter() + .map(|reference| source_edit_from_reference(reference, new_name)); source_file_edits.extend(ref_edits); } @@ -121,13 +127,7 @@ fn rename_reference( let edit = refs .into_iter() - .map(|reference| { - source_edit_from_file_id_range( - reference.file_range.file_id, - reference.file_range.range, - new_name, - ) - }) + .map(|reference| source_edit_from_reference(reference, new_name)) .collect::>(); if edit.is_empty() { @@ -285,6 +285,64 @@ mod tests { ); } + #[test] + fn test_rename_for_struct_field() { + test_rename( + r#" + struct Foo { + i<|>: i32, + } + + impl Foo { + fn new(i: i32) -> Self { + Self { i: i } + } + } + "#, + "j", + r#" + struct Foo { + j: i32, + } + + impl Foo { + fn new(i: i32) -> Self { + Self { j: i } + } + } + "#, + ); + } + + #[test] + fn test_rename_for_struct_field_shorthand() { + test_rename( + r#" + struct Foo { + i<|>: i32, + } + + impl Foo { + fn new(i: i32) -> Self { + Self { i } + } + } + "#, + "j", + r#" + struct Foo { + j: i32, + } + + impl Foo { + fn new(i: i32) -> Self { + Self { j: i } + } + } + "#, + ); + } + #[test] fn test_rename_mod() { let (analysis, position) = analysis_and_position( -- cgit v1.2.3