2 ast::{AstNode, IfExpr, MatchArm},
3 SyntaxKind::WHITESPACE,
6 use crate::{AssistContext, AssistId, 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 } <|>if distance > 10 => foo(),
24 // enum Action { Move { distance: u32 }, Stop }
26 // fn handle(action: Action) {
28 // Action::Move { distance } => if distance > 10 { foo() },
33 pub(crate) fn move_guard_to_arm_body(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
34 let match_arm = ctx.find_node_at_offset::<MatchArm>()?;
35 let guard = match_arm.guard()?;
36 let space_before_guard = guard.syntax().prev_sibling_or_token();
38 let guard_conditions = guard.expr()?;
39 let arm_expr = match_arm.expr()?;
40 let buf = format!("if {} {{ {} }}", guard_conditions.syntax().text(), arm_expr.syntax().text());
42 let target = guard.syntax().text_range();
43 acc.add(AssistId("move_guard_to_arm_body"), "Move guard to arm body", target, |edit| {
44 match space_before_guard {
45 Some(element) if element.kind() == WHITESPACE => {
46 edit.delete(element.text_range());
51 edit.delete(guard.syntax().text_range());
52 edit.replace_node_and_indent(arm_expr.syntax(), buf);
56 // Assist: move_arm_cond_to_match_guard
58 // Moves if expression from match arm body into a guard.
61 // enum Action { Move { distance: u32 }, Stop }
63 // fn handle(action: Action) {
65 // Action::Move { distance } => <|>if distance > 10 { foo() },
72 // enum Action { Move { distance: u32 }, Stop }
74 // fn handle(action: Action) {
76 // Action::Move { distance } if distance > 10 => foo(),
81 pub(crate) fn move_arm_cond_to_match_guard(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
82 let match_arm: MatchArm = ctx.find_node_at_offset::<MatchArm>()?;
83 let match_pat = match_arm.pat()?;
85 let arm_body = match_arm.expr()?;
86 let if_expr: IfExpr = IfExpr::cast(arm_body.syntax().clone())?;
87 let cond = if_expr.condition()?;
88 let then_block = if_expr.then_branch()?;
90 // Not support if with else branch
91 if if_expr.else_branch().is_some() {
94 // Not support moving if let to arm guard
95 if cond.pat().is_some() {
99 let buf = format!(" if {}", cond.syntax().text());
101 let target = if_expr.syntax().text_range();
103 AssistId("move_arm_cond_to_match_guard"),
104 "Move condition to match guard",
107 let then_only_expr = then_block.statements().next().is_none();
109 match &then_block.expr() {
110 Some(then_expr) if then_only_expr => {
111 edit.replace(if_expr.syntax().text_range(), then_expr.syntax().text())
113 _ => edit.replace(if_expr.syntax().text_range(), then_block.syntax().text()),
116 edit.insert(match_pat.syntax().text_range().end(), buf);
125 use crate::tests::{check_assist, check_assist_not_applicable, check_assist_target};
128 fn move_guard_to_arm_body_target() {
130 move_guard_to_arm_body,
136 '\r' <|>if chars.clone().next() == Some('\n') => false,
141 r#"if chars.clone().next() == Some('\n')"#,
146 fn move_guard_to_arm_body_works() {
148 move_guard_to_arm_body,
154 '\r' <|>if chars.clone().next() == Some('\n') => false,
164 '\r' => if chars.clone().next() == Some('\n') { false },
173 fn move_guard_to_arm_body_works_complex_match() {
175 move_guard_to_arm_body,
179 <|>y @ 4 | y @ 5 if y > 5 => true,
187 y @ 4 | y @ 5 => if y > 5 { true },
196 fn move_arm_cond_to_match_guard_works() {
198 move_arm_cond_to_match_guard,
204 '\r' => if chars.clone().next() == Some('\n') { <|>false },
214 '\r' if chars.clone().next() == Some('\n') => false,
223 fn move_arm_cond_to_match_guard_if_let_not_works() {
224 check_assist_not_applicable(
225 move_arm_cond_to_match_guard,
231 '\r' => if let Some(_) = chars.clone().next() { <|>false },
240 fn move_arm_cond_to_match_guard_if_empty_body_works() {
242 move_arm_cond_to_match_guard,
248 '\r' => if chars.clone().next().is_some() { <|> },
258 '\r' if chars.clone().next().is_some() => { },
267 fn move_arm_cond_to_match_guard_if_multiline_body_works() {
269 move_arm_cond_to_match_guard,
275 '\r' => if chars.clone().next().is_some() {
288 '\r' if chars.clone().next().is_some() => {