Address comments

This commit is contained in:
longfangsong 2021-09-25 22:47:20 +08:00
parent cd599ec202
commit 22abbe86f3
3 changed files with 27 additions and 19 deletions

View File

@ -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::<ast::SourceFile>()?;
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

View File

@ -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,

View File

@ -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;