Skip to content

Commit f678613

Browse files
committed
refactor: refactored and reduced assist code
1 parent 6236b1e commit f678613

File tree

1 file changed

+21
-36
lines changed

1 file changed

+21
-36
lines changed

crates/ide_assists/src/handlers/convert_iter_for_each_to_for.rs

Lines changed: 21 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -28,53 +28,37 @@ use crate::{AssistContext, AssistId, AssistKind, Assists};
2828
/// }
2929
/// ```
3030
pub(crate) fn convert_iter_for_each_to_for(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
31-
let method;
32-
33-
let stmt = if let Some(stmt) = ctx.find_node_at_offset::<ast::ExprStmt>() {
34-
method = ast::MethodCallExpr::cast(stmt.syntax().first_child()?)?;
35-
Some(stmt)
36-
} else {
37-
method = match ctx.find_node_at_offset::<ast::Expr>()? {
38-
ast::Expr::MethodCallExpr(expr) => expr,
39-
ast::Expr::ClosureExpr(expr) => {
40-
ast::MethodCallExpr::cast(expr.syntax().ancestors().nth(2)?)?
41-
}
42-
_ => {
43-
return None;
44-
}
45-
};
46-
None
47-
};
31+
let method = ctx.find_node_at_offset::<ast::MethodCallExpr>()?;
32+
let stmt = method.syntax().parent().and_then(ast::ExprStmt::cast);
4833

4934
let closure = match method.arg_list()?.args().next()? {
5035
ast::Expr::ClosureExpr(expr) => expr,
51-
_ => {
52-
return None;
53-
}
36+
_ => return None,
5437
};
5538

56-
let (method, parent) = validate_method_call_expr(&ctx.sema, method)?;
39+
let (method, receiver) = validate_method_call_expr(&ctx.sema, method)?;
5740

5841
let param_list = closure.param_list()?;
5942
let param = param_list.params().next()?.pat()?;
6043
let body = closure.body()?;
6144

62-
let indent = stmt.as_ref().map_or(method.indent_level(), |stmt| stmt.indent_level());
6345
let syntax = stmt.as_ref().map_or(method.syntax(), |stmt| stmt.syntax());
6446

6547
acc.add(
6648
AssistId("convert_iter_for_each_to_for", AssistKind::RefactorRewrite),
6749
"Replace this `Iterator::for_each` with a for loop",
6850
syntax.text_range(),
6951
|builder| {
52+
let indent = stmt.as_ref().map_or(method.indent_level(), |stmt| stmt.indent_level());
53+
7054
let block = match body {
7155
ast::Expr::BlockExpr(block) => block,
7256
_ => make::block_expr(Vec::new(), Some(body)),
7357
}
7458
.reset_indent()
7559
.indent(indent);
7660

77-
let expr_for_loop = make::expr_for_loop(param, parent, block);
61+
let expr_for_loop = make::expr_for_loop(param, receiver, block);
7862
builder.replace(syntax.text_range(), expr_for_loop.syntax().text())
7963
},
8064
)
@@ -88,15 +72,15 @@ fn validate_method_call_expr(
8872
return None;
8973
}
9074

75+
let receiver = expr.receiver()?;
9176
let expr = ast::Expr::MethodCallExpr(expr);
92-
let parent = ast::Expr::cast(expr.syntax().first_child()?)?;
9377

94-
let it_type = sema.type_of_expr(&parent)?;
95-
let module = sema.scope(parent.syntax()).module()?;
78+
let it_type = sema.type_of_expr(&receiver)?;
79+
let module = sema.scope(receiver.syntax()).module()?;
9680
let krate = module.krate();
9781

9882
let iter_trait = FamousDefs(sema, Some(krate)).core_iter_Iterator()?;
99-
it_type.impls_trait(sema.db, iter_trait, &[]).then(|| (expr, parent))
83+
it_type.impls_trait(sema.db, iter_trait, &[]).then(|| (expr, receiver))
10084
}
10185

10286
#[cfg(test)]
@@ -175,34 +159,36 @@ fn main() {
175159
}
176160

177161
#[test]
178-
fn test_for_each_without_braces_stmt() {
162+
fn test_for_each_in_iter_stmt() {
179163
check_assist_with_fixtures(
180164
r#"
181165
use empty_iter::*;
182166
fn main() {
183-
let x = Empty;
184-
x.iter().$0for_each(|(x, y)| println!("x: {}, y: {}", x, y));
167+
let x = Empty.iter();
168+
x.$0for_each(|(x, y)| {
169+
println!("x: {}, y: {}", x, y);
170+
});
185171
}"#,
186172
r#"
187173
use empty_iter::*;
188174
fn main() {
189-
let x = Empty;
190-
for (x, y) in x.iter() {
191-
println!("x: {}, y: {}", x, y)
175+
let x = Empty.iter();
176+
for (x, y) in x {
177+
println!("x: {}, y: {}", x, y);
192178
}
193179
}
194180
"#,
195181
)
196182
}
197183

198184
#[test]
199-
fn test_for_each_in_closure_stmt() {
185+
fn test_for_each_without_braces_stmt() {
200186
check_assist_with_fixtures(
201187
r#"
202188
use empty_iter::*;
203189
fn main() {
204190
let x = Empty;
205-
x.iter().for_each($0|(x, y)| println!("x: {}, y: {}", x, y));
191+
x.iter().$0for_each(|(x, y)| println!("x: {}, y: {}", x, y));
206192
}"#,
207193
r#"
208194
use empty_iter::*;
@@ -215,7 +201,6 @@ fn main() {
215201
"#,
216202
)
217203
}
218-
219204
#[test]
220205
fn test_for_each_not_applicable() {
221206
check_assist_not_applicable(

0 commit comments

Comments
 (0)