2020-08-12 11:26:51 -05:00
|
|
|
use syntax::{
|
2020-10-24 01:08:21 -05:00
|
|
|
ast::{edit::AstNodeEdit, make, AstNode, BlockExpr, Expr, IfExpr, MatchArm},
|
2020-05-20 16:14:31 -05:00
|
|
|
SyntaxKind::WHITESPACE,
|
2019-05-15 22:54:32 -05:00
|
|
|
};
|
|
|
|
|
2020-06-28 17:36:05 -05:00
|
|
|
use crate::{AssistContext, AssistId, AssistKind, Assists};
|
2019-05-15 22:54:32 -05:00
|
|
|
|
2019-10-27 03:26:46 -05:00
|
|
|
// Assist: move_guard_to_arm_body
|
|
|
|
//
|
|
|
|
// Moves match guard into match arm body.
|
|
|
|
//
|
|
|
|
// ```
|
|
|
|
// enum Action { Move { distance: u32 }, Stop }
|
|
|
|
//
|
|
|
|
// fn handle(action: Action) {
|
|
|
|
// match action {
|
2021-01-06 14:15:48 -06:00
|
|
|
// Action::Move { distance } $0if distance > 10 => foo(),
|
2019-10-27 03:26:46 -05:00
|
|
|
// _ => (),
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// ```
|
|
|
|
// ->
|
|
|
|
// ```
|
|
|
|
// enum Action { Move { distance: u32 }, Stop }
|
|
|
|
//
|
|
|
|
// fn handle(action: Action) {
|
|
|
|
// match action {
|
2020-08-13 03:32:03 -05:00
|
|
|
// Action::Move { distance } => if distance > 10 {
|
|
|
|
// foo()
|
|
|
|
// },
|
2019-10-27 03:26:46 -05:00
|
|
|
// _ => (),
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// ```
|
2020-05-06 11:45:35 -05:00
|
|
|
pub(crate) fn move_guard_to_arm_body(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
|
2019-10-27 03:48:40 -05:00
|
|
|
let match_arm = ctx.find_node_at_offset::<MatchArm>()?;
|
2019-05-15 22:54:32 -05:00
|
|
|
let guard = match_arm.guard()?;
|
|
|
|
let space_before_guard = guard.syntax().prev_sibling_or_token();
|
|
|
|
|
2021-08-12 17:21:42 -05:00
|
|
|
// FIXME: support `if let` guards too
|
|
|
|
if guard.let_token().is_some() {
|
|
|
|
return None;
|
|
|
|
}
|
2020-08-13 03:32:03 -05:00
|
|
|
let guard_condition = guard.expr()?;
|
2019-05-15 22:54:32 -05:00
|
|
|
let arm_expr = match_arm.expr()?;
|
2020-08-13 03:32:03 -05:00
|
|
|
let if_expr = make::expr_if(
|
|
|
|
make::condition(guard_condition, None),
|
|
|
|
make::block_expr(None, Some(arm_expr.clone())),
|
2020-12-05 08:41:36 -06:00
|
|
|
None,
|
2020-08-13 03:32:03 -05:00
|
|
|
)
|
|
|
|
.indent(arm_expr.indent_level());
|
2019-05-15 22:54:32 -05:00
|
|
|
|
2020-05-06 05:51:28 -05:00
|
|
|
let target = guard.syntax().text_range();
|
2020-06-28 17:36:05 -05:00
|
|
|
acc.add(
|
2020-07-02 16:48:35 -05:00
|
|
|
AssistId("move_guard_to_arm_body", AssistKind::RefactorRewrite),
|
2020-06-28 17:36:05 -05:00
|
|
|
"Move guard to arm body",
|
|
|
|
target,
|
|
|
|
|edit| {
|
|
|
|
match space_before_guard {
|
|
|
|
Some(element) if element.kind() == WHITESPACE => {
|
|
|
|
edit.delete(element.text_range());
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
};
|
2019-05-15 22:54:32 -05:00
|
|
|
|
2020-06-28 17:36:05 -05:00
|
|
|
edit.delete(guard.syntax().text_range());
|
2020-08-13 03:32:03 -05:00
|
|
|
edit.replace_ast(arm_expr, if_expr);
|
2020-06-28 17:36:05 -05:00
|
|
|
},
|
|
|
|
)
|
2019-05-15 22:54:32 -05:00
|
|
|
}
|
|
|
|
|
2019-10-27 03:26:46 -05:00
|
|
|
// Assist: move_arm_cond_to_match_guard
|
|
|
|
//
|
|
|
|
// Moves if expression from match arm body into a guard.
|
|
|
|
//
|
|
|
|
// ```
|
|
|
|
// enum Action { Move { distance: u32 }, Stop }
|
|
|
|
//
|
|
|
|
// fn handle(action: Action) {
|
|
|
|
// match action {
|
2021-01-06 14:15:48 -06:00
|
|
|
// Action::Move { distance } => $0if distance > 10 { foo() },
|
2019-10-27 03:26:46 -05:00
|
|
|
// _ => (),
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// ```
|
|
|
|
// ->
|
|
|
|
// ```
|
|
|
|
// enum Action { Move { distance: u32 }, Stop }
|
|
|
|
//
|
|
|
|
// fn handle(action: Action) {
|
|
|
|
// match action {
|
|
|
|
// Action::Move { distance } if distance > 10 => foo(),
|
|
|
|
// _ => (),
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// ```
|
2020-05-06 11:45:35 -05:00
|
|
|
pub(crate) fn move_arm_cond_to_match_guard(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
|
2019-10-27 03:48:40 -05:00
|
|
|
let match_arm: MatchArm = ctx.find_node_at_offset::<MatchArm>()?;
|
2020-02-09 12:57:01 -06:00
|
|
|
let match_pat = match_arm.pat()?;
|
2019-05-15 22:54:32 -05:00
|
|
|
let arm_body = match_arm.expr()?;
|
2020-10-24 01:08:21 -05:00
|
|
|
|
|
|
|
let mut replace_node = None;
|
|
|
|
let if_expr: IfExpr = IfExpr::cast(arm_body.syntax().clone()).or_else(|| {
|
|
|
|
let block_expr = BlockExpr::cast(arm_body.syntax().clone())?;
|
2021-01-05 06:45:46 -06:00
|
|
|
if let Expr::IfExpr(e) = block_expr.tail_expr()? {
|
2020-10-24 01:08:21 -05:00
|
|
|
replace_node = Some(block_expr.syntax().clone());
|
|
|
|
Some(e)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})?;
|
|
|
|
let replace_node = replace_node.unwrap_or_else(|| if_expr.syntax().clone());
|
|
|
|
|
2019-05-15 22:54:32 -05:00
|
|
|
let cond = if_expr.condition()?;
|
|
|
|
let then_block = if_expr.then_branch()?;
|
|
|
|
|
|
|
|
// Not support if with else branch
|
2020-02-18 07:32:19 -06:00
|
|
|
if if_expr.else_branch().is_some() {
|
2019-05-15 22:54:32 -05:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
// Not support moving if let to arm guard
|
2021-08-07 15:16:15 -05:00
|
|
|
if cond.is_pattern_cond() {
|
2019-05-15 22:54:32 -05:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
let buf = format!(" if {}", cond.syntax().text());
|
|
|
|
|
2020-05-06 11:45:35 -05:00
|
|
|
acc.add(
|
2020-07-02 16:48:35 -05:00
|
|
|
AssistId("move_arm_cond_to_match_guard", AssistKind::RefactorRewrite),
|
2020-01-14 11:32:26 -06:00
|
|
|
"Move condition to match guard",
|
2020-10-24 01:08:21 -05:00
|
|
|
replace_node.text_range(),
|
2019-05-15 22:54:32 -05:00
|
|
|
|edit| {
|
2020-05-01 18:18:19 -05:00
|
|
|
let then_only_expr = then_block.statements().next().is_none();
|
2019-05-15 22:54:32 -05:00
|
|
|
|
2021-01-05 06:45:46 -06:00
|
|
|
match &then_block.tail_expr() {
|
2019-05-15 22:54:32 -05:00
|
|
|
Some(then_expr) if then_only_expr => {
|
2020-10-24 01:08:21 -05:00
|
|
|
edit.replace(replace_node.text_range(), then_expr.syntax().text())
|
2019-05-15 22:54:32 -05:00
|
|
|
}
|
2020-10-24 01:08:21 -05:00
|
|
|
_ if replace_node != *if_expr.syntax() => {
|
|
|
|
// Dedent because if_expr is in a BlockExpr
|
|
|
|
let replace_with = then_block.dedent(1.into()).syntax().text();
|
|
|
|
edit.replace(replace_node.text_range(), replace_with)
|
|
|
|
}
|
|
|
|
_ => edit.replace(replace_node.text_range(), then_block.syntax().text()),
|
2019-05-15 22:54:32 -05:00
|
|
|
}
|
|
|
|
|
2020-02-09 12:57:01 -06:00
|
|
|
edit.insert(match_pat.syntax().text_range().end(), buf);
|
2019-05-15 22:54:32 -05:00
|
|
|
},
|
2019-10-27 09:35:37 -05:00
|
|
|
)
|
2019-05-15 22:54:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
2020-05-06 03:16:55 -05:00
|
|
|
use crate::tests::{check_assist, check_assist_not_applicable, check_assist_target};
|
2019-05-15 22:54:32 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn move_guard_to_arm_body_target() {
|
|
|
|
check_assist_target(
|
|
|
|
move_guard_to_arm_body,
|
|
|
|
r#"
|
2020-08-13 03:32:03 -05:00
|
|
|
fn main() {
|
|
|
|
match 92 {
|
2021-01-06 14:15:48 -06:00
|
|
|
x $0if x > 10 => false,
|
2020-08-13 03:32:03 -05:00
|
|
|
_ => true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"if x > 10"#,
|
2019-05-15 22:54:32 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn move_guard_to_arm_body_works() {
|
|
|
|
check_assist(
|
|
|
|
move_guard_to_arm_body,
|
|
|
|
r#"
|
2020-08-13 03:32:03 -05:00
|
|
|
fn main() {
|
|
|
|
match 92 {
|
2021-01-06 14:15:48 -06:00
|
|
|
x $0if x > 10 => false,
|
2020-08-13 03:32:03 -05:00
|
|
|
_ => true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
2019-05-15 22:54:32 -05:00
|
|
|
r#"
|
2020-08-13 03:32:03 -05:00
|
|
|
fn main() {
|
|
|
|
match 92 {
|
|
|
|
x => if x > 10 {
|
|
|
|
false
|
|
|
|
},
|
|
|
|
_ => true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
2019-05-15 22:54:32 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn move_guard_to_arm_body_works_complex_match() {
|
|
|
|
check_assist(
|
|
|
|
move_guard_to_arm_body,
|
|
|
|
r#"
|
2020-08-13 03:32:03 -05:00
|
|
|
fn main() {
|
|
|
|
match 92 {
|
2021-01-06 14:15:48 -06:00
|
|
|
$0x @ 4 | x @ 5 if x > 5 => true,
|
2020-08-13 03:32:03 -05:00
|
|
|
_ => false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
2019-05-15 22:54:32 -05:00
|
|
|
r#"
|
2020-08-13 03:32:03 -05:00
|
|
|
fn main() {
|
|
|
|
match 92 {
|
|
|
|
x @ 4 | x @ 5 => if x > 5 {
|
|
|
|
true
|
|
|
|
},
|
|
|
|
_ => false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
2019-05-15 22:54:32 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn move_arm_cond_to_match_guard_works() {
|
|
|
|
check_assist(
|
|
|
|
move_arm_cond_to_match_guard,
|
|
|
|
r#"
|
2020-08-13 03:32:03 -05:00
|
|
|
fn main() {
|
|
|
|
match 92 {
|
2021-01-06 14:15:48 -06:00
|
|
|
x => if x > 10 { $0false },
|
2020-08-13 03:32:03 -05:00
|
|
|
_ => true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
2019-05-15 22:54:32 -05:00
|
|
|
r#"
|
2020-08-13 03:32:03 -05:00
|
|
|
fn main() {
|
|
|
|
match 92 {
|
|
|
|
x if x > 10 => false,
|
|
|
|
_ => true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
2019-05-15 22:54:32 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-10-24 01:08:21 -05:00
|
|
|
#[test]
|
|
|
|
fn move_arm_cond_in_block_to_match_guard_works() {
|
|
|
|
check_assist(
|
|
|
|
move_arm_cond_to_match_guard,
|
|
|
|
r#"
|
|
|
|
fn main() {
|
|
|
|
match 92 {
|
|
|
|
x => {
|
2021-01-06 14:15:48 -06:00
|
|
|
$0if x > 10 {
|
2020-10-24 01:08:21 -05:00
|
|
|
false
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
fn main() {
|
|
|
|
match 92 {
|
|
|
|
x if x > 10 => false,
|
|
|
|
_ => true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-05-15 22:54:32 -05:00
|
|
|
#[test]
|
|
|
|
fn move_arm_cond_to_match_guard_if_let_not_works() {
|
|
|
|
check_assist_not_applicable(
|
|
|
|
move_arm_cond_to_match_guard,
|
|
|
|
r#"
|
2020-08-13 03:32:03 -05:00
|
|
|
fn main() {
|
|
|
|
match 92 {
|
2021-01-06 14:15:48 -06:00
|
|
|
x => if let 62 = x { $0false },
|
2020-08-13 03:32:03 -05:00
|
|
|
_ => true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
2019-05-15 22:54:32 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn move_arm_cond_to_match_guard_if_empty_body_works() {
|
|
|
|
check_assist(
|
|
|
|
move_arm_cond_to_match_guard,
|
|
|
|
r#"
|
2020-08-13 03:32:03 -05:00
|
|
|
fn main() {
|
|
|
|
match 92 {
|
2021-01-06 14:15:48 -06:00
|
|
|
x => if x > 10 { $0 },
|
2020-08-13 03:32:03 -05:00
|
|
|
_ => true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
2019-05-15 22:54:32 -05:00
|
|
|
r#"
|
2020-08-13 03:32:03 -05:00
|
|
|
fn main() {
|
|
|
|
match 92 {
|
|
|
|
x if x > 10 => { },
|
|
|
|
_ => true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
2019-05-15 22:54:32 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn move_arm_cond_to_match_guard_if_multiline_body_works() {
|
|
|
|
check_assist(
|
|
|
|
move_arm_cond_to_match_guard,
|
|
|
|
r#"
|
2020-08-13 03:32:03 -05:00
|
|
|
fn main() {
|
|
|
|
match 92 {
|
|
|
|
x => if x > 10 {
|
2021-01-06 14:15:48 -06:00
|
|
|
92;$0
|
2020-08-13 03:32:03 -05:00
|
|
|
false
|
|
|
|
},
|
|
|
|
_ => true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
2019-05-15 22:54:32 -05:00
|
|
|
r#"
|
2020-08-13 03:32:03 -05:00
|
|
|
fn main() {
|
|
|
|
match 92 {
|
|
|
|
x if x > 10 => {
|
|
|
|
92;
|
|
|
|
false
|
|
|
|
},
|
|
|
|
_ => true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
2019-05-15 22:54:32 -05:00
|
|
|
);
|
|
|
|
}
|
2020-10-24 01:08:21 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn move_arm_cond_in_block_to_match_guard_if_multiline_body_works() {
|
|
|
|
check_assist(
|
|
|
|
move_arm_cond_to_match_guard,
|
|
|
|
r#"
|
|
|
|
fn main() {
|
|
|
|
match 92 {
|
|
|
|
x => {
|
|
|
|
if x > 10 {
|
2021-01-06 14:15:48 -06:00
|
|
|
92;$0
|
2020-10-24 01:08:21 -05:00
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
fn main() {
|
|
|
|
match 92 {
|
|
|
|
x if x > 10 => {
|
|
|
|
92;
|
|
|
|
false
|
|
|
|
}
|
|
|
|
_ => true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
}
|
2019-05-15 22:54:32 -05:00
|
|
|
}
|