about summary refs log tree commit diff
diff options
context:
space:
mode:
authorlongfangsong <longfangsong@icloud.com>2021-09-25 22:47:20 +0800
committerlongfangsong <longfangsong@icloud.com>2021-09-25 22:48:57 +0800
commit22abbe86f343a2c3ce639a15ba88502cf6eebfe6 (patch)
tree72a61dc5719bb4dfb537cf3566493752c848e10a
parentcd599ec202d49d044d6d71fd3471e3c5199476e1 (diff)
downloadrust-22abbe86f343a2c3ce639a15ba88502cf6eebfe6.tar.gz
rust-22abbe86f343a2c3ce639a15ba88502cf6eebfe6.zip
Address comments
-rw-r--r--crates/ide_assists/src/handlers/move_to_mod_rs.rs (renamed from crates/ide_assists/src/handlers/promote_mod_file.rs)38
-rw-r--r--crates/ide_assists/src/lib.rs4
-rw-r--r--crates/ide_assists/src/tests/generated.rs4
3 files changed, 27 insertions, 19 deletions
diff --git a/crates/ide_assists/src/handlers/promote_mod_file.rs b/crates/ide_assists/src/handlers/move_to_mod_rs.rs
index 1a177893f0c..9b060bb710f 100644
--- a/crates/ide_assists/src/handlers/promote_mod_file.rs
+++ b/crates/ide_assists/src/handlers/move_to_mod_rs.rs
@@ -35,9 +35,9 @@ fn trimmed_text_range(source_file: &SourceFile, initial_range: TextRange) -> Tex
     trimmed_range
 }
 
-// Assist: promote_mod_file
+// Assist: move_to_mod_rs
 //
-// Moves inline module's contents to a separate file.
+// Moves xxx.rs to xxx/mod.rs.
 //
 // ```
 // //- /main.rs
@@ -49,13 +49,18 @@ fn trimmed_text_range(source_file: &SourceFile, initial_range: TextRange) -> Tex
 // ```
 // fn t() {}
 // ```
-pub(crate) fn promote_mod_file(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
+pub(crate) fn move_to_mod_rs(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
     let source_file = ctx.find_node_at_offset::<ast::SourceFile>()?;
     let module = ctx.sema.to_module_def(ctx.frange.file_id)?;
     // Enable this assist if the user select all "meaningful" content in the source file
     let trimmed_selected_range = trimmed_text_range(&source_file, ctx.frange.range);
     let trimmed_file_range = trimmed_text_range(&source_file, source_file.syntax().text_range());
-    if module.is_mod_rs(ctx.db()) || trimmed_selected_range != trimmed_file_range {
+    if module.is_mod_rs(ctx.db()) {
+        cov_mark::hit!(already_mod_rs);
+        return None;
+    }
+    if trimmed_selected_range != trimmed_file_range {
+        cov_mark::hit!(not_all_selected);
         return None;
     }
 
@@ -67,7 +72,7 @@ pub(crate) fn promote_mod_file(acc: &mut Assists, ctx: &AssistContext) -> Option
     let path = format!("./{}/mod.rs", module_name);
     let dst = AnchoredPathBuf { anchor: ctx.frange.file_id, path };
     acc.add(
-        AssistId("promote_mod_file", AssistKind::Refactor),
+        AssistId("move_to_mod_rs", AssistKind::Refactor),
         format!("Turn {}.rs to {}/mod.rs", module_name, module_name),
         target,
         |builder| {
@@ -85,7 +90,7 @@ mod tests {
     #[test]
     fn trivial() {
         check_assist(
-            promote_mod_file,
+            move_to_mod_rs,
             r#"
 //- /main.rs
 mod a;
@@ -101,8 +106,9 @@ fn t() {}
 
     #[test]
     fn must_select_all_file() {
+        cov_mark::check!(not_all_selected);
         check_assist_not_applicable(
-            promote_mod_file,
+            move_to_mod_rs,
             r#"
 //- /main.rs
 mod a;
@@ -110,8 +116,9 @@ mod a;
 fn t() {}$0
 "#,
         );
+        cov_mark::check!(not_all_selected);
         check_assist_not_applicable(
-            promote_mod_file,
+            move_to_mod_rs,
             r#"
 //- /main.rs
 mod a;
@@ -123,12 +130,13 @@ $0fn$0 t() {}
 
     #[test]
     fn cannot_promote_mod_rs() {
+        cov_mark::check!(already_mod_rs);
         check_assist_not_applicable(
-            promote_mod_file,
+            move_to_mod_rs,
             r#"//- /main.rs
 mod a;
 //- /a/mod.rs
-$0fn t() {}
+$0fn t() {}$0
 "#,
         );
     }
@@ -136,15 +144,15 @@ $0fn t() {}
     #[test]
     fn cannot_promote_main_and_lib_rs() {
         check_assist_not_applicable(
-            promote_mod_file,
+            move_to_mod_rs,
             r#"//- /main.rs
-$0fn t() {}
+$0fn t() {}$0
 "#,
         );
         check_assist_not_applicable(
-            promote_mod_file,
+            move_to_mod_rs,
             r#"//- /lib.rs
-$0fn t() {}
+$0fn t() {}$0
 "#,
         );
     }
@@ -153,7 +161,7 @@ $0fn t() {}
     fn works_in_mod() {
         // note: /a/b.rs remains untouched
         check_assist(
-            promote_mod_file,
+            move_to_mod_rs,
             r#"//- /main.rs
 mod a;
 //- /a.rs
diff --git a/crates/ide_assists/src/lib.rs b/crates/ide_assists/src/lib.rs
index 57007691b5e..1f5e2f036e0 100644
--- a/crates/ide_assists/src/lib.rs
+++ b/crates/ide_assists/src/lib.rs
@@ -96,7 +96,7 @@ mod handlers {
     mod move_bounds;
     mod move_guard;
     mod move_module_to_file;
-    mod promote_mod_file;
+    mod move_to_mod_rs;
     mod pull_assignment_up;
     mod qualify_path;
     mod raw_string;
@@ -168,7 +168,7 @@ mod handlers {
             move_guard::move_arm_cond_to_match_guard,
             move_guard::move_guard_to_arm_body,
             move_module_to_file::move_module_to_file,
-            promote_mod_file::promote_mod_file,
+            move_to_mod_rs::move_to_mod_rs,
             pull_assignment_up::pull_assignment_up,
             qualify_path::qualify_path,
             raw_string::add_hash,
diff --git a/crates/ide_assists/src/tests/generated.rs b/crates/ide_assists/src/tests/generated.rs
index 4ec3e0bb1bd..f371ca80178 100644
--- a/crates/ide_assists/src/tests/generated.rs
+++ b/crates/ide_assists/src/tests/generated.rs
@@ -1227,9 +1227,9 @@ mod foo;
 }
 
 #[test]
-fn doctest_promote_mod_file() {
+fn doctest_move_to_mod_rs() {
     check_doc_test(
-        "promote_mod_file",
+        "move_to_mod_rs",
         r#####"
 //- /main.rs
 mod a;