diff options
| author | bors <bors@rust-lang.org> | 2024-05-14 14:29:00 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2024-05-14 14:29:00 +0000 |
| commit | 652426ce656e6ce870100f20dabb5a1307fd2215 (patch) | |
| tree | 4f0a06320e5760164a927518bf58d89dd43345b3 | |
| parent | 473b262b6cedc898876af902c38ea128ea33f4d8 (diff) | |
| parent | de58ddfd0f06609154879a462c85880d7b9b0dff (diff) | |
| download | rust-652426ce656e6ce870100f20dabb5a1307fd2215.tar.gz rust-652426ce656e6ce870100f20dabb5a1307fd2215.zip | |
Auto merge of #17216 - Young-Flash:mod_with_path, r=Veykril
fix: extract mod to file should respect path attribute close https://github.com/rust-lang/rust-analyzer/issues/17181
| -rw-r--r-- | src/tools/rust-analyzer/crates/ide-assists/src/handlers/move_module_to_file.rs | 81 |
1 files changed, 78 insertions, 3 deletions
diff --git a/src/tools/rust-analyzer/crates/ide-assists/src/handlers/move_module_to_file.rs b/src/tools/rust-analyzer/crates/ide-assists/src/handlers/move_module_to_file.rs index 048906d9d9f..9af8411f4cb 100644 --- a/src/tools/rust-analyzer/crates/ide-assists/src/handlers/move_module_to_file.rs +++ b/src/tools/rust-analyzer/crates/ide-assists/src/handlers/move_module_to_file.rs @@ -1,6 +1,7 @@ use std::iter; use ast::edit::IndentLevel; +use hir::HasAttrs; use ide_db::base_db::AnchoredPathBuf; use itertools::Itertools; use stdx::format_to; @@ -50,9 +51,17 @@ pub(crate) fn move_module_to_file(acc: &mut Assists, ctx: &AssistContext<'_>) -> |builder| { let path = { let mut buf = String::from("./"); - match parent_module.name(ctx.db()) { - Some(name) if !parent_module.is_mod_rs(ctx.db()) => { - format_to!(buf, "{}/", name.display(ctx.db())) + let db = ctx.db(); + match parent_module.name(db) { + Some(name) + if !parent_module.is_mod_rs(db) + && parent_module + .attrs(db) + .by_key("path") + .string_value_unescape() + .is_none() => + { + format_to!(buf, "{}/", name.display(db)) } _ => (), } @@ -108,6 +117,72 @@ mod tests { use super::*; #[test] + fn extract_with_specified_path_attr() { + check_assist( + move_module_to_file, + r#" +//- /main.rs +#[path="parser/__mod.rs"] +mod parser; +//- /parser/__mod.rs +fn test() {} +mod $0expr { + struct A {} +} +"#, + r#" +//- /parser/__mod.rs +fn test() {} +mod expr; +//- /parser/expr.rs +struct A {} +"#, + ); + + check_assist( + move_module_to_file, + r#" +//- /main.rs +#[path="parser/a/__mod.rs"] +mod parser; +//- /parser/a/__mod.rs +fn test() {} +mod $0expr { + struct A {} +} +"#, + r#" +//- /parser/a/__mod.rs +fn test() {} +mod expr; +//- /parser/a/expr.rs +struct A {} +"#, + ); + + check_assist( + move_module_to_file, + r#" +//- /main.rs +#[path="a.rs"] +mod parser; +//- /a.rs +fn test() {} +mod $0expr { + struct A {} +} +"#, + r#" +//- /a.rs +fn test() {} +mod expr; +//- /expr.rs +struct A {} +"#, + ); + } + + #[test] fn extract_from_root() { check_assist( move_module_to_file, |
