Skip to content

Commit 22abbe8

Browse files
committed
Address comments
1 parent cd599ec commit 22abbe8

File tree

3 files changed

+27
-19
lines changed

3 files changed

+27
-19
lines changed

crates/ide_assists/src/handlers/promote_mod_file.rs renamed to crates/ide_assists/src/handlers/move_to_mod_rs.rs

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ fn trimmed_text_range(source_file: &SourceFile, initial_range: TextRange) -> Tex
3535
trimmed_range
3636
}
3737

38-
// Assist: promote_mod_file
38+
// Assist: move_to_mod_rs
3939
//
40-
// Moves inline module's contents to a separate file.
40+
// Moves xxx.rs to xxx/mod.rs.
4141
//
4242
// ```
4343
// //- /main.rs
@@ -49,13 +49,18 @@ fn trimmed_text_range(source_file: &SourceFile, initial_range: TextRange) -> Tex
4949
// ```
5050
// fn t() {}
5151
// ```
52-
pub(crate) fn promote_mod_file(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
52+
pub(crate) fn move_to_mod_rs(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
5353
let source_file = ctx.find_node_at_offset::<ast::SourceFile>()?;
5454
let module = ctx.sema.to_module_def(ctx.frange.file_id)?;
5555
// Enable this assist if the user select all "meaningful" content in the source file
5656
let trimmed_selected_range = trimmed_text_range(&source_file, ctx.frange.range);
5757
let trimmed_file_range = trimmed_text_range(&source_file, source_file.syntax().text_range());
58-
if module.is_mod_rs(ctx.db()) || trimmed_selected_range != trimmed_file_range {
58+
if module.is_mod_rs(ctx.db()) {
59+
cov_mark::hit!(already_mod_rs);
60+
return None;
61+
}
62+
if trimmed_selected_range != trimmed_file_range {
63+
cov_mark::hit!(not_all_selected);
5964
return None;
6065
}
6166

@@ -67,7 +72,7 @@ pub(crate) fn promote_mod_file(acc: &mut Assists, ctx: &AssistContext) -> Option
6772
let path = format!("./{}/mod.rs", module_name);
6873
let dst = AnchoredPathBuf { anchor: ctx.frange.file_id, path };
6974
acc.add(
70-
AssistId("promote_mod_file", AssistKind::Refactor),
75+
AssistId("move_to_mod_rs", AssistKind::Refactor),
7176
format!("Turn {}.rs to {}/mod.rs", module_name, module_name),
7277
target,
7378
|builder| {
@@ -85,7 +90,7 @@ mod tests {
8590
#[test]
8691
fn trivial() {
8792
check_assist(
88-
promote_mod_file,
93+
move_to_mod_rs,
8994
r#"
9095
//- /main.rs
9196
mod a;
@@ -101,17 +106,19 @@ fn t() {}
101106

102107
#[test]
103108
fn must_select_all_file() {
109+
cov_mark::check!(not_all_selected);
104110
check_assist_not_applicable(
105-
promote_mod_file,
111+
move_to_mod_rs,
106112
r#"
107113
//- /main.rs
108114
mod a;
109115
//- /a.rs
110116
fn t() {}$0
111117
"#,
112118
);
119+
cov_mark::check!(not_all_selected);
113120
check_assist_not_applicable(
114-
promote_mod_file,
121+
move_to_mod_rs,
115122
r#"
116123
//- /main.rs
117124
mod a;
@@ -123,28 +130,29 @@ $0fn$0 t() {}
123130

124131
#[test]
125132
fn cannot_promote_mod_rs() {
133+
cov_mark::check!(already_mod_rs);
126134
check_assist_not_applicable(
127-
promote_mod_file,
135+
move_to_mod_rs,
128136
r#"//- /main.rs
129137
mod a;
130138
//- /a/mod.rs
131-
$0fn t() {}
139+
$0fn t() {}$0
132140
"#,
133141
);
134142
}
135143

136144
#[test]
137145
fn cannot_promote_main_and_lib_rs() {
138146
check_assist_not_applicable(
139-
promote_mod_file,
147+
move_to_mod_rs,
140148
r#"//- /main.rs
141-
$0fn t() {}
149+
$0fn t() {}$0
142150
"#,
143151
);
144152
check_assist_not_applicable(
145-
promote_mod_file,
153+
move_to_mod_rs,
146154
r#"//- /lib.rs
147-
$0fn t() {}
155+
$0fn t() {}$0
148156
"#,
149157
);
150158
}
@@ -153,7 +161,7 @@ $0fn t() {}
153161
fn works_in_mod() {
154162
// note: /a/b.rs remains untouched
155163
check_assist(
156-
promote_mod_file,
164+
move_to_mod_rs,
157165
r#"//- /main.rs
158166
mod a;
159167
//- /a.rs

crates/ide_assists/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ mod handlers {
9696
mod move_bounds;
9797
mod move_guard;
9898
mod move_module_to_file;
99-
mod promote_mod_file;
99+
mod move_to_mod_rs;
100100
mod pull_assignment_up;
101101
mod qualify_path;
102102
mod raw_string;
@@ -168,7 +168,7 @@ mod handlers {
168168
move_guard::move_arm_cond_to_match_guard,
169169
move_guard::move_guard_to_arm_body,
170170
move_module_to_file::move_module_to_file,
171-
promote_mod_file::promote_mod_file,
171+
move_to_mod_rs::move_to_mod_rs,
172172
pull_assignment_up::pull_assignment_up,
173173
qualify_path::qualify_path,
174174
raw_string::add_hash,

crates/ide_assists/src/tests/generated.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1227,9 +1227,9 @@ mod foo;
12271227
}
12281228

12291229
#[test]
1230-
fn doctest_promote_mod_file() {
1230+
fn doctest_move_to_mod_rs() {
12311231
check_doc_test(
1232-
"promote_mod_file",
1232+
"move_to_mod_rs",
12331233
r#####"
12341234
//- /main.rs
12351235
mod a;

0 commit comments

Comments
 (0)