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 let space_before_guard = guard.syntax().prev_sibling_or_token();
40 let guard_condition = guard.expr()?;
41 let arm_expr = match_arm.expr()?;
42 let if_expr = make::expr_if(
43 make::condition(guard_condition, None),
44 make::block_expr(None, Some(arm_expr.clone())),
47 .indent(arm_expr.indent_level());
49 let target = guard.syntax().text_range();
51 AssistId("move_guard_to_arm_body", AssistKind::RefactorRewrite),
52 "Move guard to arm body",
55 match space_before_guard {
56 Some(element) if element.kind() == WHITESPACE => {
57 edit.delete(element.text_range());
62 edit.delete(guard.syntax().text_range());
63 edit.replace_ast(arm_expr, if_expr);
68 // Assist: move_arm_cond_to_match_guard
70 // Moves if expression from match arm body into a guard.
73 // enum Action { Move { distance: u32 }, Stop }
75 // fn handle(action: Action) {
77 // Action::Move { distance } => $0if distance > 10 { foo() },
84 // enum Action { Move { distance: u32 }, Stop }
86 // fn handle(action: Action) {
88 // Action::Move { distance } if distance > 10 => foo(),
93 pub(crate) fn move_arm_cond_to_match_guard(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
94 let match_arm: MatchArm = ctx.find_node_at_offset::<MatchArm>()?;
95 let match_pat = match_arm.pat()?;
96 let arm_body = match_arm.expr()?;
98 let mut replace_node = None;
99 let if_expr: IfExpr = IfExpr::cast(arm_body.syntax().clone()).or_else(|| {
100 let block_expr = BlockExpr::cast(arm_body.syntax().clone())?;
101 if let Expr::IfExpr(e) = block_expr.tail_expr()? {
102 replace_node = Some(block_expr.syntax().clone());
108 let replace_node = replace_node.unwrap_or_else(|| if_expr.syntax().clone());
110 let cond = if_expr.condition()?;
111 let then_block = if_expr.then_branch()?;
113 // Not support if with else branch
114 if if_expr.else_branch().is_some() {
117 // Not support moving if let to arm guard
118 if cond.is_pattern_cond() {
122 let buf = format!(" if {}", cond.syntax().text());
125 AssistId("move_arm_cond_to_match_guard", AssistKind::RefactorRewrite),
126 "Move condition to match guard",
127 replace_node.text_range(),
129 let then_only_expr = then_block.statements().next().is_none();
131 match &then_block.tail_expr() {
132 Some(then_expr) if then_only_expr => {
133 edit.replace(replace_node.text_range(), then_expr.syntax().text())
135 _ if replace_node != *if_expr.syntax() => {
136 // Dedent because if_expr is in a BlockExpr
137 let replace_with = then_block.dedent(1.into()).syntax().text();
138 edit.replace(replace_node.text_range(), replace_with)
140 _ => edit.replace(replace_node.text_range(), then_block.syntax().text()),
143 edit.insert(match_pat.syntax().text_range().end(), buf);
152 use crate::tests::{check_assist, check_assist_not_applicable, check_assist_target};
155 fn move_guard_to_arm_body_target() {
157 move_guard_to_arm_body,
161 x $0if x > 10 => false,
171 fn move_guard_to_arm_body_works() {
173 move_guard_to_arm_body,
177 x $0if x > 10 => false,
196 fn move_guard_to_arm_body_works_complex_match() {
198 move_guard_to_arm_body,
202 $0x @ 4 | x @ 5 if x > 5 => true,
210 x @ 4 | x @ 5 => if x > 5 {
221 fn move_arm_cond_to_match_guard_works() {
223 move_arm_cond_to_match_guard,
227 x => if x > 10 { $0false },
235 x if x > 10 => false,
244 fn move_arm_cond_in_block_to_match_guard_works() {
246 move_arm_cond_to_match_guard,
262 x if x > 10 => false,
271 fn move_arm_cond_to_match_guard_if_let_not_works() {
272 check_assist_not_applicable(
273 move_arm_cond_to_match_guard,
277 x => if let 62 = x { $0false },
286 fn move_arm_cond_to_match_guard_if_empty_body_works() {
288 move_arm_cond_to_match_guard,
292 x => if x > 10 { $0 },
309 fn move_arm_cond_to_match_guard_if_multiline_body_works() {
311 move_arm_cond_to_match_guard,
338 fn move_arm_cond_in_block_to_match_guard_if_multiline_body_works() {
340 move_arm_cond_to_match_guard,