|
1 |
| -use ast::{edit::IndentLevel, VisibilityOwner}; |
| 1 | +use ast::edit::IndentLevel; |
2 | 2 | use ide_db::base_db::AnchoredPathBuf;
|
3 | 3 | use stdx::format_to;
|
4 | 4 | use syntax::{
|
@@ -60,12 +60,18 @@ pub(crate) fn move_module_to_file(acc: &mut Assists, ctx: &AssistContext) -> Opt
|
60 | 60 | };
|
61 | 61 |
|
62 | 62 | let mut buf = String::new();
|
63 |
| - if let Some(v) = module_ast.visibility() { |
64 |
| - format_to!(buf, "{} ", v); |
65 |
| - } |
66 | 63 | format_to!(buf, "mod {};", module_name);
|
67 | 64 |
|
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 | + ); |
69 | 75 |
|
70 | 76 | let dst = AnchoredPathBuf { anchor: ctx.frange.file_id, path };
|
71 | 77 | builder.create_file(dst, contents);
|
@@ -184,4 +190,26 @@ pub(crate) mod tests;
|
184 | 190 | cov_mark::check!(available_before_curly);
|
185 | 191 | check_assist_not_applicable(move_module_to_file, r#"mod m { $0 }"#);
|
186 | 192 | }
|
| 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 | + } |
187 | 215 | }
|
0 commit comments