Skip to content

Commit 652426c

Browse files
committed
Auto merge of #17216 - Young-Flash:mod_with_path, r=Veykril
fix: extract mod to file should respect path attribute close rust-lang/rust-analyzer#17181
2 parents 473b262 + de58ddf commit 652426c

File tree

1 file changed

+78
-3
lines changed

1 file changed

+78
-3
lines changed

src/tools/rust-analyzer/crates/ide-assists/src/handlers/move_module_to_file.rs

Lines changed: 78 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::iter;
22

33
use ast::edit::IndentLevel;
4+
use hir::HasAttrs;
45
use ide_db::base_db::AnchoredPathBuf;
56
use itertools::Itertools;
67
use stdx::format_to;
@@ -50,9 +51,17 @@ pub(crate) fn move_module_to_file(acc: &mut Assists, ctx: &AssistContext<'_>) ->
5051
|builder| {
5152
let path = {
5253
let mut buf = String::from("./");
53-
match parent_module.name(ctx.db()) {
54-
Some(name) if !parent_module.is_mod_rs(ctx.db()) => {
55-
format_to!(buf, "{}/", name.display(ctx.db()))
54+
let db = ctx.db();
55+
match parent_module.name(db) {
56+
Some(name)
57+
if !parent_module.is_mod_rs(db)
58+
&& parent_module
59+
.attrs(db)
60+
.by_key("path")
61+
.string_value_unescape()
62+
.is_none() =>
63+
{
64+
format_to!(buf, "{}/", name.display(db))
5665
}
5766
_ => (),
5867
}
@@ -107,6 +116,72 @@ mod tests {
107116

108117
use super::*;
109118

119+
#[test]
120+
fn extract_with_specified_path_attr() {
121+
check_assist(
122+
move_module_to_file,
123+
r#"
124+
//- /main.rs
125+
#[path="parser/__mod.rs"]
126+
mod parser;
127+
//- /parser/__mod.rs
128+
fn test() {}
129+
mod $0expr {
130+
struct A {}
131+
}
132+
"#,
133+
r#"
134+
//- /parser/__mod.rs
135+
fn test() {}
136+
mod expr;
137+
//- /parser/expr.rs
138+
struct A {}
139+
"#,
140+
);
141+
142+
check_assist(
143+
move_module_to_file,
144+
r#"
145+
//- /main.rs
146+
#[path="parser/a/__mod.rs"]
147+
mod parser;
148+
//- /parser/a/__mod.rs
149+
fn test() {}
150+
mod $0expr {
151+
struct A {}
152+
}
153+
"#,
154+
r#"
155+
//- /parser/a/__mod.rs
156+
fn test() {}
157+
mod expr;
158+
//- /parser/a/expr.rs
159+
struct A {}
160+
"#,
161+
);
162+
163+
check_assist(
164+
move_module_to_file,
165+
r#"
166+
//- /main.rs
167+
#[path="a.rs"]
168+
mod parser;
169+
//- /a.rs
170+
fn test() {}
171+
mod $0expr {
172+
struct A {}
173+
}
174+
"#,
175+
r#"
176+
//- /a.rs
177+
fn test() {}
178+
mod expr;
179+
//- /expr.rs
180+
struct A {}
181+
"#,
182+
);
183+
}
184+
110185
#[test]
111186
fn extract_from_root() {
112187
check_assist(

0 commit comments

Comments
 (0)