From c402b007a3272efada7924fce4eecf698b98968b Mon Sep 17 00:00:00 2001 From: DJMcNab <36049421+DJMcNab@users.noreply.github.com> Date: Sun, 30 Dec 2018 18:14:55 +0000 Subject: Move renames into ra_analysis --- crates/ra_analysis/src/imp.rs | 26 ++++++++++++++++++++++++-- crates/ra_analysis/src/lib.rs | 7 +++++++ crates/ra_lsp_server/src/main_loop/handlers.rs | 21 ++++++++------------- 3 files changed, 39 insertions(+), 15 deletions(-) (limited to 'crates') diff --git a/crates/ra_analysis/src/imp.rs b/crates/ra_analysis/src/imp.rs index bff2e00c9..5ed374c79 100644 --- a/crates/ra_analysis/src/imp.rs +++ b/crates/ra_analysis/src/imp.rs @@ -288,7 +288,11 @@ impl AnalysisImpl { Some(it) => it, }; - let mut ret = vec![(position.file_id, binding.syntax().range())]; + let mut ret = binding + .name() + .into_iter() + .map(|name| (position.file_id, name.syntax().range())) + .collect::>(); ret.extend( descr .scopes(&*self.db) @@ -505,7 +509,25 @@ impl AnalysisImpl { let infer = function.infer(&*self.db)?; Ok(infer.type_of_node(node).map(|t| t.to_string())) } - + pub fn rename( + &self, + position: FilePosition, + new_name: &str, + ) -> Cancelable> { + let res = self + .find_all_refs(position)? + .iter() + .map(|(file_id, text_range)| SourceFileEdit { + file_id: *file_id, + edit: { + let mut builder = ra_text_edit::TextEditBuilder::new(); + builder.replace(*text_range, new_name.into()); + builder.finish() + }, + }) + .collect::>(); + Ok(res) + } fn index_resolve(&self, name_ref: ast::NameRef) -> Cancelable> { let name = name_ref.text(); let mut query = Query::new(name.to_string()); diff --git a/crates/ra_analysis/src/lib.rs b/crates/ra_analysis/src/lib.rs index 9f5e9f358..e56168510 100644 --- a/crates/ra_analysis/src/lib.rs +++ b/crates/ra_analysis/src/lib.rs @@ -367,6 +367,13 @@ impl Analysis { pub fn type_of(&self, frange: FileRange) -> Cancelable> { self.imp.type_of(frange) } + pub fn rename( + &self, + position: FilePosition, + new_name: &str, + ) -> Cancelable> { + self.imp.rename(position, new_name) + } } pub struct LibraryData { diff --git a/crates/ra_lsp_server/src/main_loop/handlers.rs b/crates/ra_lsp_server/src/main_loop/handlers.rs index a2c12a4c1..3b7a14a5c 100644 --- a/crates/ra_lsp_server/src/main_loop/handlers.rs +++ b/crates/ra_lsp_server/src/main_loop/handlers.rs @@ -557,24 +557,19 @@ pub fn handle_rename(world: ServerWorld, params: RenameParams) -> Result Date: Sun, 30 Dec 2018 20:43:30 +0000 Subject: Test renames --- crates/ra_analysis/tests/tests.rs | 93 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 92 insertions(+), 1 deletion(-) (limited to 'crates') diff --git a/crates/ra_analysis/tests/tests.rs b/crates/ra_analysis/tests/tests.rs index 210fa2a13..a314fbc40 100644 --- a/crates/ra_analysis/tests/tests.rs +++ b/crates/ra_analysis/tests/tests.rs @@ -1,5 +1,5 @@ use ra_syntax::TextRange; -use test_utils::assert_eq_dbg; +use test_utils::{assert_eq_dbg, assert_eq_text}; use ra_analysis::{ mock_analysis::{analysis_and_position, single_file, single_file_with_position, MockAnalysis}, @@ -453,3 +453,94 @@ fn test_find_all_refs_for_fn_param() { let refs = get_all_refs(code); assert_eq!(refs.len(), 2); } +#[test] +fn test_rename_for_local() { + test_rename( + r#" + fn main() { + let mut i = 1; + let j = 1; + i = i<|> + j; + + { + i = 0; + } + + i = 5; + }"#, + "k", + r#" + fn main() { + let mut k = 1; + let j = 1; + k = k + j; + + { + k = 0; + } + + k = 5; + }"#, + ); +} + +#[test] +fn test_rename_for_param_inside() { + test_rename( + r#" + fn foo(i : u32) -> u32 { + i<|> + }"#, + "j", + r#" + fn foo(j : u32) -> u32 { + j + }"#, + ); +} + +#[test] +fn test_rename_refs_for_fn_param() { + test_rename( + r#" + fn foo(i<|> : u32) -> u32 { + i + }"#, + "new_name", + r#" + fn foo(new_name : u32) -> u32 { + new_name + }"#, + ); +} + +#[test] +fn test_rename_for_mut_param() { + test_rename( + r#" + fn foo(mut i<|> : u32) -> u32 { + i + }"#, + "new_name", + r#" + fn foo(mut new_name : u32) -> u32 { + new_name + }"#, + ); +} +fn test_rename(text: &str, new_name: &str, expected: &str) { + let (analysis, position) = single_file_with_position(text); + let edits = analysis.rename(position, new_name).unwrap(); + let mut text_edit_bulder = ra_text_edit::TextEditBuilder::new(); + let mut file_id: Option = None; + for edit in edits { + file_id = Some(edit.file_id); + for atom in edit.edit.as_atoms() { + text_edit_bulder.replace(atom.delete, atom.insert.clone()); + } + } + let result = text_edit_bulder + .finish() + .apply(&*analysis.file_text(file_id.unwrap())); + assert_eq_text!(expected, &*result); +} -- cgit v1.2.3