aboutsummaryrefslogtreecommitdiff
path: root/crates/ide_assists/src/handlers
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2021-02-28 18:14:53 +0000
committerGitHub <[email protected]>2021-02-28 18:14:53 +0000
commit0a913fd11194faff9c0100bc78f2d4fe682075aa (patch)
treeab17d9e641f19bcf514673c9ed5e9c35646fe269 /crates/ide_assists/src/handlers
parent358b9a50f73000711371a82546161956f53f8c9c (diff)
parent406d96c7d4c3a78d42b58a91ba633333ec37d487 (diff)
Merge #7812
7812: Use consistent naming for assist r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ide_assists/src/handlers')
-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
2 files changed, 30 insertions, 10 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//