aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
Diffstat (limited to 'crates')
-rw-r--r--crates/ide_assists/src/handlers/replace_for_loop_with_for_each.rs (renamed from crates/ide_assists/src/handlers/convert_for_to_iter_for_each.rs)38
-rw-r--r--crates/ide_assists/src/handlers/replace_let_with_if_let.rs2
-rw-r--r--crates/ide_assists/src/lib.rs12
-rw-r--r--crates/ide_assists/src/tests.rs2
-rw-r--r--crates/ide_assists/src/tests/generated.rs46
5 files changed, 60 insertions, 40 deletions
diff --git a/crates/ide_assists/src/handlers/convert_for_to_iter_for_each.rs b/crates/ide_assists/src/handlers/replace_for_loop_with_for_each.rs
index 9fddf889c..27da28bc0 100644
--- a/crates/ide_assists/src/handlers/convert_for_to_iter_for_each.rs
+++ b/crates/ide_assists/src/handlers/replace_for_loop_with_for_each.rs
@@ -3,17 +3,18 @@ use hir::known;
3use ide_db::helpers::FamousDefs; 3use ide_db::helpers::FamousDefs;
4use stdx::format_to; 4use stdx::format_to;
5use syntax::{ast, AstNode}; 5use syntax::{ast, AstNode};
6use test_utils::mark;
6 7
7use crate::{AssistContext, AssistId, AssistKind, Assists}; 8use crate::{AssistContext, AssistId, AssistKind, Assists};
8 9
9// Assist: convert_for_to_iter_for_each 10// Assist: replace_for_loop_with_for_each
10// 11//
11// Converts a for loop into a for_each loop on the Iterator. 12// Converts a for loop into a for_each loop on the Iterator.
12// 13//
13// ``` 14// ```
14// fn main() { 15// fn main() {
15// let x = vec![1, 2, 3]; 16// let x = vec![1, 2, 3];
16// for $0v in x { 17// for$0 v in x {
17// let y = v * 2; 18// let y = v * 2;
18// } 19// }
19// } 20// }
@@ -27,15 +28,19 @@ use crate::{AssistContext, AssistId, AssistKind, Assists};
27// }); 28// });
28// } 29// }
29// ``` 30// ```
30pub(crate) fn convert_for_to_iter_for_each(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { 31pub(crate) fn replace_for_loop_with_for_each(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
31 let for_loop = ctx.find_node_at_offset::<ast::ForExpr>()?; 32 let for_loop = ctx.find_node_at_offset::<ast::ForExpr>()?;
32 let iterable = for_loop.iterable()?; 33 let iterable = for_loop.iterable()?;
33 let pat = for_loop.pat()?; 34 let pat = for_loop.pat()?;
34 let body = for_loop.loop_body()?; 35 let body = for_loop.loop_body()?;
36 if body.syntax().text_range().start() < ctx.offset() {
37 mark::hit!(not_available_in_body);
38 return None;
39 }
35 40
36 acc.add( 41 acc.add(
37 AssistId("convert_for_to_iter_for_each", AssistKind::RefactorRewrite), 42 AssistId("replace_for_loop_with_for_each", AssistKind::RefactorRewrite),
38 "Convert a for loop into an Iterator::for_each", 43 "Replace this for loop with `Iterator::for_each`",
39 for_loop.syntax().text_range(), 44 for_loop.syntax().text_range(),
40 |builder| { 45 |builder| {
41 let mut buf = String::new(); 46 let mut buf = String::new();
@@ -145,13 +150,13 @@ pub struct NoIterMethod;
145 FamousDefs::FIXTURE, 150 FamousDefs::FIXTURE,
146 EMPTY_ITER_FIXTURE 151 EMPTY_ITER_FIXTURE
147 ); 152 );
148 check_assist(convert_for_to_iter_for_each, before, after); 153 check_assist(replace_for_loop_with_for_each, before, after);
149 } 154 }
150 155
151 #[test] 156 #[test]
152 fn test_not_for() { 157 fn test_not_for() {
153 check_assist_not_applicable( 158 check_assist_not_applicable(
154 convert_for_to_iter_for_each, 159 replace_for_loop_with_for_each,
155 r" 160 r"
156let mut x = vec![1, 2, 3]; 161let mut x = vec![1, 2, 3];
157x.iter_mut().$0for_each(|v| *v *= 2); 162x.iter_mut().$0for_each(|v| *v *= 2);
@@ -162,7 +167,7 @@ x.iter_mut().$0for_each(|v| *v *= 2);
162 #[test] 167 #[test]
163 fn test_simple_for() { 168 fn test_simple_for() {
164 check_assist( 169 check_assist(
165 convert_for_to_iter_for_each, 170 replace_for_loop_with_for_each,
166 r" 171 r"
167fn main() { 172fn main() {
168 let x = vec![1, 2, 3]; 173 let x = vec![1, 2, 3];
@@ -181,6 +186,21 @@ fn main() {
181 } 186 }
182 187
183 #[test] 188 #[test]
189 fn not_available_in_body() {
190 mark::check!(not_available_in_body);
191 check_assist_not_applicable(
192 replace_for_loop_with_for_each,
193 r"
194fn main() {
195 let x = vec![1, 2, 3];
196 for v in x {
197 $0v *= 2;
198 }
199}",
200 )
201 }
202
203 #[test]
184 fn test_for_borrowed() { 204 fn test_for_borrowed() {
185 check_assist_with_fixtures( 205 check_assist_with_fixtures(
186 r" 206 r"
@@ -255,7 +275,7 @@ fn main() {
255 #[test] 275 #[test]
256 fn test_for_borrowed_mut_behind_var() { 276 fn test_for_borrowed_mut_behind_var() {
257 check_assist( 277 check_assist(
258 convert_for_to_iter_for_each, 278 replace_for_loop_with_for_each,
259 r" 279 r"
260fn main() { 280fn main() {
261 let x = vec![1, 2, 3]; 281 let x = vec![1, 2, 3];
diff --git a/crates/ide_assists/src/handlers/replace_let_with_if_let.rs b/crates/ide_assists/src/handlers/replace_let_with_if_let.rs
index 5a27ada6b..be7e724b5 100644
--- a/crates/ide_assists/src/handlers/replace_let_with_if_let.rs
+++ b/crates/ide_assists/src/handlers/replace_let_with_if_let.rs
@@ -1,5 +1,6 @@
1use std::iter::once; 1use std::iter::once;
2 2
3use ide_db::ty_filter::TryEnum;
3use syntax::{ 4use syntax::{
4 ast::{ 5 ast::{
5 self, 6 self,
@@ -10,7 +11,6 @@ use syntax::{
10}; 11};
11 12
12use crate::{AssistContext, AssistId, AssistKind, Assists}; 13use crate::{AssistContext, AssistId, AssistKind, Assists};
13use ide_db::ty_filter::TryEnum;
14 14
15// Assist: replace_let_with_if_let 15// Assist: replace_let_with_if_let
16// 16//
diff --git a/crates/ide_assists/src/lib.rs b/crates/ide_assists/src/lib.rs
index 4c067d451..53542d433 100644
--- a/crates/ide_assists/src/lib.rs
+++ b/crates/ide_assists/src/lib.rs
@@ -114,7 +114,6 @@ mod handlers {
114 mod apply_demorgan; 114 mod apply_demorgan;
115 mod auto_import; 115 mod auto_import;
116 mod change_visibility; 116 mod change_visibility;
117 mod convert_for_to_iter_for_each;
118 mod convert_integer_literal; 117 mod convert_integer_literal;
119 mod early_return; 118 mod early_return;
120 mod expand_glob_import; 119 mod expand_glob_import;
@@ -132,8 +131,8 @@ mod handlers {
132 mod generate_enum_projection_method; 131 mod generate_enum_projection_method;
133 mod generate_from_impl_for_enum; 132 mod generate_from_impl_for_enum;
134 mod generate_function; 133 mod generate_function;
135 mod generate_getter;
136 mod generate_getter_mut; 134 mod generate_getter_mut;
135 mod generate_getter;
137 mod generate_impl; 136 mod generate_impl;
138 mod generate_new; 137 mod generate_new;
139 mod generate_setter; 138 mod generate_setter;
@@ -156,6 +155,7 @@ mod handlers {
156 mod reorder_fields; 155 mod reorder_fields;
157 mod reorder_impl; 156 mod reorder_impl;
158 mod replace_derive_with_manual_impl; 157 mod replace_derive_with_manual_impl;
158 mod replace_for_loop_with_for_each;
159 mod replace_if_let_with_match; 159 mod replace_if_let_with_match;
160 mod replace_impl_trait_with_generic; 160 mod replace_impl_trait_with_generic;
161 mod replace_let_with_if_let; 161 mod replace_let_with_if_let;
@@ -177,11 +177,9 @@ mod handlers {
177 apply_demorgan::apply_demorgan, 177 apply_demorgan::apply_demorgan,
178 auto_import::auto_import, 178 auto_import::auto_import,
179 change_visibility::change_visibility, 179 change_visibility::change_visibility,
180 convert_for_to_iter_for_each::convert_for_to_iter_for_each,
181 convert_integer_literal::convert_integer_literal, 180 convert_integer_literal::convert_integer_literal,
182 early_return::convert_to_guarded_return, 181 early_return::convert_to_guarded_return,
183 expand_glob_import::expand_glob_import, 182 expand_glob_import::expand_glob_import,
184 move_module_to_file::move_module_to_file,
185 extract_struct_from_enum_variant::extract_struct_from_enum_variant, 183 extract_struct_from_enum_variant::extract_struct_from_enum_variant,
186 fill_match_arms::fill_match_arms, 184 fill_match_arms::fill_match_arms,
187 fix_visibility::fix_visibility, 185 fix_visibility::fix_visibility,
@@ -191,12 +189,12 @@ mod handlers {
191 generate_default_from_enum_variant::generate_default_from_enum_variant, 189 generate_default_from_enum_variant::generate_default_from_enum_variant,
192 generate_derive::generate_derive, 190 generate_derive::generate_derive,
193 generate_enum_is_method::generate_enum_is_method, 191 generate_enum_is_method::generate_enum_is_method,
194 generate_enum_projection_method::generate_enum_try_into_method,
195 generate_enum_projection_method::generate_enum_as_method, 192 generate_enum_projection_method::generate_enum_as_method,
193 generate_enum_projection_method::generate_enum_try_into_method,
196 generate_from_impl_for_enum::generate_from_impl_for_enum, 194 generate_from_impl_for_enum::generate_from_impl_for_enum,
197 generate_function::generate_function, 195 generate_function::generate_function,
198 generate_getter::generate_getter,
199 generate_getter_mut::generate_getter_mut, 196 generate_getter_mut::generate_getter_mut,
197 generate_getter::generate_getter,
200 generate_impl::generate_impl, 198 generate_impl::generate_impl,
201 generate_new::generate_new, 199 generate_new::generate_new,
202 generate_setter::generate_setter, 200 generate_setter::generate_setter,
@@ -210,6 +208,7 @@ mod handlers {
210 move_bounds::move_bounds_to_where_clause, 208 move_bounds::move_bounds_to_where_clause,
211 move_guard::move_arm_cond_to_match_guard, 209 move_guard::move_arm_cond_to_match_guard,
212 move_guard::move_guard_to_arm_body, 210 move_guard::move_guard_to_arm_body,
211 move_module_to_file::move_module_to_file,
213 pull_assignment_up::pull_assignment_up, 212 pull_assignment_up::pull_assignment_up,
214 qualify_path::qualify_path, 213 qualify_path::qualify_path,
215 raw_string::add_hash, 214 raw_string::add_hash,
@@ -221,6 +220,7 @@ mod handlers {
221 reorder_fields::reorder_fields, 220 reorder_fields::reorder_fields,
222 reorder_impl::reorder_impl, 221 reorder_impl::reorder_impl,
223 replace_derive_with_manual_impl::replace_derive_with_manual_impl, 222 replace_derive_with_manual_impl::replace_derive_with_manual_impl,
223 replace_for_loop_with_for_each::replace_for_loop_with_for_each,
224 replace_if_let_with_match::replace_if_let_with_match, 224 replace_if_let_with_match::replace_if_let_with_match,
225 replace_if_let_with_match::replace_match_with_if_let, 225 replace_if_let_with_match::replace_match_with_if_let,
226 replace_impl_trait_with_generic::replace_impl_trait_with_generic, 226 replace_impl_trait_with_generic::replace_impl_trait_with_generic,
diff --git a/crates/ide_assists/src/tests.rs b/crates/ide_assists/src/tests.rs
index 384eb7eee..b7f616760 100644
--- a/crates/ide_assists/src/tests.rs
+++ b/crates/ide_assists/src/tests.rs
@@ -190,8 +190,8 @@ fn assist_order_field_struct() {
190 let mut assists = assists.iter(); 190 let mut assists = assists.iter();
191 191
192 assert_eq!(assists.next().expect("expected assist").label, "Change visibility to pub(crate)"); 192 assert_eq!(assists.next().expect("expected assist").label, "Change visibility to pub(crate)");
193 assert_eq!(assists.next().expect("expected assist").label, "Generate a getter method");
194 assert_eq!(assists.next().expect("expected assist").label, "Generate a mut getter method"); 193 assert_eq!(assists.next().expect("expected assist").label, "Generate a mut getter method");
194 assert_eq!(assists.next().expect("expected assist").label, "Generate a getter method");
195 assert_eq!(assists.next().expect("expected assist").label, "Generate a setter method"); 195 assert_eq!(assists.next().expect("expected assist").label, "Generate a setter method");
196 assert_eq!(assists.next().expect("expected assist").label, "Add `#[derive]`"); 196 assert_eq!(assists.next().expect("expected assist").label, "Add `#[derive]`");
197} 197}
diff --git a/crates/ide_assists/src/tests/generated.rs b/crates/ide_assists/src/tests/generated.rs
index 7f6dbbccf..4f007aa48 100644
--- a/crates/ide_assists/src/tests/generated.rs
+++ b/crates/ide_assists/src/tests/generated.rs
@@ -193,29 +193,6 @@ pub(crate) fn frobnicate() {}
193} 193}
194 194
195#[test] 195#[test]
196fn doctest_convert_for_to_iter_for_each() {
197 check_doc_test(
198 "convert_for_to_iter_for_each",
199 r#####"
200fn main() {
201 let x = vec![1, 2, 3];
202 for $0v in x {
203 let y = v * 2;
204 }
205}
206"#####,
207 r#####"
208fn main() {
209 let x = vec![1, 2, 3];
210 x.into_iter().for_each(|v| {
211 let y = v * 2;
212 });
213}
214"#####,
215 )
216}
217
218#[test]
219fn doctest_convert_integer_literal() { 196fn doctest_convert_integer_literal() {
220 check_doc_test( 197 check_doc_test(
221 "convert_integer_literal", 198 "convert_integer_literal",
@@ -1180,6 +1157,29 @@ impl Debug for S {
1180} 1157}
1181 1158
1182#[test] 1159#[test]
1160fn doctest_replace_for_loop_with_for_each() {
1161 check_doc_test(
1162 "replace_for_loop_with_for_each",
1163 r#####"
1164fn main() {
1165 let x = vec![1, 2, 3];
1166 for$0 v in x {
1167 let y = v * 2;
1168 }
1169}
1170"#####,
1171 r#####"
1172fn main() {
1173 let x = vec![1, 2, 3];
1174 x.into_iter().for_each(|v| {
1175 let y = v * 2;
1176 });
1177}
1178"#####,
1179 )
1180}
1181
1182#[test]
1183fn doctest_replace_if_let_with_match() { 1183fn doctest_replace_if_let_with_match() {
1184 check_doc_test( 1184 check_doc_test(
1185 "replace_if_let_with_match", 1185 "replace_if_let_with_match",