diff --git a/crates/ide_assists/src/handlers/promote_mod_file.rs b/crates/ide_assists/src/handlers/move_to_mod_rs.rs similarity index 81% rename from crates/ide_assists/src/handlers/promote_mod_file.rs rename to 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::()?; 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 @@ fn must_select_all_file() { 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 @@ fn t() {}$0 #[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 @@ fn cannot_promote_mod_rs() { #[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 @@ fn cannot_promote_main_and_lib_rs() { 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 @@ pub(crate) fn all() -> &'static [Handler] { 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 @@ fn t() {} } #[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;