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 // FIXME: support `if let` guards too
41 if guard.let_token().is_some() {
44 let guard_condition = guard.expr()?;
45 let arm_expr = match_arm.expr()?;
46 let if_expr = make::expr_if(
47 make::condition(guard_condition, None),
48 make::block_expr(None, Some(arm_expr.clone())),
51 .indent(arm_expr.indent_level());
53 let target = guard.syntax().text_range();
55 AssistId("move_guard_to_arm_body", AssistKind::RefactorRewrite),
56 "Move guard to arm body",
59 match space_before_guard {
60 Some(element) if element.kind() == WHITESPACE => {
61 edit.delete(element.text_range());
66 edit.delete(guard.syntax().text_range());
67 edit.replace_ast(arm_expr, if_expr);
72 // Assist: move_arm_cond_to_match_guard
74 // Moves if expression from match arm body into a guard.
77 // enum Action { Move { distance: u32 }, Stop }
79 // fn handle(action: Action) {
81 // Action::Move { distance } => $0if distance > 10 { foo() },
88 // enum Action { Move { distance: u32 }, Stop }
90 // fn handle(action: Action) {
92 // Action::Move { distance } if distance > 10 => foo(),
97 pub(crate) fn move_arm_cond_to_match_guard(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
98 let match_arm: MatchArm = ctx.find_node_at_offset::<MatchArm>()?;
99 let match_pat = match_arm.pat()?;
100 let arm_body = match_arm.expr()?;
102 let mut replace_node = None;
103 let if_expr: IfExpr = IfExpr::cast(arm_body.syntax().clone()).or_else(|| {
104 let block_expr = BlockExpr::cast(arm_body.syntax().clone())?;
105 if let Expr::IfExpr(e) = block_expr.tail_expr()? {
106 replace_node = Some(block_expr.syntax().clone());
112 let replace_node = replace_node.unwrap_or_else(|| if_expr.syntax().clone());
114 let cond = if_expr.condition()?;
115 let then_block = if_expr.then_branch()?;
117 // Not support if with else branch
118 if if_expr.else_branch().is_some() {
121 // Not support moving if let to arm guard
122 if cond.is_pattern_cond() {
126 let buf = format!(" if {}", cond.syntax().text());
129 AssistId("move_arm_cond_to_match_guard", AssistKind::RefactorRewrite),
130 "Move condition to match guard",
131 replace_node.text_range(),
133 let then_only_expr = then_block.statements().next().is_none();
135 match &then_block.tail_expr() {
136 Some(then_expr) if then_only_expr => {
137 edit.replace(replace_node.text_range(), then_expr.syntax().text())
139 _ if replace_node != *if_expr.syntax() => {
140 // Dedent because if_expr is in a BlockExpr
141 let replace_with = then_block.dedent(1.into()).syntax().text();
142 edit.replace(replace_node.text_range(), replace_with)
144 _ => edit.replace(replace_node.text_range(), then_block.syntax().text()),
147 edit.insert(match_pat.syntax().text_range().end(), buf);
156 use crate::tests::{check_assist, check_assist_not_applicable, check_assist_target};
159 fn move_guard_to_arm_body_target() {
161 move_guard_to_arm_body,
165 x $0if x > 10 => false,
175 fn move_guard_to_arm_body_works() {
177 move_guard_to_arm_body,
181 x $0if x > 10 => false,
200 fn move_guard_to_arm_body_works_complex_match() {
202 move_guard_to_arm_body,
206 $0x @ 4 | x @ 5 if x > 5 => true,
214 x @ 4 | x @ 5 => if x > 5 {
225 fn move_arm_cond_to_match_guard_works() {
227 move_arm_cond_to_match_guard,
231 x => if x > 10 { $0false },
239 x if x > 10 => false,
248 fn move_arm_cond_in_block_to_match_guard_works() {
250 move_arm_cond_to_match_guard,
266 x if x > 10 => false,
275 fn move_arm_cond_to_match_guard_if_let_not_works() {
276 check_assist_not_applicable(
277 move_arm_cond_to_match_guard,
281 x => if let 62 = x { $0false },
290 fn move_arm_cond_to_match_guard_if_empty_body_works() {
292 move_arm_cond_to_match_guard,
296 x => if x > 10 { $0 },
313 fn move_arm_cond_to_match_guard_if_multiline_body_works() {
315 move_arm_cond_to_match_guard,
342 fn move_arm_cond_in_block_to_match_guard_if_multiline_body_works() {
344 move_arm_cond_to_match_guard,