about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2021-05-13 17:37:52 +0000
committerGitHub <noreply@github.com>2021-05-13 17:37:52 +0000
commit1552fdd3bcbecd77971617d16be210c74c88e409 (patch)
tree72f7974fb3bcffc421ca0bf39b97908cc1855736
parent908cd23f81d94bc53e318089fd8bd52e27906f19 (diff)
parent8c95b205a27a27bed6434644e8016caa49aeffc1 (diff)
downloadrust-1552fdd3bcbecd77971617d16be210c74c88e409.tar.gz
rust-1552fdd3bcbecd77971617d16be210c74c88e409.zip
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 <github@jessebakker.com>
-rw-r--r--crates/ide_assists/src/handlers/move_module_to_file.rs38
1 files changed, 33 insertions, 5 deletions
diff --git a/crates/ide_assists/src/handlers/move_module_to_file.rs b/crates/ide_assists/src/handlers/move_module_to_file.rs
index 6e685b4b293..93f702c5568 100644
--- a/crates/ide_assists/src/handlers/move_module_to_file.rs
+++ b/crates/ide_assists/src/handlers/move_module_to_file.rs
@@ -1,4 +1,4 @@
-use ast::{edit::IndentLevel, VisibilityOwner};
+use ast::edit::IndentLevel;
 use ide_db::base_db::AnchoredPathBuf;
 use stdx::format_to;
 use syntax::{
@@ -60,12 +60,18 @@ pub(crate) fn move_module_to_file(acc: &mut Assists, ctx: &AssistContext) -> Opt
             };
 
             let mut buf = String::new();
-            if let Some(v) = module_ast.visibility() {
-                format_to!(buf, "{} ", v);
-            }
             format_to!(buf, "mod {};", module_name);
 
-            builder.replace(module_ast.syntax().text_range(), buf);
+            let replacement_start = if let Some(mod_token) = module_ast.mod_token() {
+                mod_token.text_range().start()
+            } else {
+                module_ast.syntax().text_range().start()
+            };
+
+            builder.replace(
+                TextRange::new(replacement_start, module_ast.syntax().text_range().end()),
+                buf,
+            );
 
             let dst = AnchoredPathBuf { anchor: ctx.frange.file_id, path };
             builder.create_file(dst, contents);
@@ -184,4 +190,26 @@ pub(crate) mod tests;
         cov_mark::check!(available_before_curly);
         check_assist_not_applicable(move_module_to_file, r#"mod m { $0 }"#);
     }
+
+    #[test]
+    fn keep_outer_comments_and_attributes() {
+        check_assist(
+            move_module_to_file,
+            r#"
+/// doc comment
+#[attribute]
+mod $0tests {
+    #[test] fn t() {}
+}
+"#,
+            r#"
+//- /main.rs
+/// doc comment
+#[attribute]
+mod tests;
+//- /tests.rs
+#[test] fn t() {}
+"#,
+        );
+    }
 }