Skip to content

Commit 00b1984

Browse files
bors[bot]agluszak
andauthored
Merge #10257
10257: assists: turn while into loop r=Veykril a=agluszak Implements an assist to turn a `while` loop into a `loop` loop, as requested in #10214. I'm not sure sure what the guidelines are regarding naming assists convert_x_to_y vs replace_x_with_y. This is my first commit to rust-analyzer :D Thank you `@matklad` for your awesome Explaining rust-analyzer series <3 Closes #10214 Co-authored-by: Andrzej Głuszak <[email protected]>
2 parents ae36af2 + 11a56f8 commit 00b1984

File tree

4 files changed

+220
-0
lines changed

4 files changed

+220
-0
lines changed
Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
use std::iter::once;
2+
3+
use syntax::{
4+
ast::{
5+
self,
6+
edit::{AstNodeEdit, IndentLevel},
7+
make, LoopBodyOwner,
8+
},
9+
AstNode, T,
10+
};
11+
12+
use crate::{
13+
assist_context::{AssistContext, Assists},
14+
utils::invert_boolean_expression,
15+
AssistId, AssistKind,
16+
};
17+
18+
// Assist: convert_while_to_loop
19+
//
20+
// Replace a while with a loop.
21+
//
22+
// ```
23+
// fn main() {
24+
// $0while cond {
25+
// foo();
26+
// }
27+
// }
28+
// ```
29+
// ->
30+
// ```
31+
// fn main() {
32+
// loop {
33+
// if !cond {
34+
// break;
35+
// }
36+
// foo();
37+
// }
38+
// }
39+
// ```
40+
pub(crate) fn convert_while_to_loop(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
41+
let while_kw = ctx.find_token_syntax_at_offset(T![while])?;
42+
let while_expr: ast::WhileExpr = while_kw.parent().and_then(ast::WhileExpr::cast)?;
43+
let while_body = while_expr.loop_body()?;
44+
let cond = while_expr.condition()?;
45+
46+
// Don't handle while let
47+
if let Some(_) = cond.pat() {
48+
return None;
49+
};
50+
51+
let cond_expr = cond.expr()?;
52+
53+
let target = while_expr.syntax().text_range();
54+
acc.add(
55+
AssistId("convert_while_to_loop", AssistKind::RefactorRewrite),
56+
"Convert while to loop",
57+
target,
58+
|edit| {
59+
let while_indent_level = IndentLevel::from_node(while_expr.syntax());
60+
61+
let replacement = {
62+
let if_expr = {
63+
let cond = invert_boolean_expression(cond_expr);
64+
let then_branch = make::block_expr(
65+
once(make::expr_stmt(make::expr_break(None)).into()),
66+
None,
67+
);
68+
69+
make::expr_if(make::condition(cond, None), then_branch, None)
70+
};
71+
72+
let if_expr = if_expr.indent(while_indent_level);
73+
let stmts = once(make::expr_stmt(if_expr).into()).chain(while_body.statements());
74+
75+
let block_expr = make::block_expr(stmts, while_body.tail_expr());
76+
77+
let block_expr = block_expr.indent(while_indent_level);
78+
79+
make::expr_loop(block_expr)
80+
};
81+
82+
edit.replace(target, replacement.syntax().text())
83+
},
84+
)
85+
}
86+
87+
#[cfg(test)]
88+
mod tests {
89+
use crate::tests::{check_assist, check_assist_not_applicable};
90+
91+
use super::*;
92+
93+
#[test]
94+
fn convert_inside_fn() {
95+
check_assist(
96+
convert_while_to_loop,
97+
r#"
98+
fn main() {
99+
while$0 cond {
100+
foo();
101+
}
102+
}
103+
"#,
104+
r#"
105+
fn main() {
106+
loop {
107+
if !cond {
108+
break;
109+
}
110+
foo();
111+
}
112+
}
113+
"#,
114+
);
115+
}
116+
117+
#[test]
118+
fn convert_busy_wait() {
119+
check_assist(
120+
convert_while_to_loop,
121+
r#"
122+
fn main() {
123+
while$0 cond() {}
124+
}
125+
"#,
126+
r#"
127+
fn main() {
128+
loop {
129+
if !cond() {
130+
break;
131+
}
132+
}
133+
}
134+
"#,
135+
);
136+
}
137+
138+
#[test]
139+
fn convert_trailing_expr() {
140+
check_assist(
141+
convert_while_to_loop,
142+
r#"
143+
fn main() {
144+
while$0 cond() {
145+
bar()
146+
}
147+
}
148+
"#,
149+
r#"
150+
fn main() {
151+
loop {
152+
if !cond() {
153+
break;
154+
}
155+
bar()
156+
}
157+
}
158+
"#,
159+
);
160+
}
161+
162+
#[test]
163+
fn ignore_while_let() {
164+
check_assist_not_applicable(
165+
convert_while_to_loop,
166+
r#"
167+
fn main() {
168+
while$0 let Some(_) = foo() {
169+
bar();
170+
}
171+
}
172+
"#,
173+
);
174+
}
175+
176+
#[test]
177+
fn ignore_cursor_in_body() {
178+
check_assist_not_applicable(
179+
convert_while_to_loop,
180+
r#"
181+
fn main() {
182+
while cond {$0
183+
bar();
184+
}
185+
}
186+
"#,
187+
);
188+
}
189+
}

crates/ide_assists/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ mod handlers {
118118
mod convert_iter_for_each_to_for;
119119
mod convert_tuple_struct_to_named_struct;
120120
mod convert_to_guarded_return;
121+
mod convert_while_to_loop;
121122
mod destructure_tuple_binding;
122123
mod expand_glob_import;
123124
mod extract_function;
@@ -191,6 +192,7 @@ mod handlers {
191192
convert_iter_for_each_to_for::convert_iter_for_each_to_for,
192193
convert_to_guarded_return::convert_to_guarded_return,
193194
convert_tuple_struct_to_named_struct::convert_tuple_struct_to_named_struct,
195+
convert_while_to_loop::convert_while_to_loop,
194196
destructure_tuple_binding::destructure_tuple_binding,
195197
expand_glob_import::expand_glob_import,
196198
extract_struct_from_enum_variant::extract_struct_from_enum_variant,

crates/ide_assists/src/tests/generated.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,30 @@ impl Point {
367367
)
368368
}
369369

370+
#[test]
371+
fn doctest_convert_while_to_loop() {
372+
check_doc_test(
373+
"convert_while_to_loop",
374+
r#####"
375+
fn main() {
376+
$0while cond {
377+
foo();
378+
}
379+
}
380+
"#####,
381+
r#####"
382+
fn main() {
383+
loop {
384+
if !cond {
385+
break;
386+
}
387+
foo();
388+
}
389+
}
390+
"#####,
391+
)
392+
}
393+
370394
#[test]
371395
fn doctest_destructure_tuple_binding() {
372396
check_doc_test(

crates/syntax/src/ast/make.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,11 @@ pub fn expr_if(
316316
pub fn expr_for_loop(pat: ast::Pat, expr: ast::Expr, block: ast::BlockExpr) -> ast::Expr {
317317
expr_from_text(&format!("for {} in {} {}", pat, expr, block))
318318
}
319+
320+
pub fn expr_loop(block: ast::BlockExpr) -> ast::Expr {
321+
expr_from_text(&format!("loop {}", block))
322+
}
323+
319324
pub fn expr_prefix(op: SyntaxKind, expr: ast::Expr) -> ast::Expr {
320325
let token = token(op);
321326
expr_from_text(&format!("{}{}", token, expr))

0 commit comments

Comments
 (0)