diff options
author | Aleksey Kladov <[email protected]> | 2021-02-28 18:08:51 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2021-02-28 18:08:51 +0000 |
commit | aa04e3bbb2fab8ee7f7aa8eb406943d314976a0d (patch) | |
tree | 9918df0e4041ad8b77f33b34eff4b982f3d77430 | |
parent | 358b9a50f73000711371a82546161956f53f8c9c (diff) |
Don't spam loop-rewriting assist
The more focused the assist, the better!
-rw-r--r-- | crates/ide_assists/src/handlers/convert_for_to_iter_for_each.rs | 22 | ||||
-rw-r--r-- | crates/ide_assists/src/handlers/replace_let_with_if_let.rs | 2 |
2 files changed, 22 insertions, 2 deletions
diff --git a/crates/ide_assists/src/handlers/convert_for_to_iter_for_each.rs b/crates/ide_assists/src/handlers/convert_for_to_iter_for_each.rs index 9fddf889c..011df1c18 100644 --- a/crates/ide_assists/src/handlers/convert_for_to_iter_for_each.rs +++ b/crates/ide_assists/src/handlers/convert_for_to_iter_for_each.rs | |||
@@ -3,6 +3,7 @@ use hir::known; | |||
3 | use ide_db::helpers::FamousDefs; | 3 | use ide_db::helpers::FamousDefs; |
4 | use stdx::format_to; | 4 | use stdx::format_to; |
5 | use syntax::{ast, AstNode}; | 5 | use syntax::{ast, AstNode}; |
6 | use test_utils::mark; | ||
6 | 7 | ||
7 | use crate::{AssistContext, AssistId, AssistKind, Assists}; | 8 | use crate::{AssistContext, AssistId, AssistKind, Assists}; |
8 | 9 | ||
@@ -13,7 +14,7 @@ use crate::{AssistContext, AssistId, AssistKind, Assists}; | |||
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 | // } |
@@ -32,6 +33,10 @@ pub(crate) fn convert_for_to_iter_for_each(acc: &mut Assists, ctx: &AssistContex | |||
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("convert_for_to_iter_for_each", AssistKind::RefactorRewrite), |
@@ -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 | convert_for_to_iter_for_each, | ||
193 | r" | ||
194 | fn 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" |
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 @@ | |||
1 | use std::iter::once; | 1 | use std::iter::once; |
2 | 2 | ||
3 | use ide_db::ty_filter::TryEnum; | ||
3 | use syntax::{ | 4 | use syntax::{ |
4 | ast::{ | 5 | ast::{ |
5 | self, | 6 | self, |
@@ -10,7 +11,6 @@ use syntax::{ | |||
10 | }; | 11 | }; |
11 | 12 | ||
12 | use crate::{AssistContext, AssistId, AssistKind, Assists}; | 13 | use crate::{AssistContext, AssistId, AssistKind, Assists}; |
13 | use ide_db::ty_filter::TryEnum; | ||
14 | 14 | ||
15 | // Assist: replace_let_with_if_let | 15 | // Assist: replace_let_with_if_let |
16 | // | 16 | // |