diff options
-rw-r--r-- | crates/assists/src/handlers/move_module_to_file.rs | 47 |
1 files changed, 45 insertions, 2 deletions
diff --git a/crates/assists/src/handlers/move_module_to_file.rs b/crates/assists/src/handlers/move_module_to_file.rs index 9d8579f47..91c395c1b 100644 --- a/crates/assists/src/handlers/move_module_to_file.rs +++ b/crates/assists/src/handlers/move_module_to_file.rs | |||
@@ -1,5 +1,6 @@ | |||
1 | use ast::edit::IndentLevel; | 1 | use ast::{edit::IndentLevel, VisibilityOwner}; |
2 | use ide_db::base_db::AnchoredPathBuf; | 2 | use ide_db::base_db::AnchoredPathBuf; |
3 | use stdx::format_to; | ||
3 | use syntax::{ | 4 | use syntax::{ |
4 | ast::{self, edit::AstNodeEdit, NameOwner}, | 5 | ast::{self, edit::AstNodeEdit, NameOwner}, |
5 | AstNode, TextRange, | 6 | AstNode, TextRange, |
@@ -59,7 +60,13 @@ pub(crate) fn move_module_to_file(acc: &mut Assists, ctx: &AssistContext) -> Opt | |||
59 | items | 60 | items |
60 | }; | 61 | }; |
61 | 62 | ||
62 | builder.replace(module_ast.syntax().text_range(), format!("mod {};", module_name)); | 63 | let mut buf = String::new(); |
64 | if let Some(v) = module_ast.visibility() { | ||
65 | format_to!(buf, "{} ", v); | ||
66 | } | ||
67 | format_to!(buf, "mod {};", module_name); | ||
68 | |||
69 | builder.replace(module_ast.syntax().text_range(), buf); | ||
63 | 70 | ||
64 | let dst = AnchoredPathBuf { anchor: ctx.frange.file_id, path }; | 71 | let dst = AnchoredPathBuf { anchor: ctx.frange.file_id, path }; |
65 | builder.create_file(dst, contents); | 72 | builder.create_file(dst, contents); |
@@ -138,6 +145,42 @@ fn f() {} | |||
138 | } | 145 | } |
139 | 146 | ||
140 | #[test] | 147 | #[test] |
148 | fn extract_public() { | ||
149 | check_assist( | ||
150 | move_module_to_file, | ||
151 | r#" | ||
152 | pub mod $0tests { | ||
153 | #[test] fn t() {} | ||
154 | } | ||
155 | "#, | ||
156 | r#" | ||
157 | //- /main.rs | ||
158 | pub mod tests; | ||
159 | //- /tests.rs | ||
160 | #[test] fn t() {} | ||
161 | "#, | ||
162 | ); | ||
163 | } | ||
164 | |||
165 | #[test] | ||
166 | fn extract_public_crate() { | ||
167 | check_assist( | ||
168 | move_module_to_file, | ||
169 | r#" | ||
170 | pub(crate) mod $0tests { | ||
171 | #[test] fn t() {} | ||
172 | } | ||
173 | "#, | ||
174 | r#" | ||
175 | //- /main.rs | ||
176 | pub(crate) mod tests; | ||
177 | //- /tests.rs | ||
178 | #[test] fn t() {} | ||
179 | "#, | ||
180 | ); | ||
181 | } | ||
182 | |||
183 | #[test] | ||
141 | fn available_before_curly() { | 184 | fn available_before_curly() { |
142 | mark::check!(available_before_curly); | 185 | mark::check!(available_before_curly); |
143 | check_assist_not_applicable(move_module_to_file, r#"mod m { $0 }"#); | 186 | check_assist_not_applicable(move_module_to_file, r#"mod m { $0 }"#); |