2021-09-11 13:30:56 -05:00
|
|
|
use ide_db::{
|
|
|
|
assists::{AssistId, AssistKind},
|
|
|
|
base_db::AnchoredPathBuf,
|
|
|
|
};
|
2021-09-27 21:20:35 -05:00
|
|
|
use syntax::{ast, AstNode};
|
2021-09-11 13:30:56 -05:00
|
|
|
|
2021-09-27 21:20:35 -05:00
|
|
|
use crate::{
|
|
|
|
assist_context::{AssistContext, Assists},
|
|
|
|
utils::trimmed_text_range,
|
|
|
|
};
|
2021-09-20 07:43:13 -05:00
|
|
|
|
2021-09-25 09:47:20 -05:00
|
|
|
// Assist: move_to_mod_rs
|
2021-09-11 21:53:56 -05:00
|
|
|
//
|
2021-09-25 09:47:20 -05:00
|
|
|
// Moves xxx.rs to xxx/mod.rs.
|
2021-09-11 21:53:56 -05:00
|
|
|
//
|
|
|
|
// ```
|
2021-09-11 22:20:28 -05:00
|
|
|
// //- /main.rs
|
|
|
|
// mod a;
|
|
|
|
// //- /a.rs
|
2021-09-20 07:43:13 -05:00
|
|
|
// $0fn t() {}$0
|
2021-09-11 21:53:56 -05:00
|
|
|
// ```
|
|
|
|
// ->
|
|
|
|
// ```
|
|
|
|
// fn t() {}
|
|
|
|
// ```
|
2021-09-25 09:47:20 -05:00
|
|
|
pub(crate) fn move_to_mod_rs(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
|
2021-09-11 13:30:56 -05:00
|
|
|
let source_file = ctx.find_node_at_offset::<ast::SourceFile>()?;
|
2021-10-13 07:39:37 -05:00
|
|
|
let module = ctx.sema.to_module_def(ctx.file_id())?;
|
2021-09-20 07:43:13 -05:00
|
|
|
// Enable this assist if the user select all "meaningful" content in the source file
|
2021-10-12 07:41:59 -05:00
|
|
|
let trimmed_selected_range = trimmed_text_range(&source_file, ctx.selection_trimmed());
|
2021-09-20 07:43:13 -05:00
|
|
|
let trimmed_file_range = trimmed_text_range(&source_file, source_file.syntax().text_range());
|
2021-09-25 09:47:20 -05:00
|
|
|
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);
|
2021-09-11 13:30:56 -05:00
|
|
|
return None;
|
|
|
|
}
|
2021-09-20 07:43:13 -05:00
|
|
|
|
2021-09-27 21:20:35 -05:00
|
|
|
let target = source_file.syntax().text_range();
|
2021-09-20 07:43:13 -05:00
|
|
|
let module_name = module.name(ctx.db())?.to_string();
|
|
|
|
let path = format!("./{}/mod.rs", module_name);
|
2021-10-13 07:39:37 -05:00
|
|
|
let dst = AnchoredPathBuf { anchor: ctx.file_id(), path };
|
2021-09-11 13:30:56 -05:00
|
|
|
acc.add(
|
2021-09-25 09:47:20 -05:00
|
|
|
AssistId("move_to_mod_rs", AssistKind::Refactor),
|
2021-09-27 21:20:35 -05:00
|
|
|
format!("Convert {}.rs to {}/mod.rs", module_name, module_name),
|
2021-09-11 13:30:56 -05:00
|
|
|
target,
|
|
|
|
|builder| {
|
2021-10-13 07:39:37 -05:00
|
|
|
builder.move_file(ctx.file_id(), dst);
|
2021-09-11 13:30:56 -05:00
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use crate::tests::{check_assist, check_assist_not_applicable};
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn trivial() {
|
|
|
|
check_assist(
|
2021-09-25 09:47:20 -05:00
|
|
|
move_to_mod_rs,
|
2021-09-11 13:30:56 -05:00
|
|
|
r#"
|
|
|
|
//- /main.rs
|
|
|
|
mod a;
|
|
|
|
//- /a.rs
|
|
|
|
$0fn t() {}
|
2021-09-20 07:43:13 -05:00
|
|
|
$0"#,
|
2021-09-11 13:30:56 -05:00
|
|
|
r#"
|
|
|
|
//- /a/mod.rs
|
|
|
|
fn t() {}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-09-20 07:43:13 -05:00
|
|
|
fn must_select_all_file() {
|
2021-09-25 09:47:20 -05:00
|
|
|
cov_mark::check!(not_all_selected);
|
2021-09-20 07:43:13 -05:00
|
|
|
check_assist_not_applicable(
|
2021-09-25 09:47:20 -05:00
|
|
|
move_to_mod_rs,
|
2021-09-11 13:30:56 -05:00
|
|
|
r#"
|
|
|
|
//- /main.rs
|
|
|
|
mod a;
|
|
|
|
//- /a.rs
|
|
|
|
fn t() {}$0
|
|
|
|
"#,
|
|
|
|
);
|
2021-09-25 09:47:20 -05:00
|
|
|
cov_mark::check!(not_all_selected);
|
2021-09-20 07:43:13 -05:00
|
|
|
check_assist_not_applicable(
|
2021-09-25 09:47:20 -05:00
|
|
|
move_to_mod_rs,
|
2021-09-11 13:30:56 -05:00
|
|
|
r#"
|
|
|
|
//- /main.rs
|
|
|
|
mod a;
|
|
|
|
//- /a.rs
|
2021-09-20 07:43:13 -05:00
|
|
|
$0fn$0 t() {}
|
2021-09-11 13:30:56 -05:00
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn cannot_promote_mod_rs() {
|
2021-09-25 09:47:20 -05:00
|
|
|
cov_mark::check!(already_mod_rs);
|
2021-09-11 13:30:56 -05:00
|
|
|
check_assist_not_applicable(
|
2021-09-25 09:47:20 -05:00
|
|
|
move_to_mod_rs,
|
2021-09-11 13:30:56 -05:00
|
|
|
r#"//- /main.rs
|
|
|
|
mod a;
|
|
|
|
//- /a/mod.rs
|
2021-09-25 09:47:20 -05:00
|
|
|
$0fn t() {}$0
|
2021-09-11 13:30:56 -05:00
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn cannot_promote_main_and_lib_rs() {
|
|
|
|
check_assist_not_applicable(
|
2021-09-25 09:47:20 -05:00
|
|
|
move_to_mod_rs,
|
2021-09-11 13:30:56 -05:00
|
|
|
r#"//- /main.rs
|
2021-09-25 09:47:20 -05:00
|
|
|
$0fn t() {}$0
|
2021-09-11 13:30:56 -05:00
|
|
|
"#,
|
|
|
|
);
|
|
|
|
check_assist_not_applicable(
|
2021-09-25 09:47:20 -05:00
|
|
|
move_to_mod_rs,
|
2021-09-11 13:30:56 -05:00
|
|
|
r#"//- /lib.rs
|
2021-09-25 09:47:20 -05:00
|
|
|
$0fn t() {}$0
|
2021-09-11 13:30:56 -05:00
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn works_in_mod() {
|
|
|
|
// note: /a/b.rs remains untouched
|
|
|
|
check_assist(
|
2021-09-25 09:47:20 -05:00
|
|
|
move_to_mod_rs,
|
2021-09-11 13:30:56 -05:00
|
|
|
r#"//- /main.rs
|
|
|
|
mod a;
|
|
|
|
//- /a.rs
|
2021-09-20 07:43:13 -05:00
|
|
|
$0mod b;
|
|
|
|
fn t() {}$0
|
2021-09-11 13:30:56 -05:00
|
|
|
//- /a/b.rs
|
|
|
|
fn t1() {}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
//- /a/mod.rs
|
|
|
|
mod b;
|
|
|
|
fn t() {}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|