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_guard_to_arm_body_works_complex_match() {
272 move_guard_to_arm_body,
276 $0x @ 4 | x @ 5 if x > 5 => true,
284 x @ 4 | x @ 5 => if x > 5 {
295 fn move_arm_cond_to_match_guard_works() {
297 move_arm_cond_to_match_guard,
301 x => if x > 10 { $0false },
309 x if x > 10 => false,
318 fn move_arm_cond_in_block_to_match_guard_works() {
319 cov_mark::check!(move_guard_ifelse_has_wildcard);
321 move_arm_cond_to_match_guard,
337 x if x > 10 => false,
346 fn move_arm_cond_in_block_to_match_guard_no_wildcard_works() {
347 cov_mark::check_count!(move_guard_ifelse_has_wildcard, 0);
349 move_arm_cond_to_match_guard,
364 x if x > 10 => false,
373 fn move_arm_cond_in_block_to_match_guard_wildcard_guard_works() {
374 cov_mark::check_count!(move_guard_ifelse_has_wildcard, 0);
376 move_arm_cond_to_match_guard,
392 x if x > 10 => false,
402 fn move_arm_cond_in_block_to_match_guard_add_comma_works() {
404 move_arm_cond_to_match_guard,
420 x if x > 10 => false,
429 fn move_arm_cond_to_match_guard_if_let_not_works() {
430 check_assist_not_applicable(
431 move_arm_cond_to_match_guard,
435 x => if let 62 = x { $0false },
444 fn move_arm_cond_to_match_guard_if_empty_body_works() {
446 move_arm_cond_to_match_guard,
450 x => if x > 10 { $0 },
467 fn move_arm_cond_to_match_guard_if_multiline_body_works() {
469 move_arm_cond_to_match_guard,
496 fn move_arm_cond_in_block_to_match_guard_if_multiline_body_works() {
498 move_arm_cond_to_match_guard,
527 fn move_arm_cond_to_match_guard_with_else_works() {
529 move_arm_cond_to_match_guard,
545 x if x > 10 => false,
555 fn move_arm_cond_to_match_guard_with_else_block_works() {
556 cov_mark::check!(move_guard_ifelse_expr_only);
558 move_arm_cond_to_match_guard,
576 x if x > 10 => false,
586 fn move_arm_cond_to_match_guard_else_if_empty_body_works() {
588 move_arm_cond_to_match_guard,
592 x => if x > 10 { $0 } else { },
610 fn move_arm_cond_to_match_guard_with_else_multiline_works() {
612 move_arm_cond_to_match_guard,
642 fn move_arm_cond_to_match_guard_with_else_multiline_else_works() {
643 cov_mark::check!(move_guard_ifelse_else_block);
645 move_arm_cond_to_match_guard,
662 x if x > 10 => false,
675 fn move_arm_cond_to_match_guard_with_else_multiline_else_block_works() {
676 cov_mark::check!(move_guard_ifelse_in_block);
678 move_arm_cond_to_match_guard,
697 x if x > 10 => false,
710 fn move_arm_cond_to_match_guard_with_else_last_arm_works() {
712 move_arm_cond_to_match_guard,
732 x if x > 10 => false,
744 fn move_arm_cond_to_match_guard_with_else_comma_works() {
746 move_arm_cond_to_match_guard,
764 x if x > 10 => false,
776 fn move_arm_cond_to_match_guard_elseif() {
778 move_arm_cond_to_match_guard,
799 x if x > 10 => false,
810 fn move_arm_cond_to_match_guard_elseif_in_block() {
811 cov_mark::check!(move_guard_ifelse_in_block);
813 move_arm_cond_to_match_guard,
836 x if x > 10 => false,
847 fn move_arm_cond_to_match_guard_elseif_chain() {
848 cov_mark::check!(move_guard_ifelse_else_tail);
850 move_arm_cond_to_match_guard,
886 fn move_arm_cond_to_match_guard_elseif_iflet() {
887 check_assist_not_applicable(
888 move_arm_cond_to_match_guard,
897 } else if let 4 = 4 {
910 fn move_arm_cond_to_match_guard_elseif_notail() {
911 cov_mark::check!(move_guard_ifelse_notail);
913 move_arm_cond_to_match_guard,