aboutsummaryrefslogtreecommitdiff
path: root/crates/assists
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2021-01-06 13:33:25 +0000
committerGitHub <[email protected]>2021-01-06 13:33:25 +0000
commit655ac47ee3fc27de8281b4962831cf051b1e9118 (patch)
tree4818fcd60f5f4110037faac367744c925d5f5075 /crates/assists
parented732e86eb88393cdec471b263303adea6ffcb73 (diff)
parent25edc3c60a8fe9a01b1ff7b6a20ceb83d78e2965 (diff)
Merge #7178
7178: Better target for move module r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/assists')
-rw-r--r--crates/assists/src/handlers/move_module_to_file.rs (renamed from crates/assists/src/handlers/extract_module_to_file.rs)48
-rw-r--r--crates/assists/src/lib.rs4
-rw-r--r--crates/assists/src/tests/generated.rs30
3 files changed, 47 insertions, 35 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;
2use ide_db::base_db::AnchoredPathBuf; 2use ide_db::base_db::AnchoredPathBuf;
3use syntax::{ 3use syntax::{
4 ast::{self, edit::AstNodeEdit, NameOwner}, 4 ast::{self, edit::AstNodeEdit, NameOwner},
5 AstNode, 5 AstNode, TextRange,
6}; 6};
7use test_utils::mark;
7 8
8use crate::{AssistContext, AssistId, AssistKind, Assists}; 9use 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// ```
23pub(crate) fn extract_module_to_file(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { 24pub(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)]
65mod tests { 71mod 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#"
75mod tests {<|> 81mod <|>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
94mod submod; 100mod submod;
95//- /submod.rs 101//- /submod.rs
96mod inner<|> { 102<|>mod inner {
97 fn f() {} 103 fn f() {}
98} 104}
99fn g() {} 105fn 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
117mod submodule; 123mod 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}
diff --git a/crates/assists/src/lib.rs b/crates/assists/src/lib.rs
index 9c2a95735..5e3a1b368 100644
--- a/crates/assists/src/lib.rs
+++ b/crates/assists/src/lib.rs
@@ -116,7 +116,6 @@ mod handlers {
116 mod convert_integer_literal; 116 mod convert_integer_literal;
117 mod early_return; 117 mod early_return;
118 mod expand_glob_import; 118 mod expand_glob_import;
119 mod extract_module_to_file;
120 mod extract_struct_from_enum_variant; 119 mod extract_struct_from_enum_variant;
121 mod extract_variable; 120 mod extract_variable;
122 mod fill_match_arms; 121 mod fill_match_arms;
@@ -139,6 +138,7 @@ mod handlers {
139 mod merge_match_arms; 138 mod merge_match_arms;
140 mod move_bounds; 139 mod move_bounds;
141 mod move_guard; 140 mod move_guard;
141 mod move_module_to_file;
142 mod pull_assignment_up; 142 mod pull_assignment_up;
143 mod qualify_path; 143 mod qualify_path;
144 mod raw_string; 144 mod raw_string;
@@ -169,7 +169,7 @@ mod handlers {
169 convert_integer_literal::convert_integer_literal, 169 convert_integer_literal::convert_integer_literal,
170 early_return::convert_to_guarded_return, 170 early_return::convert_to_guarded_return,
171 expand_glob_import::expand_glob_import, 171 expand_glob_import::expand_glob_import,
172 extract_module_to_file::extract_module_to_file, 172 move_module_to_file::move_module_to_file,
173 extract_struct_from_enum_variant::extract_struct_from_enum_variant, 173 extract_struct_from_enum_variant::extract_struct_from_enum_variant,
174 extract_variable::extract_variable, 174 extract_variable::extract_variable,
175 fill_match_arms::fill_match_arms, 175 fill_match_arms::fill_match_arms,
diff --git a/crates/assists/src/tests/generated.rs b/crates/assists/src/tests/generated.rs
index b15352cf3..fdebee4fe 100644
--- a/crates/assists/src/tests/generated.rs
+++ b/crates/assists/src/tests/generated.rs
@@ -238,21 +238,6 @@ fn qux(bar: Bar, baz: Baz) {}
238} 238}
239 239
240#[test] 240#[test]
241fn doctest_extract_module_to_file() {
242 check_doc_test(
243 "extract_module_to_file",
244 r#####"
245mod foo {<|>
246 fn t() {}
247}
248"#####,
249 r#####"
250mod foo;
251"#####,
252 )
253}
254
255#[test]
256fn doctest_extract_struct_from_enum_variant() { 241fn doctest_extract_struct_from_enum_variant() {
257 check_doc_test( 242 check_doc_test(
258 "extract_struct_from_enum_variant", 243 "extract_struct_from_enum_variant",
@@ -761,6 +746,21 @@ fn handle(action: Action) {
761} 746}
762 747
763#[test] 748#[test]
749fn doctest_move_module_to_file() {
750 check_doc_test(
751 "move_module_to_file",
752 r#####"
753mod <|>foo {
754 fn t() {}
755}
756"#####,
757 r#####"
758mod foo;
759"#####,
760 )
761}
762
763#[test]
764fn doctest_pull_assignment_up() { 764fn doctest_pull_assignment_up() {
765 check_doc_test( 765 check_doc_test(
766 "pull_assignment_up", 766 "pull_assignment_up",