Skip to content

Commit aa04e3b

Browse files
committed
Don't spam loop-rewriting assist
The more focused the assist, the better!
1 parent 358b9a5 commit aa04e3b

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

crates/ide_assists/src/handlers/convert_for_to_iter_for_each.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use hir::known;
33
use ide_db::helpers::FamousDefs;
44
use stdx::format_to;
55
use syntax::{ast, AstNode};
6+
use test_utils::mark;
67

78
use crate::{AssistContext, AssistId, AssistKind, Assists};
89

@@ -13,7 +14,7 @@ use crate::{AssistContext, AssistId, AssistKind, Assists};
1314
// ```
1415
// fn main() {
1516
// let x = vec![1, 2, 3];
16-
// for $0v in x {
17+
// for$0 v in x {
1718
// let y = v * 2;
1819
// }
1920
// }
@@ -32,6 +33,10 @@ pub(crate) fn convert_for_to_iter_for_each(acc: &mut Assists, ctx: &AssistContex
3233
let iterable = for_loop.iterable()?;
3334
let pat = for_loop.pat()?;
3435
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+
}
3540

3641
acc.add(
3742
AssistId("convert_for_to_iter_for_each", AssistKind::RefactorRewrite),
@@ -180,6 +185,21 @@ fn main() {
180185
)
181186
}
182187

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+
183203
#[test]
184204
fn test_for_borrowed() {
185205
check_assist_with_fixtures(

crates/ide_assists/src/handlers/replace_let_with_if_let.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::iter::once;
22

3+
use ide_db::ty_filter::TryEnum;
34
use syntax::{
45
ast::{
56
self,
@@ -10,7 +11,6 @@ use syntax::{
1011
};
1112

1213
use crate::{AssistContext, AssistId, AssistKind, Assists};
13-
use ide_db::ty_filter::TryEnum;
1414

1515
// Assist: replace_let_with_if_let
1616
//

0 commit comments

Comments
 (0)