2 ast::{edit::AstNodeEdit, make, AstNode, BlockExpr, Expr, IfExpr, MatchArm},
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 // FIXME: support `if let` guards too
45 if guard.let_token().is_some() {
48 let guard_condition = guard.expr()?;
49 let arm_expr = match_arm.expr()?;
50 let if_expr = make::expr_if(
51 make::condition(guard_condition, None),
52 make::block_expr(None, Some(arm_expr.clone())),
55 .indent(arm_expr.indent_level());
57 let target = guard.syntax().text_range();
59 AssistId("move_guard_to_arm_body", AssistKind::RefactorRewrite),
60 "Move guard to arm body",
63 match space_before_guard {
64 Some(element) if element.kind() == WHITESPACE => {
65 edit.delete(element.text_range());
70 edit.delete(guard.syntax().text_range());
71 edit.replace_ast(arm_expr, if_expr);
76 // Assist: move_arm_cond_to_match_guard
78 // Moves if expression from match arm body into a guard.
81 // enum Action { Move { distance: u32 }, Stop }
83 // fn handle(action: Action) {
85 // Action::Move { distance } => $0if distance > 10 { foo() },
92 // enum Action { Move { distance: u32 }, Stop }
94 // fn handle(action: Action) {
96 // Action::Move { distance } if distance > 10 => foo(),
101 pub(crate) fn move_arm_cond_to_match_guard(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
102 let match_arm: MatchArm = ctx.find_node_at_offset::<MatchArm>()?;
103 let match_pat = match_arm.pat()?;
104 let arm_body = match_arm.expr()?;
106 let mut replace_node = None;
107 let if_expr: IfExpr = IfExpr::cast(arm_body.syntax().clone()).or_else(|| {
108 let block_expr = BlockExpr::cast(arm_body.syntax().clone())?;
109 if let Expr::IfExpr(e) = block_expr.tail_expr()? {
110 replace_node = Some(block_expr.syntax().clone());
116 let replace_node = replace_node.unwrap_or_else(|| if_expr.syntax().clone());
118 let cond = if_expr.condition()?;
119 let then_block = if_expr.then_branch()?;
121 // Not support if with else branch
122 if if_expr.else_branch().is_some() {
125 // Not support moving if let to arm guard
126 if cond.is_pattern_cond() {
130 let buf = format!(" if {}", cond.syntax().text());
133 AssistId("move_arm_cond_to_match_guard", AssistKind::RefactorRewrite),
134 "Move condition to match guard",
135 replace_node.text_range(),
137 let then_only_expr = then_block.statements().next().is_none();
139 match &then_block.tail_expr() {
140 Some(then_expr) if then_only_expr => {
141 edit.replace(replace_node.text_range(), then_expr.syntax().text())
143 _ if replace_node != *if_expr.syntax() => {
144 // Dedent because if_expr is in a BlockExpr
145 let replace_with = then_block.dedent(1.into()).syntax().text();
146 edit.replace(replace_node.text_range(), replace_with)
148 _ => edit.replace(replace_node.text_range(), then_block.syntax().text()),
151 edit.insert(match_pat.syntax().text_range().end(), buf);
160 use crate::tests::{check_assist, check_assist_not_applicable, check_assist_target};
163 fn move_guard_to_arm_body_range() {
164 cov_mark::check!(move_guard_unapplicable_in_arm_body);
165 check_assist_not_applicable(
166 move_guard_to_arm_body,
170 x if x > 10 => $0false,
178 fn move_guard_to_arm_body_target() {
180 move_guard_to_arm_body,
184 x $0if x > 10 => false,
194 fn move_guard_to_arm_body_works() {
196 move_guard_to_arm_body,
200 x $0if x > 10 => false,
219 fn move_guard_to_arm_body_works_complex_match() {
221 move_guard_to_arm_body,
225 $0x @ 4 | x @ 5 if x > 5 => true,
233 x @ 4 | x @ 5 => if x > 5 {
244 fn move_arm_cond_to_match_guard_works() {
246 move_arm_cond_to_match_guard,
250 x => if x > 10 { $0false },
258 x if x > 10 => false,
267 fn move_arm_cond_in_block_to_match_guard_works() {
269 move_arm_cond_to_match_guard,
285 x if x > 10 => false,
294 fn move_arm_cond_to_match_guard_if_let_not_works() {
295 check_assist_not_applicable(
296 move_arm_cond_to_match_guard,
300 x => if let 62 = x { $0false },
309 fn move_arm_cond_to_match_guard_if_empty_body_works() {
311 move_arm_cond_to_match_guard,
315 x => if x > 10 { $0 },
332 fn move_arm_cond_to_match_guard_if_multiline_body_works() {
334 move_arm_cond_to_match_guard,
361 fn move_arm_cond_in_block_to_match_guard_if_multiline_body_works() {
363 move_arm_cond_to_match_guard,