diff options
author | Matt Hall <[email protected]> | 2021-02-20 16:51:16 +0000 |
---|---|---|
committer | Matt Hall <[email protected]> | 2021-02-20 16:51:16 +0000 |
commit | b963893349c0df4c6e8786303eaa39e2bc9fa500 (patch) | |
tree | fb8cd9f819e663dd36fa98d4f8d47e8c2ed8a2f3 /crates | |
parent | de67469f59d5c16e9b4ca106a0265bb8b1585c83 (diff) |
Fix #7712 retain visibility extracting mod to file
Diffstat (limited to 'crates')
-rw-r--r-- | crates/assists/src/handlers/move_module_to_file.rs | 45 |
1 files changed, 43 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..1f2d8f20f 100644 --- a/crates/assists/src/handlers/move_module_to_file.rs +++ b/crates/assists/src/handlers/move_module_to_file.rs | |||
@@ -1,4 +1,4 @@ | |||
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 syntax::{ | 3 | use syntax::{ |
4 | ast::{self, edit::AstNodeEdit, NameOwner}, | 4 | ast::{self, edit::AstNodeEdit, NameOwner}, |
@@ -36,6 +36,8 @@ pub(crate) fn move_module_to_file(acc: &mut Assists, ctx: &AssistContext) -> Opt | |||
36 | 36 | ||
37 | let module_def = ctx.sema.to_def(&module_ast)?; | 37 | let module_def = ctx.sema.to_def(&module_ast)?; |
38 | let parent_module = module_def.parent(ctx.db())?; | 38 | let parent_module = module_def.parent(ctx.db())?; |
39 | let vis_str = | ||
40 | if let Some(v) = module_ast.visibility() { v.to_string() + " " } else { "".to_string() }; | ||
39 | 41 | ||
40 | acc.add( | 42 | acc.add( |
41 | AssistId("move_module_to_file", AssistKind::RefactorExtract), | 43 | AssistId("move_module_to_file", AssistKind::RefactorExtract), |
@@ -59,7 +61,10 @@ pub(crate) fn move_module_to_file(acc: &mut Assists, ctx: &AssistContext) -> Opt | |||
59 | items | 61 | items |
60 | }; | 62 | }; |
61 | 63 | ||
62 | builder.replace(module_ast.syntax().text_range(), format!("mod {};", module_name)); | 64 | builder.replace( |
65 | module_ast.syntax().text_range(), | ||
66 | format!("{}mod {};", vis_str, module_name), | ||
67 | ); | ||
63 | 68 | ||
64 | let dst = AnchoredPathBuf { anchor: ctx.frange.file_id, path }; | 69 | let dst = AnchoredPathBuf { anchor: ctx.frange.file_id, path }; |
65 | builder.create_file(dst, contents); | 70 | builder.create_file(dst, contents); |
@@ -138,6 +143,42 @@ fn f() {} | |||
138 | } | 143 | } |
139 | 144 | ||
140 | #[test] | 145 | #[test] |
146 | fn extract_public() { | ||
147 | check_assist( | ||
148 | move_module_to_file, | ||
149 | r#" | ||
150 | pub mod $0tests { | ||
151 | #[test] fn t() {} | ||
152 | } | ||
153 | "#, | ||
154 | r#" | ||
155 | //- /main.rs | ||
156 | pub mod tests; | ||
157 | //- /tests.rs | ||
158 | #[test] fn t() {} | ||
159 | "#, | ||
160 | ); | ||
161 | } | ||
162 | |||
163 | #[test] | ||
164 | fn extract_public_crate() { | ||
165 | check_assist( | ||
166 | move_module_to_file, | ||
167 | r#" | ||
168 | pub(crate) mod $0tests { | ||
169 | #[test] fn t() {} | ||
170 | } | ||
171 | "#, | ||
172 | r#" | ||
173 | //- /main.rs | ||
174 | pub(crate) mod tests; | ||
175 | //- /tests.rs | ||
176 | #[test] fn t() {} | ||
177 | "#, | ||
178 | ); | ||
179 | } | ||
180 | |||
181 | #[test] | ||
141 | fn available_before_curly() { | 182 | fn available_before_curly() { |
142 | mark::check!(available_before_curly); | 183 | mark::check!(available_before_curly); |
143 | check_assist_not_applicable(move_module_to_file, r#"mod m { $0 }"#); | 184 | check_assist_not_applicable(move_module_to_file, r#"mod m { $0 }"#); |