@@ -35,9 +35,9 @@ fn trimmed_text_range(source_file: &SourceFile, initial_range: TextRange) -> Tex
35
35
trimmed_range
36
36
}
37
37
38
- // Assist: promote_mod_file
38
+ // Assist: move_to_mod_rs
39
39
//
40
- // Moves inline module's contents to a separate file .
40
+ // Moves xxx.rs to xxx/mod.rs .
41
41
//
42
42
// ```
43
43
// //- /main.rs
@@ -49,13 +49,18 @@ fn trimmed_text_range(source_file: &SourceFile, initial_range: TextRange) -> Tex
49
49
// ```
50
50
// fn t() {}
51
51
// ```
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 < ( ) > {
53
53
let source_file = ctx. find_node_at_offset :: < ast:: SourceFile > ( ) ?;
54
54
let module = ctx. sema . to_module_def ( ctx. frange . file_id ) ?;
55
55
// Enable this assist if the user select all "meaningful" content in the source file
56
56
let trimmed_selected_range = trimmed_text_range ( & source_file, ctx. frange . range ) ;
57
57
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) ;
59
64
return None ;
60
65
}
61
66
@@ -67,7 +72,7 @@ pub(crate) fn promote_mod_file(acc: &mut Assists, ctx: &AssistContext) -> Option
67
72
let path = format ! ( "./{}/mod.rs" , module_name) ;
68
73
let dst = AnchoredPathBuf { anchor : ctx. frange . file_id , path } ;
69
74
acc. add (
70
- AssistId ( "promote_mod_file " , AssistKind :: Refactor ) ,
75
+ AssistId ( "move_to_mod_rs " , AssistKind :: Refactor ) ,
71
76
format ! ( "Turn {}.rs to {}/mod.rs" , module_name, module_name) ,
72
77
target,
73
78
|builder| {
@@ -85,7 +90,7 @@ mod tests {
85
90
#[ test]
86
91
fn trivial ( ) {
87
92
check_assist (
88
- promote_mod_file ,
93
+ move_to_mod_rs ,
89
94
r#"
90
95
//- /main.rs
91
96
mod a;
@@ -101,17 +106,19 @@ fn t() {}
101
106
102
107
#[ test]
103
108
fn must_select_all_file ( ) {
109
+ cov_mark:: check!( not_all_selected) ;
104
110
check_assist_not_applicable (
105
- promote_mod_file ,
111
+ move_to_mod_rs ,
106
112
r#"
107
113
//- /main.rs
108
114
mod a;
109
115
//- /a.rs
110
116
fn t() {}$0
111
117
"# ,
112
118
) ;
119
+ cov_mark:: check!( not_all_selected) ;
113
120
check_assist_not_applicable (
114
- promote_mod_file ,
121
+ move_to_mod_rs ,
115
122
r#"
116
123
//- /main.rs
117
124
mod a;
@@ -123,28 +130,29 @@ $0fn$0 t() {}
123
130
124
131
#[ test]
125
132
fn cannot_promote_mod_rs ( ) {
133
+ cov_mark:: check!( already_mod_rs) ;
126
134
check_assist_not_applicable (
127
- promote_mod_file ,
135
+ move_to_mod_rs ,
128
136
r#"//- /main.rs
129
137
mod a;
130
138
//- /a/mod.rs
131
- $0fn t() {}
139
+ $0fn t() {}$0
132
140
"# ,
133
141
) ;
134
142
}
135
143
136
144
#[ test]
137
145
fn cannot_promote_main_and_lib_rs ( ) {
138
146
check_assist_not_applicable (
139
- promote_mod_file ,
147
+ move_to_mod_rs ,
140
148
r#"//- /main.rs
141
- $0fn t() {}
149
+ $0fn t() {}$0
142
150
"# ,
143
151
) ;
144
152
check_assist_not_applicable (
145
- promote_mod_file ,
153
+ move_to_mod_rs ,
146
154
r#"//- /lib.rs
147
- $0fn t() {}
155
+ $0fn t() {}$0
148
156
"# ,
149
157
) ;
150
158
}
@@ -153,7 +161,7 @@ $0fn t() {}
153
161
fn works_in_mod ( ) {
154
162
// note: /a/b.rs remains untouched
155
163
check_assist (
156
- promote_mod_file ,
164
+ move_to_mod_rs ,
157
165
r#"//- /main.rs
158
166
mod a;
159
167
//- /a.rs
0 commit comments