From 4b3164f129dcbbf6a4d15a4c01bfdb01f3f8721e Mon Sep 17 00:00:00 2001 From: harpsword Date: Thu, 7 Jul 2022 22:25:25 +0800 Subject: [PATCH] fix: ignore renames for crate root --- crates/ide-db/src/rename.rs | 4 ++++ crates/ide/src/rename.rs | 29 +++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/crates/ide-db/src/rename.rs b/crates/ide-db/src/rename.rs index 8f83496e93..59cbde0944 100644 --- a/crates/ide-db/src/rename.rs +++ b/crates/ide-db/src/rename.rs @@ -177,6 +177,10 @@ fn rename_mod( let mut source_change = SourceChange::default(); + if module.is_crate_root(sema.db) { + return Ok(source_change); + } + let InFile { file_id, value: def_source } = module.definition_source(sema.db); if let ModuleSource::SourceFile(..) = def_source { let anchor = file_id.original_file(sema.db); diff --git a/crates/ide/src/rename.rs b/crates/ide/src/rename.rs index 0f8033611a..23d6f8d8e4 100644 --- a/crates/ide/src/rename.rs +++ b/crates/ide/src/rename.rs @@ -379,6 +379,15 @@ mod tests { expect.assert_debug_eq(&source_change) } + fn check_expect_will_rename_file(new_name: &str, ra_fixture: &str, expect: Expect) { + let (analysis, position) = fixture::position(ra_fixture); + let source_change = analysis + .will_rename_file(position.file_id, new_name) + .unwrap() + .expect("Expect returned a RenameError"); + expect.assert_debug_eq(&source_change) + } + fn check_prepare(ra_fixture: &str, expect: Expect) { let (analysis, position) = fixture::position(ra_fixture); let result = analysis @@ -1245,6 +1254,26 @@ submodule!(bar); ) } + #[test] + fn test_rename_mod_for_crate_root() { + check_expect_will_rename_file( + "main", + r#" +//- /lib.rs +use crate::foo as bar; +fn foo() {} +mod bar$0; +"#, + expect![[r#" + SourceChange { + source_file_edits: {}, + file_system_edits: [], + is_snippet: false, + } + "#]], + ) + } + #[test] fn test_enum_variant_from_module_1() { cov_mark::check!(rename_non_local);