diff options
Diffstat (limited to 'crates/assists/src/handlers')
-rw-r--r-- | crates/assists/src/handlers/move_module_to_file.rs (renamed from crates/assists/src/handlers/extract_module_to_file.rs) | 48 |
1 files changed, 30 insertions, 18 deletions
diff --git a/crates/assists/src/handlers/extract_module_to_file.rs b/crates/assists/src/handlers/move_module_to_file.rs index 50bf67ef7..165faaf61 100644 --- a/crates/assists/src/handlers/extract_module_to_file.rs +++ b/crates/assists/src/handlers/move_module_to_file.rs | |||
@@ -2,17 +2,18 @@ use ast::edit::IndentLevel; | |||
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}, |
5 | AstNode, | 5 | AstNode, TextRange, |
6 | }; | 6 | }; |
7 | use test_utils::mark; | ||
7 | 8 | ||
8 | use crate::{AssistContext, AssistId, AssistKind, Assists}; | 9 | use crate::{AssistContext, AssistId, AssistKind, Assists}; |
9 | 10 | ||
10 | // Assist: extract_module_to_file | 11 | // Assist: move_module_to_file |
11 | // | 12 | // |
12 | // This assist extract module to file. | 13 | // Moves inline module's contents to a separate file. |
13 | // | 14 | // |
14 | // ``` | 15 | // ``` |
15 | // mod foo {<|> | 16 | // mod <|>foo { |
16 | // fn t() {} | 17 | // fn t() {} |
17 | // } | 18 | // } |
18 | // ``` | 19 | // ``` |
@@ -20,19 +21,24 @@ use crate::{AssistContext, AssistId, AssistKind, Assists}; | |||
20 | // ``` | 21 | // ``` |
21 | // mod foo; | 22 | // mod foo; |
22 | // ``` | 23 | // ``` |
23 | pub(crate) fn extract_module_to_file(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { | 24 | pub(crate) fn move_module_to_file(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { |
24 | let module_ast = ctx.find_node_at_offset::<ast::Module>()?; | 25 | let module_ast = ctx.find_node_at_offset::<ast::Module>()?; |
26 | let module_items = module_ast.item_list()?; | ||
27 | |||
28 | let l_curly_offset = module_items.syntax().text_range().start(); | ||
29 | if l_curly_offset <= ctx.offset() { | ||
30 | mark::hit!(available_before_curly); | ||
31 | return None; | ||
32 | } | ||
33 | let target = TextRange::new(module_ast.syntax().text_range().start(), l_curly_offset); | ||
34 | |||
25 | let module_name = module_ast.name()?; | 35 | let module_name = module_ast.name()?; |
26 | 36 | ||
27 | let module_def = ctx.sema.to_def(&module_ast)?; | 37 | let module_def = ctx.sema.to_def(&module_ast)?; |
28 | let parent_module = module_def.parent(ctx.db())?; | 38 | let parent_module = module_def.parent(ctx.db())?; |
29 | 39 | ||
30 | let module_items = module_ast.item_list()?; | ||
31 | let target = module_ast.syntax().text_range(); | ||
32 | let anchor_file_id = ctx.frange.file_id; | ||
33 | |||
34 | acc.add( | 40 | acc.add( |
35 | AssistId("extract_module_to_file", AssistKind::RefactorExtract), | 41 | AssistId("move_module_to_file", AssistKind::RefactorExtract), |
36 | "Extract module to file", | 42 | "Extract module to file", |
37 | target, | 43 | target, |
38 | |builder| { | 44 | |builder| { |
@@ -53,9 +59,9 @@ pub(crate) fn extract_module_to_file(acc: &mut Assists, ctx: &AssistContext) -> | |||
53 | items | 59 | items |
54 | }; | 60 | }; |
55 | 61 | ||
56 | builder.replace(target, format!("mod {};", module_name)); | 62 | builder.replace(module_ast.syntax().text_range(), format!("mod {};", module_name)); |
57 | 63 | ||
58 | let dst = AnchoredPathBuf { anchor: anchor_file_id, path }; | 64 | let dst = AnchoredPathBuf { anchor: ctx.frange.file_id, path }; |
59 | builder.create_file(dst, contents); | 65 | builder.create_file(dst, contents); |
60 | }, | 66 | }, |
61 | ) | 67 | ) |
@@ -63,16 +69,16 @@ pub(crate) fn extract_module_to_file(acc: &mut Assists, ctx: &AssistContext) -> | |||
63 | 69 | ||
64 | #[cfg(test)] | 70 | #[cfg(test)] |
65 | mod tests { | 71 | mod tests { |
66 | use crate::tests::check_assist; | 72 | use crate::tests::{check_assist, check_assist_not_applicable}; |
67 | 73 | ||
68 | use super::*; | 74 | use super::*; |
69 | 75 | ||
70 | #[test] | 76 | #[test] |
71 | fn extract_from_root() { | 77 | fn extract_from_root() { |
72 | check_assist( | 78 | check_assist( |
73 | extract_module_to_file, | 79 | move_module_to_file, |
74 | r#" | 80 | r#" |
75 | mod tests {<|> | 81 | mod <|>tests { |
76 | #[test] fn t() {} | 82 | #[test] fn t() {} |
77 | } | 83 | } |
78 | "#, | 84 | "#, |
@@ -88,12 +94,12 @@ mod tests; | |||
88 | #[test] | 94 | #[test] |
89 | fn extract_from_submodule() { | 95 | fn extract_from_submodule() { |
90 | check_assist( | 96 | check_assist( |
91 | extract_module_to_file, | 97 | move_module_to_file, |
92 | r#" | 98 | r#" |
93 | //- /main.rs | 99 | //- /main.rs |
94 | mod submod; | 100 | mod submod; |
95 | //- /submod.rs | 101 | //- /submod.rs |
96 | mod inner<|> { | 102 | <|>mod inner { |
97 | fn f() {} | 103 | fn f() {} |
98 | } | 104 | } |
99 | fn g() {} | 105 | fn g() {} |
@@ -111,7 +117,7 @@ fn f() {} | |||
111 | #[test] | 117 | #[test] |
112 | fn extract_from_mod_rs() { | 118 | fn extract_from_mod_rs() { |
113 | check_assist( | 119 | check_assist( |
114 | extract_module_to_file, | 120 | move_module_to_file, |
115 | r#" | 121 | r#" |
116 | //- /main.rs | 122 | //- /main.rs |
117 | mod submodule; | 123 | mod submodule; |
@@ -130,4 +136,10 @@ fn f() {} | |||
130 | "#, | 136 | "#, |
131 | ); | 137 | ); |
132 | } | 138 | } |
139 | |||
140 | #[test] | ||
141 | fn available_before_curly() { | ||
142 | mark::check!(available_before_curly); | ||
143 | check_assist_not_applicable(move_module_to_file, r#"mod m { <|> }"#); | ||
144 | } | ||
133 | } | 145 | } |