3 edit::AstNodeEdit, make, AstNode, BlockExpr, Condition, ElseBranch, Expr, IfExpr, MatchArm,
5 SyntaxKind::WHITESPACE,
8 use crate::{AssistContext, AssistId, AssistKind, Assists};
10 // Assist: move_guard_to_arm_body
12 // Moves match guard into match arm body.
15 // enum Action { Move { distance: u32 }, Stop }
17 // fn handle(action: Action) {
19 // Action::Move { distance } $0if distance > 10 => foo(),
26 // enum Action { Move { distance: u32 }, Stop }
28 // fn handle(action: Action) {
30 // Action::Move { distance } => if distance > 10 {
37 pub(crate) fn move_guard_to_arm_body(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
38 let match_arm = ctx.find_node_at_offset::<MatchArm>()?;
39 let guard = match_arm.guard()?;
40 if ctx.offset() > guard.syntax().text_range().end() {
41 cov_mark::hit!(move_guard_unapplicable_in_arm_body);
44 let space_before_guard = guard.syntax().prev_sibling_or_token();
46 // FIXME: support `if let` guards too
47 if guard.let_token().is_some() {
50 let guard_condition = guard.expr()?;
51 let arm_expr = match_arm.expr()?;
52 let if_expr = make::expr_if(
53 make::condition(guard_condition, None),
54 make::block_expr(None, Some(arm_expr.clone())),
57 .indent(arm_expr.indent_level());
59 let target = guard.syntax().text_range();
61 AssistId("move_guard_to_arm_body", AssistKind::RefactorRewrite),
62 "Move guard to arm body",
65 match space_before_guard {
66 Some(element) if element.kind() == WHITESPACE => {
67 edit.delete(element.text_range());
72 edit.delete(guard.syntax().text_range());
73 edit.replace_ast(arm_expr, if_expr);
78 // Assist: move_arm_cond_to_match_guard
80 // Moves if expression from match arm body into a guard.
83 // enum Action { Move { distance: u32 }, Stop }
85 // fn handle(action: Action) {
87 // Action::Move { distance } => $0if distance > 10 { foo() },
94 // enum Action { Move { distance: u32 }, Stop }
96 // fn handle(action: Action) {
98 // Action::Move { distance } if distance > 10 => foo(),
99 // Action::Move { distance } => {}
104 pub(crate) fn move_arm_cond_to_match_guard(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
105 let match_arm: MatchArm = ctx.find_node_at_offset::<MatchArm>()?;
106 let match_pat = match_arm.pat()?;
107 let arm_body = match_arm.expr()?;
109 let mut replace_node = None;
110 let if_expr: IfExpr = IfExpr::cast(arm_body.syntax().clone()).or_else(|| {
111 let block_expr = BlockExpr::cast(arm_body.syntax().clone())?;
112 if let Expr::IfExpr(e) = block_expr.tail_expr()? {
113 replace_node = Some(block_expr.syntax().clone());
119 let replace_node = replace_node.unwrap_or_else(|| if_expr.syntax().clone());
120 let needs_dedent = replace_node != *if_expr.syntax();
121 let (conds_blocks, tail) = parse_if_chain(if_expr)?;
124 AssistId("move_arm_cond_to_match_guard", AssistKind::RefactorRewrite),
125 "Move condition to match guard",
126 replace_node.text_range(),
128 edit.delete(match_arm.syntax().text_range());
129 // Dedent if if_expr is in a BlockExpr
130 let dedent = if needs_dedent {
131 cov_mark::hit!(move_guard_ifelse_in_block);
134 cov_mark::hit!(move_guard_ifelse_else_block);
137 let then_arm_end = match_arm.syntax().text_range().end();
138 let indent_level = match_arm.indent_level();
139 let spaces = " ".repeat(indent_level.0 as _);
141 let mut first = true;
142 for (cond, block) in conds_blocks {
144 edit.insert(then_arm_end, format!("\n{}", spaces));
148 let guard = format!("{} if {} => ", match_pat, cond.syntax().text());
149 edit.insert(then_arm_end, guard);
150 let only_expr = block.statements().next().is_none();
151 match &block.tail_expr() {
152 Some(then_expr) if only_expr => {
153 edit.insert(then_arm_end, then_expr.syntax().text());
154 edit.insert(then_arm_end, ",");
157 let to_insert = block.dedent(dedent.into()).syntax().text();
158 edit.insert(then_arm_end, to_insert)
162 if let Some(e) = tail {
163 cov_mark::hit!(move_guard_ifelse_else_tail);
164 let guard = format!("\n{}{} => ", spaces, match_pat);
165 edit.insert(then_arm_end, guard);
166 let only_expr = e.statements().next().is_none();
167 match &e.tail_expr() {
168 Some(expr) if only_expr => {
169 cov_mark::hit!(move_guard_ifelse_expr_only);
170 edit.insert(then_arm_end, expr.syntax().text());
171 edit.insert(then_arm_end, ",");
174 let to_insert = e.dedent(dedent.into()).syntax().text();
175 edit.insert(then_arm_end, to_insert)
179 // There's no else branch. Add a pattern without guard
180 cov_mark::hit!(move_guard_ifelse_notail);
181 edit.insert(then_arm_end, format!("\n{}{} => {{}}", spaces, match_pat));
187 // Parses an if-else-if chain to get the conditons and the then branches until we encounter an else
188 // branch or the end.
189 fn parse_if_chain(if_expr: IfExpr) -> Option<(Vec<(Condition, BlockExpr)>, Option<BlockExpr>)> {
190 let mut conds_blocks = Vec::new();
191 let mut curr_if = if_expr;
193 let cond = curr_if.condition()?;
194 // Not support moving if let to arm guard
195 if cond.is_pattern_cond() {
198 conds_blocks.push((cond, curr_if.then_branch()?));
199 match curr_if.else_branch() {
200 Some(ElseBranch::IfExpr(e)) => {
203 Some(ElseBranch::Block(b)) => {
209 Some((conds_blocks, tail))
216 use crate::tests::{check_assist, check_assist_not_applicable, check_assist_target};
219 fn move_guard_to_arm_body_range() {
220 cov_mark::check!(move_guard_unapplicable_in_arm_body);
221 check_assist_not_applicable(
222 move_guard_to_arm_body,
226 x if x > 10 => $0false,
234 fn move_guard_to_arm_body_target() {
236 move_guard_to_arm_body,
240 x $0if x > 10 => false,
250 fn move_guard_to_arm_body_works() {
252 move_guard_to_arm_body,
256 x $0if x > 10 => false,
275 fn move_guard_to_arm_body_works_complex_match() {
277 move_guard_to_arm_body,
281 $0x @ 4 | x @ 5 if x > 5 => true,
289 x @ 4 | x @ 5 => if x > 5 {
300 fn move_arm_cond_to_match_guard_works() {
302 move_arm_cond_to_match_guard,
306 x => if x > 10 { $0false },
314 x if x > 10 => false,
324 fn move_arm_cond_in_block_to_match_guard_works() {
326 move_arm_cond_to_match_guard,
342 x if x > 10 => false,
352 fn move_arm_cond_in_block_to_match_guard_add_comma_works() {
354 move_arm_cond_to_match_guard,
370 x if x > 10 => false,
380 fn move_arm_cond_to_match_guard_if_let_not_works() {
381 check_assist_not_applicable(
382 move_arm_cond_to_match_guard,
386 x => if let 62 = x { $0false },
395 fn move_arm_cond_to_match_guard_if_empty_body_works() {
397 move_arm_cond_to_match_guard,
401 x => if x > 10 { $0 },
419 fn move_arm_cond_to_match_guard_if_multiline_body_works() {
421 move_arm_cond_to_match_guard,
449 fn move_arm_cond_in_block_to_match_guard_if_multiline_body_works() {
451 move_arm_cond_to_match_guard,
481 fn move_arm_cond_to_match_guard_with_else_works() {
483 move_arm_cond_to_match_guard,
499 x if x > 10 => false,
509 fn move_arm_cond_to_match_guard_with_else_block_works() {
510 cov_mark::check!(move_guard_ifelse_expr_only);
512 move_arm_cond_to_match_guard,
530 x if x > 10 => false,
540 fn move_arm_cond_to_match_guard_else_if_empty_body_works() {
542 move_arm_cond_to_match_guard,
546 x => if x > 10 { $0 } else { },
564 fn move_arm_cond_to_match_guard_with_else_multiline_works() {
566 move_arm_cond_to_match_guard,
596 fn move_arm_cond_to_match_guard_with_else_multiline_else_works() {
597 cov_mark::check!(move_guard_ifelse_else_block);
599 move_arm_cond_to_match_guard,
616 x if x > 10 => false,
629 fn move_arm_cond_to_match_guard_with_else_multiline_else_block_works() {
630 cov_mark::check!(move_guard_ifelse_in_block);
632 move_arm_cond_to_match_guard,
651 x if x > 10 => false,
664 fn move_arm_cond_to_match_guard_with_else_last_arm_works() {
666 move_arm_cond_to_match_guard,
686 x if x > 10 => false,
698 fn move_arm_cond_to_match_guard_with_else_comma_works() {
700 move_arm_cond_to_match_guard,
718 x if x > 10 => false,
730 fn move_arm_cond_to_match_guard_elseif() {
732 move_arm_cond_to_match_guard,
753 x if x > 10 => false,
764 fn move_arm_cond_to_match_guard_elseif_in_block() {
765 cov_mark::check!(move_guard_ifelse_in_block);
767 move_arm_cond_to_match_guard,
790 x if x > 10 => false,
801 fn move_arm_cond_to_match_guard_elseif_chain() {
802 cov_mark::check!(move_guard_ifelse_else_tail);
804 move_arm_cond_to_match_guard,
840 fn move_arm_cond_to_match_guard_elseif_iflet() {
841 check_assist_not_applicable(
842 move_arm_cond_to_match_guard,
851 } else if let 4 = 4 {
864 fn move_arm_cond_to_match_guard_elseif_notail() {
865 cov_mark::check!(move_guard_ifelse_notail);
867 move_arm_cond_to_match_guard,