2 ast::{edit::AstNodeEdit, make, AstNode, BlockExpr, ElseBranch, Expr, IfExpr, MatchArm, Pat},
3 SyntaxKind::WHITESPACE,
6 use crate::{AssistContext, AssistId, AssistKind, Assists};
8 // Assist: move_guard_to_arm_body
10 // Moves match guard into match arm body.
13 // enum Action { Move { distance: u32 }, Stop }
15 // fn handle(action: Action) {
17 // Action::Move { distance } $0if distance > 10 => foo(),
24 // enum Action { Move { distance: u32 }, Stop }
26 // fn handle(action: Action) {
28 // Action::Move { distance } => if distance > 10 {
35 pub(crate) fn move_guard_to_arm_body(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
36 let match_arm = ctx.find_node_at_offset::<MatchArm>()?;
37 let guard = match_arm.guard()?;
38 if ctx.offset() > guard.syntax().text_range().end() {
39 cov_mark::hit!(move_guard_unapplicable_in_arm_body);
42 let space_before_guard = guard.syntax().prev_sibling_or_token();
44 let guard_condition = guard.condition()?;
45 let arm_expr = match_arm.expr()?;
47 make::expr_if(guard_condition, make::block_expr(None, Some(arm_expr.clone())), None)
48 .indent(arm_expr.indent_level());
50 let target = guard.syntax().text_range();
52 AssistId("move_guard_to_arm_body", AssistKind::RefactorRewrite),
53 "Move guard to arm body",
56 match space_before_guard {
57 Some(element) if element.kind() == WHITESPACE => {
58 edit.delete(element.text_range());
63 edit.delete(guard.syntax().text_range());
64 edit.replace_ast(arm_expr, if_expr);
69 // Assist: move_arm_cond_to_match_guard
71 // Moves if expression from match arm body into a guard.
74 // enum Action { Move { distance: u32 }, Stop }
76 // fn handle(action: Action) {
78 // Action::Move { distance } => $0if distance > 10 { foo() },
85 // enum Action { Move { distance: u32 }, Stop }
87 // fn handle(action: Action) {
89 // Action::Move { distance } if distance > 10 => foo(),
94 pub(crate) fn move_arm_cond_to_match_guard(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
95 let match_arm: MatchArm = ctx.find_node_at_offset::<MatchArm>()?;
96 let match_pat = match_arm.pat()?;
97 let arm_body = match_arm.expr()?;
99 let mut replace_node = None;
100 let if_expr: IfExpr = IfExpr::cast(arm_body.syntax().clone()).or_else(|| {
101 let block_expr = BlockExpr::cast(arm_body.syntax().clone())?;
102 if let Expr::IfExpr(e) = block_expr.tail_expr()? {
103 replace_node = Some(block_expr.syntax().clone());
109 let replace_node = replace_node.unwrap_or_else(|| if_expr.syntax().clone());
110 let needs_dedent = replace_node != *if_expr.syntax();
111 let (conds_blocks, tail) = parse_if_chain(if_expr)?;
114 AssistId("move_arm_cond_to_match_guard", AssistKind::RefactorRewrite),
115 "Move condition to match guard",
116 replace_node.text_range(),
118 edit.delete(match_arm.syntax().text_range());
119 // Dedent if if_expr is in a BlockExpr
120 let dedent = if needs_dedent {
121 cov_mark::hit!(move_guard_ifelse_in_block);
124 cov_mark::hit!(move_guard_ifelse_else_block);
127 let then_arm_end = match_arm.syntax().text_range().end();
128 let indent_level = match_arm.indent_level();
129 let spaces = " ".repeat(indent_level.0 as _);
131 let mut first = true;
132 for (cond, block) in conds_blocks {
134 edit.insert(then_arm_end, format!("\n{}", spaces));
138 let guard = format!("{} if {} => ", match_pat, cond.syntax().text());
139 edit.insert(then_arm_end, guard);
140 let only_expr = block.statements().next().is_none();
141 match &block.tail_expr() {
142 Some(then_expr) if only_expr => {
143 edit.insert(then_arm_end, then_expr.syntax().text());
144 edit.insert(then_arm_end, ",");
147 let to_insert = block.dedent(dedent.into()).syntax().text();
148 edit.insert(then_arm_end, to_insert)
152 if let Some(e) = tail {
153 cov_mark::hit!(move_guard_ifelse_else_tail);
154 let guard = format!("\n{}{} => ", spaces, match_pat);
155 edit.insert(then_arm_end, guard);
156 let only_expr = e.statements().next().is_none();
157 match &e.tail_expr() {
158 Some(expr) if only_expr => {
159 cov_mark::hit!(move_guard_ifelse_expr_only);
160 edit.insert(then_arm_end, expr.syntax().text());
161 edit.insert(then_arm_end, ",");
164 let to_insert = e.dedent(dedent.into()).syntax().text();
165 edit.insert(then_arm_end, to_insert)
169 // There's no else branch. Add a pattern without guard, unless the following match
171 cov_mark::hit!(move_guard_ifelse_notail);
172 match match_arm.syntax().next_sibling().and_then(MatchArm::cast) {
174 if matches!(next_arm.pat(), Some(Pat::WildcardPat(_)))
175 && next_arm.guard().is_none() =>
177 cov_mark::hit!(move_guard_ifelse_has_wildcard);
179 _ => edit.insert(then_arm_end, format!("\n{}{} => {{}}", spaces, match_pat)),
186 // Parses an if-else-if chain to get the conditions and the then branches until we encounter an else
187 // branch or the end.
188 fn parse_if_chain(if_expr: IfExpr) -> Option<(Vec<(Expr, BlockExpr)>, Option<BlockExpr>)> {
189 let mut conds_blocks = Vec::new();
190 let mut curr_if = if_expr;
192 let cond = curr_if.condition()?;
193 conds_blocks.push((cond, curr_if.then_branch()?));
194 match curr_if.else_branch() {
195 Some(ElseBranch::IfExpr(e)) => {
198 Some(ElseBranch::Block(b)) => {
204 Some((conds_blocks, tail))
211 use crate::tests::{check_assist, check_assist_not_applicable, check_assist_target};
214 fn move_guard_to_arm_body_range() {
215 cov_mark::check!(move_guard_unapplicable_in_arm_body);
216 check_assist_not_applicable(
217 move_guard_to_arm_body,
221 x if x > 10 => $0false,
229 fn move_guard_to_arm_body_target() {
231 move_guard_to_arm_body,
235 x $0if x > 10 => false,
245 fn move_guard_to_arm_body_works() {
247 move_guard_to_arm_body,
251 x $0if x > 10 => false,
270 fn move_let_guard_to_arm_body_works() {
272 move_guard_to_arm_body,
276 x $0if (let 1 = x) => false,
284 x => if (let 1 = x) {
295 fn move_guard_to_arm_body_works_complex_match() {
297 move_guard_to_arm_body,
301 $0x @ 4 | x @ 5 if x > 5 => true,
309 x @ 4 | x @ 5 => if x > 5 {
320 fn move_arm_cond_to_match_guard_works() {
322 move_arm_cond_to_match_guard,
326 x => if x > 10 { $0false },
334 x if x > 10 => false,
343 fn move_arm_cond_in_block_to_match_guard_works() {
344 cov_mark::check!(move_guard_ifelse_has_wildcard);
346 move_arm_cond_to_match_guard,
362 x if x > 10 => false,
371 fn move_arm_cond_in_block_to_match_guard_no_wildcard_works() {
372 cov_mark::check_count!(move_guard_ifelse_has_wildcard, 0);
374 move_arm_cond_to_match_guard,
389 x if x > 10 => false,
398 fn move_arm_cond_in_block_to_match_guard_wildcard_guard_works() {
399 cov_mark::check_count!(move_guard_ifelse_has_wildcard, 0);
401 move_arm_cond_to_match_guard,
417 x if x > 10 => false,
427 fn move_arm_cond_in_block_to_match_guard_add_comma_works() {
429 move_arm_cond_to_match_guard,
445 x if x > 10 => false,
454 fn move_arm_cond_to_match_guard_if_let_works() {
456 move_arm_cond_to_match_guard,
460 x => if let 62 = x && true { $0false },
468 x if let 62 = x && true => false,
477 fn move_arm_cond_to_match_guard_if_empty_body_works() {
479 move_arm_cond_to_match_guard,
483 x => if x > 10 { $0 },
500 fn move_arm_cond_to_match_guard_if_multiline_body_works() {
502 move_arm_cond_to_match_guard,
529 fn move_arm_cond_in_block_to_match_guard_if_multiline_body_works() {
531 move_arm_cond_to_match_guard,
560 fn move_arm_cond_to_match_guard_with_else_works() {
562 move_arm_cond_to_match_guard,
578 x if x > 10 => false,
588 fn move_arm_cond_to_match_guard_with_else_block_works() {
589 cov_mark::check!(move_guard_ifelse_expr_only);
591 move_arm_cond_to_match_guard,
609 x if x > 10 => false,
619 fn move_arm_cond_to_match_guard_else_if_empty_body_works() {
621 move_arm_cond_to_match_guard,
625 x => if x > 10 { $0 } else { },
643 fn move_arm_cond_to_match_guard_with_else_multiline_works() {
645 move_arm_cond_to_match_guard,
675 fn move_arm_cond_to_match_guard_with_else_multiline_else_works() {
676 cov_mark::check!(move_guard_ifelse_else_block);
678 move_arm_cond_to_match_guard,
695 x if x > 10 => false,
708 fn move_arm_cond_to_match_guard_with_else_multiline_else_block_works() {
709 cov_mark::check!(move_guard_ifelse_in_block);
711 move_arm_cond_to_match_guard,
730 x if x > 10 => false,
743 fn move_arm_cond_to_match_guard_with_else_last_arm_works() {
745 move_arm_cond_to_match_guard,
765 x if x > 10 => false,
777 fn move_arm_cond_to_match_guard_with_else_comma_works() {
779 move_arm_cond_to_match_guard,
797 x if x > 10 => false,
809 fn move_arm_cond_to_match_guard_elseif() {
811 move_arm_cond_to_match_guard,
832 x if x > 10 => false,
843 fn move_arm_cond_to_match_guard_elseif_in_block() {
844 cov_mark::check!(move_guard_ifelse_in_block);
846 move_arm_cond_to_match_guard,
869 x if x > 10 => false,
880 fn move_arm_cond_to_match_guard_elseif_chain() {
881 cov_mark::check!(move_guard_ifelse_else_tail);
883 move_arm_cond_to_match_guard,
919 fn move_arm_cond_to_match_guard_elseif_iflet() {
921 move_arm_cond_to_match_guard,
930 } else if let 4 = 4 {
955 fn move_arm_cond_to_match_guard_elseif_notail() {
956 cov_mark::check!(move_guard_ifelse_notail);
958 move_arm_cond_to_match_guard,