From 4f989cfa08e9a9f4abf8dc6136c9802bc7ad3a4c Mon Sep 17 00:00:00 2001 From: Jeremy Kolb Date: Tue, 22 Dec 2020 14:19:51 -0500 Subject: Implement workspace/willRenameFiles for single-level file moves Renames modules during file rename if they're in the same directory. --- crates/ide/src/lib.rs | 8 ++++++++ crates/ide/src/references/rename.rs | 19 ++++++++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) (limited to 'crates/ide') diff --git a/crates/ide/src/lib.rs b/crates/ide/src/lib.rs index dbad9a84f..52c7f9775 100644 --- a/crates/ide/src/lib.rs +++ b/crates/ide/src/lib.rs @@ -535,6 +535,14 @@ impl Analysis { self.with_db(|db| references::rename::prepare_rename(db, position)) } + pub fn will_rename_file( + &self, + file_id: FileId, + new_name_stem: &str, + ) -> Cancelable> { + self.with_db(|db| references::rename::will_rename_file(db, file_id, new_name_stem)) + } + pub fn structural_search_replace( &self, query: &str, diff --git a/crates/ide/src/references/rename.rs b/crates/ide/src/references/rename.rs index cd721b7eb..15c95f239 100644 --- a/crates/ide/src/references/rename.rs +++ b/crates/ide/src/references/rename.rs @@ -6,7 +6,7 @@ use std::{ }; use hir::{Module, ModuleDef, ModuleSource, Semantics}; -use ide_db::base_db::{AnchoredPathBuf, FileRange, SourceDatabaseExt}; +use ide_db::base_db::{AnchoredPathBuf, FileId, FileRange, SourceDatabaseExt}; use ide_db::{ defs::{Definition, NameClass, NameRefClass}, RootDatabase, @@ -110,6 +110,23 @@ pub(crate) fn rename_with_semantics( } } +pub(crate) fn will_rename_file( + db: &RootDatabase, + file_id: FileId, + new_name_stem: &str, +) -> Option { + let sema = Semantics::new(db); + let module = sema.to_module_def(file_id)?; + + let decl = module.declaration_source(db)?; + let range = decl.value.name()?.syntax().text_range(); + + let position = FilePosition { file_id: decl.file_id.original_file(db), offset: range.start() }; + let mut change = rename_mod(&sema, position, module, new_name_stem).ok()?.info; + change.file_system_edits.clear(); + Some(change) +} + fn find_module_at_offset( sema: &Semantics, position: FilePosition, -- cgit v1.2.3