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') 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 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') 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 From 13ccbb2919dff8e98d0a242d9d6b7edd17a6bd2c Mon Sep 17 00:00:00 2001 From: Matt Niemeir Date: Tue, 10 Mar 2020 22:27:38 -0500 Subject: find_usages limited to actual usages again --- crates/ra_ide/src/references/rename.rs | 70 ++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) (limited to 'crates/ra_ide') diff --git a/crates/ra_ide/src/references/rename.rs b/crates/ra_ide/src/references/rename.rs index 1fca6de1f..7d1190af9 100644 --- a/crates/ra_ide/src/references/rename.rs +++ b/crates/ra_ide/src/references/rename.rs @@ -381,6 +381,76 @@ mod tests { ); } + #[test] + fn test_field_shorthand_correct_struct() { + test_rename( + r#" + struct Foo { + i<|>: i32, + } + + struct Bar { + i: i32, + } + + impl Bar { + fn new(i: i32) -> Self { + Self { i } + } + } + "#, + "j", + r#" + struct Foo { + j: i32, + } + + struct Bar { + i: i32, + } + + impl Bar { + fn new(i: i32) -> Self { + Self { i } + } + } + "#, + ); + } + + #[test] + fn test_shadow_local_for_struct_shorthand() { + test_rename( + r#" + struct Foo { + i: i32, + } + + fn baz(i<|>: i32) -> Self { + let x = Foo { i }; + { + let i = 0; + Foo { i } + } + } + "#, + "j", + r#" + struct Foo { + i: i32, + } + + fn baz(j: i32) -> Self { + let x = Foo { i: j }; + { + let i = 0; + Foo { i } + } + } + "#, + ); + } + #[test] fn test_rename_mod() { let (analysis, position) = analysis_and_position( -- cgit v1.2.3