2 assists::{AssistId, AssistKind},
3 base_db::AnchoredPathBuf,
5 use syntax::{ast, AstNode, TextRange};
7 use crate::{assist_context::{AssistContext, Assists}, utils::trimmed_text_range};
9 // Assist: move_from_mod_rs
11 // Moves xxx/mod.rs to xxx.rs.
23 pub(crate) fn move_from_mod_rs(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
24 let source_file = ctx.find_node_at_offset::<ast::SourceFile>()?;
25 let module = ctx.sema.to_module_def(ctx.frange.file_id)?;
26 // Enable this assist if the user select all "meaningful" content in the source file
27 let trimmed_selected_range = trimmed_text_range(&source_file, ctx.frange.range);
28 let trimmed_file_range = trimmed_text_range(&source_file, source_file.syntax().text_range());
29 if !module.is_mod_rs(ctx.db()) {
30 cov_mark::hit!(not_mod_rs);
33 if trimmed_selected_range != trimmed_file_range {
34 cov_mark::hit!(not_all_selected);
38 let target = TextRange::new(
39 source_file.syntax().text_range().start(),
40 source_file.syntax().text_range().end(),
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!("Turn {}/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(