2 assists::{AssistId, AssistKind},
3 base_db::AnchoredPathBuf,
5 use syntax::{ast, AstNode};
8 assist_context::{AssistContext, Assists},
9 utils::trimmed_text_range,
12 // Assist: move_to_mod_rs
14 // Moves xxx.rs to xxx/mod.rs.
26 pub(crate) fn move_to_mod_rs(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
27 let source_file = ctx.find_node_at_offset::<ast::SourceFile>()?;
28 let module = ctx.sema.to_module_def(ctx.file_id())?;
29 // Enable this assist if the user select all "meaningful" content in the source file
30 let trimmed_selected_range = trimmed_text_range(&source_file, ctx.selection_trimmed());
31 let trimmed_file_range = trimmed_text_range(&source_file, source_file.syntax().text_range());
32 if module.is_mod_rs(ctx.db()) {
33 cov_mark::hit!(already_mod_rs);
36 if trimmed_selected_range != trimmed_file_range {
37 cov_mark::hit!(not_all_selected);
41 let target = source_file.syntax().text_range();
42 let module_name = module.name(ctx.db())?.to_string();
43 let path = format!("./{}/mod.rs", module_name);
44 let dst = AnchoredPathBuf { anchor: ctx.file_id(), path };
46 AssistId("move_to_mod_rs", AssistKind::Refactor),
47 format!("Convert {}.rs to {}/mod.rs", module_name, module_name),
50 builder.move_file(ctx.file_id(), dst);
57 use crate::tests::{check_assist, check_assist_not_applicable};
79 fn must_select_all_file() {
80 cov_mark::check!(not_all_selected);
81 check_assist_not_applicable(
90 cov_mark::check!(not_all_selected);
91 check_assist_not_applicable(
103 fn cannot_promote_mod_rs() {
104 cov_mark::check!(already_mod_rs);
105 check_assist_not_applicable(
116 fn cannot_promote_main_and_lib_rs() {
117 check_assist_not_applicable(
123 check_assist_not_applicable(
133 // note: /a/b.rs remains untouched