Skip to content

Commit 1552fdd

Browse files
Merge #8814
8814: fix: Keep doc comments and outer attrs on "Move module to file" assist r=Veykril a=Jesse-Bakker Fixes #8804 Co-authored-by: Jesse Bakker <[email protected]>
2 parents 908cd23 + 8c95b20 commit 1552fdd

File tree

1 file changed

+33
-5
lines changed

1 file changed

+33
-5
lines changed

crates/ide_assists/src/handlers/move_module_to_file.rs

+33-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use ast::{edit::IndentLevel, VisibilityOwner};
1+
use ast::edit::IndentLevel;
22
use ide_db::base_db::AnchoredPathBuf;
33
use stdx::format_to;
44
use syntax::{
@@ -60,12 +60,18 @@ pub(crate) fn move_module_to_file(acc: &mut Assists, ctx: &AssistContext) -> Opt
6060
};
6161

6262
let mut buf = String::new();
63-
if let Some(v) = module_ast.visibility() {
64-
format_to!(buf, "{} ", v);
65-
}
6663
format_to!(buf, "mod {};", module_name);
6764

68-
builder.replace(module_ast.syntax().text_range(), buf);
65+
let replacement_start = if let Some(mod_token) = module_ast.mod_token() {
66+
mod_token.text_range().start()
67+
} else {
68+
module_ast.syntax().text_range().start()
69+
};
70+
71+
builder.replace(
72+
TextRange::new(replacement_start, module_ast.syntax().text_range().end()),
73+
buf,
74+
);
6975

7076
let dst = AnchoredPathBuf { anchor: ctx.frange.file_id, path };
7177
builder.create_file(dst, contents);
@@ -184,4 +190,26 @@ pub(crate) mod tests;
184190
cov_mark::check!(available_before_curly);
185191
check_assist_not_applicable(move_module_to_file, r#"mod m { $0 }"#);
186192
}
193+
194+
#[test]
195+
fn keep_outer_comments_and_attributes() {
196+
check_assist(
197+
move_module_to_file,
198+
r#"
199+
/// doc comment
200+
#[attribute]
201+
mod $0tests {
202+
#[test] fn t() {}
203+
}
204+
"#,
205+
r#"
206+
//- /main.rs
207+
/// doc comment
208+
#[attribute]
209+
mod tests;
210+
//- /tests.rs
211+
#[test] fn t() {}
212+
"#,
213+
);
214+
}
187215
}

0 commit comments

Comments
 (0)