From 8b3f449b5f848b43c8bbb9aea431ce4df8090fd4 Mon Sep 17 00:00:00 2001 From: adamrk Date: Wed, 19 Feb 2020 22:51:59 +0100 Subject: rename module references --- crates/ra_ide/src/references/rename.rs | 112 +++++++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) (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 c46b78cb6..97a97c0ae 100644 --- a/crates/ra_ide/src/references/rename.rs +++ b/crates/ra_ide/src/references/rename.rs @@ -98,6 +98,23 @@ fn rename_mod( }; source_file_edits.push(edit); + if let Some(RangeInfo { range: _, info: refs }) = find_all_refs(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, + ) + }) + .collect::>(); + for ref_edit in ref_edits { + source_file_edits.push(ref_edit); + } + } + Some(SourceChange::from_edits("rename", source_file_edits, file_system_edits)) } @@ -383,6 +400,101 @@ mod tests { ); } + #[test] + fn test_module_rename_in_path() { + test_rename( + r#" + mod <|>foo { + pub fn bar() {} + } + + fn main() { + foo::bar(); + }"#, + "baz", + r#" + mod baz { + pub fn bar() {} + } + + fn main() { + baz::bar(); + }"#, + ); + } + + #[test] + fn test_rename_mod_filename_and_path() { + let (analysis, position) = analysis_and_position( + " + //- /lib.rs + mod bar; + fn f() { + bar::foo::fun() + } + + //- /bar.rs + pub mod foo<|>; + + //- /bar/foo.rs + // pub fn fun() {} + ", + ); + let new_name = "foo2"; + let source_change = analysis.rename(position, new_name).unwrap(); + assert_debug_snapshot!(&source_change, +@r###" + Some( + RangeInfo { + range: [8; 11), + info: SourceChange { + label: "rename", + source_file_edits: [ + SourceFileEdit { + file_id: FileId( + 2, + ), + edit: TextEdit { + atoms: [ + AtomTextEdit { + delete: [8; 11), + insert: "foo2", + }, + ], + }, + }, + SourceFileEdit { + file_id: FileId( + 1, + ), + edit: TextEdit { + atoms: [ + AtomTextEdit { + delete: [27; 30), + insert: "foo2", + }, + ], + }, + }, + ], + file_system_edits: [ + MoveFile { + src: FileId( + 3, + ), + dst_source_root: SourceRootId( + 0, + ), + dst_path: "bar/foo2.rs", + }, + ], + cursor_position: None, + }, + }, + ) + "###); + } + fn test_rename(text: &str, new_name: &str, expected: &str) { let (analysis, position) = single_file_with_position(text); let source_change = analysis.rename(position, new_name).unwrap(); -- cgit v1.2.3 From 7be104410468a1fdcb01e5b98bedb7438b2c99f0 Mon Sep 17 00:00:00 2001 From: Adam Bratschi-Kaye Date: Thu, 20 Feb 2020 08:35:09 +0100 Subject: use Vec::extend instead of Vec::push in loop MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Laurențiu Nicola --- crates/ra_ide/src/references/rename.rs | 4 +--- 1 file changed, 1 insertion(+), 3 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 97a97c0ae..bcb62b33d 100644 --- a/crates/ra_ide/src/references/rename.rs +++ b/crates/ra_ide/src/references/rename.rs @@ -110,9 +110,7 @@ fn rename_mod( ) }) .collect::>(); - for ref_edit in ref_edits { - source_file_edits.push(ref_edit); - } + source_file_edits.extend(ref_edits); } Some(SourceChange::from_edits("rename", source_file_edits, file_system_edits)) -- cgit v1.2.3 From fe8ce4c41d5f82ab164e76aef6c504c99fdff410 Mon Sep 17 00:00:00 2001 From: adamrk Date: Thu, 20 Feb 2020 10:34:22 +0100 Subject: remove unneeded `collect` --- crates/ra_ide/src/references/rename.rs | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 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 bcb62b33d..bdb90020b 100644 --- a/crates/ra_ide/src/references/rename.rs +++ b/crates/ra_ide/src/references/rename.rs @@ -99,17 +99,13 @@ fn rename_mod( source_file_edits.push(edit); if let Some(RangeInfo { range: _, info: refs }) = find_all_refs(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, - ) - }) - .collect::>(); + 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, + ) + }); source_file_edits.extend(ref_edits); } -- cgit v1.2.3