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_from_mod_rs
14 // Moves xxx/mod.rs to xxx.rs.
26 pub(crate) fn move_from_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.frange.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.frange.range);
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!(not_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!("../{}.rs", module_name);
44 let dst = AnchoredPathBuf { anchor: ctx.frange.file_id, path };
46 AssistId("move_from_mod_rs", AssistKind::Refactor),
47 format!("Convert {}/mod.rs to {}.rs", module_name, module_name),
50 builder.move_file(ctx.frange.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_move_not_mod_rs() {
104 cov_mark::check!(not_mod_rs);
105 check_assist_not_applicable(
116 fn cannot_downgrade_main_and_lib_rs() {
117 check_assist_not_applicable(
123 check_assist_not_applicable(