2019-05-15 22:54:32 -05:00
|
|
|
use hir::db::HirDatabase;
|
|
|
|
use ra_syntax::{
|
|
|
|
ast,
|
2019-07-04 15:05:17 -05:00
|
|
|
ast::{AstNode, AstToken, IfExpr, MatchArm},
|
2019-07-20 12:04:34 -05:00
|
|
|
TextUnit,
|
2019-05-15 22:54:32 -05:00
|
|
|
};
|
|
|
|
|
2019-07-04 15:05:17 -05:00
|
|
|
use crate::{Assist, AssistCtx, AssistId};
|
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 {
|
|
|
|
// Action::Move { distance } <|>if distance > 10 => foo(),
|
|
|
|
// _ => (),
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// ```
|
|
|
|
// ->
|
|
|
|
// ```
|
|
|
|
// enum Action { Move { distance: u32 }, Stop }
|
|
|
|
//
|
|
|
|
// fn handle(action: Action) {
|
|
|
|
// match action {
|
|
|
|
// Action::Move { distance } => if distance > 10 { foo() },
|
|
|
|
// _ => (),
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// ```
|
2019-05-15 22:54:32 -05:00
|
|
|
pub(crate) fn move_guard_to_arm_body(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
|
|
|
|
let match_arm = ctx.node_at_offset::<MatchArm>()?;
|
|
|
|
let guard = match_arm.guard()?;
|
|
|
|
let space_before_guard = guard.syntax().prev_sibling_or_token();
|
|
|
|
|
|
|
|
let guard_conditions = guard.expr()?;
|
|
|
|
let arm_expr = match_arm.expr()?;
|
|
|
|
let buf = format!("if {} {{ {} }}", guard_conditions.syntax().text(), arm_expr.syntax().text());
|
|
|
|
|
|
|
|
ctx.add_action(AssistId("move_guard_to_arm_body"), "move guard to arm body", |edit| {
|
2019-07-20 04:58:27 -05:00
|
|
|
edit.target(guard.syntax().text_range());
|
2019-07-20 12:04:34 -05:00
|
|
|
let offseting_amount = match space_before_guard.and_then(|it| it.into_token()) {
|
|
|
|
Some(tok) => {
|
2019-07-19 03:24:41 -05:00
|
|
|
if let Some(_) = ast::Whitespace::cast(tok.clone()) {
|
2019-07-20 12:04:34 -05:00
|
|
|
let ele = tok.text_range();
|
2019-05-15 22:54:32 -05:00
|
|
|
edit.delete(ele);
|
|
|
|
ele.len()
|
|
|
|
} else {
|
|
|
|
TextUnit::from(0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => TextUnit::from(0),
|
|
|
|
};
|
|
|
|
|
2019-07-20 04:58:27 -05:00
|
|
|
edit.delete(guard.syntax().text_range());
|
2019-05-15 22:54:32 -05:00
|
|
|
edit.replace_node_and_indent(arm_expr.syntax(), buf);
|
2019-07-20 04:58:27 -05:00
|
|
|
edit.set_cursor(
|
|
|
|
arm_expr.syntax().text_range().start() + TextUnit::from(3) - offseting_amount,
|
|
|
|
);
|
2019-05-15 22:54:32 -05:00
|
|
|
});
|
|
|
|
ctx.build()
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
// Action::Move { distance } => <|>if distance > 10 { foo() },
|
|
|
|
// _ => (),
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// ```
|
|
|
|
// ->
|
|
|
|
// ```
|
|
|
|
// enum Action { Move { distance: u32 }, Stop }
|
|
|
|
//
|
|
|
|
// fn handle(action: Action) {
|
|
|
|
// match action {
|
|
|
|
// Action::Move { distance } if distance > 10 => foo(),
|
|
|
|
// _ => (),
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// ```
|
2019-05-15 22:54:32 -05:00
|
|
|
pub(crate) fn move_arm_cond_to_match_guard(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
|
2019-07-19 03:24:41 -05:00
|
|
|
let match_arm: MatchArm = ctx.node_at_offset::<MatchArm>()?;
|
2019-05-15 22:54:32 -05:00
|
|
|
let last_match_pat = match_arm.pats().last()?;
|
|
|
|
|
|
|
|
let arm_body = match_arm.expr()?;
|
2019-07-19 03:24:41 -05:00
|
|
|
let if_expr: IfExpr = IfExpr::cast(arm_body.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
|
|
|
|
if let Some(_) = if_expr.else_branch() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
// Not support moving if let to arm guard
|
|
|
|
if let Some(_) = cond.pat() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
let buf = format!(" if {}", cond.syntax().text());
|
|
|
|
|
|
|
|
ctx.add_action(
|
|
|
|
AssistId("move_arm_cond_to_match_guard"),
|
|
|
|
"move condition to match guard",
|
|
|
|
|edit| {
|
2019-07-20 04:58:27 -05:00
|
|
|
edit.target(if_expr.syntax().text_range());
|
2019-09-02 13:23:19 -05:00
|
|
|
let then_only_expr = then_block.block().and_then(|it| it.statements().next()).is_none();
|
2019-05-15 22:54:32 -05:00
|
|
|
|
2019-09-02 13:23:19 -05:00
|
|
|
match &then_block.block().and_then(|it| it.expr()) {
|
2019-05-15 22:54:32 -05:00
|
|
|
Some(then_expr) if then_only_expr => {
|
2019-07-20 04:58:27 -05:00
|
|
|
edit.replace(if_expr.syntax().text_range(), then_expr.syntax().text())
|
2019-05-15 22:54:32 -05:00
|
|
|
}
|
2019-07-20 04:58:27 -05:00
|
|
|
_ => edit.replace(if_expr.syntax().text_range(), then_block.syntax().text()),
|
2019-05-15 22:54:32 -05:00
|
|
|
}
|
|
|
|
|
2019-07-20 04:58:27 -05:00
|
|
|
edit.insert(last_match_pat.syntax().text_range().end(), buf);
|
|
|
|
edit.set_cursor(last_match_pat.syntax().text_range().end() + TextUnit::from(1));
|
2019-05-15 22:54:32 -05:00
|
|
|
},
|
|
|
|
);
|
|
|
|
ctx.build()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
2019-07-04 15:05:17 -05:00
|
|
|
use crate::helpers::{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#"
|
|
|
|
fn f() {
|
|
|
|
let t = 'a';
|
|
|
|
let chars = "abcd";
|
|
|
|
match t {
|
|
|
|
'\r' <|>if chars.clone().next() == Some('\n') => false,
|
|
|
|
_ => true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"if chars.clone().next() == Some('\n')"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn move_guard_to_arm_body_works() {
|
|
|
|
check_assist(
|
|
|
|
move_guard_to_arm_body,
|
|
|
|
r#"
|
|
|
|
fn f() {
|
|
|
|
let t = 'a';
|
|
|
|
let chars = "abcd";
|
|
|
|
match t {
|
|
|
|
'\r' <|>if chars.clone().next() == Some('\n') => false,
|
|
|
|
_ => true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
fn f() {
|
|
|
|
let t = 'a';
|
|
|
|
let chars = "abcd";
|
|
|
|
match t {
|
|
|
|
'\r' => if chars.clone().next() == Some('\n') { <|>false },
|
|
|
|
_ => true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn move_guard_to_arm_body_works_complex_match() {
|
|
|
|
check_assist(
|
|
|
|
move_guard_to_arm_body,
|
|
|
|
r#"
|
|
|
|
fn f() {
|
|
|
|
match x {
|
|
|
|
<|>y @ 4 | y @ 5 if y > 5 => true,
|
|
|
|
_ => false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
fn f() {
|
|
|
|
match x {
|
|
|
|
y @ 4 | y @ 5 => if y > 5 { <|>true },
|
|
|
|
_ => false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn move_arm_cond_to_match_guard_works() {
|
|
|
|
check_assist(
|
|
|
|
move_arm_cond_to_match_guard,
|
|
|
|
r#"
|
|
|
|
fn f() {
|
|
|
|
let t = 'a';
|
|
|
|
let chars = "abcd";
|
|
|
|
match t {
|
|
|
|
'\r' => if chars.clone().next() == Some('\n') { <|>false },
|
|
|
|
_ => true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
fn f() {
|
|
|
|
let t = 'a';
|
|
|
|
let chars = "abcd";
|
|
|
|
match t {
|
|
|
|
'\r' <|>if chars.clone().next() == Some('\n') => false,
|
|
|
|
_ => true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn move_arm_cond_to_match_guard_if_let_not_works() {
|
|
|
|
check_assist_not_applicable(
|
|
|
|
move_arm_cond_to_match_guard,
|
|
|
|
r#"
|
|
|
|
fn f() {
|
|
|
|
let t = 'a';
|
|
|
|
let chars = "abcd";
|
|
|
|
match t {
|
|
|
|
'\r' => if let Some(_) = chars.clone().next() { <|>false },
|
|
|
|
_ => true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn move_arm_cond_to_match_guard_if_empty_body_works() {
|
|
|
|
check_assist(
|
|
|
|
move_arm_cond_to_match_guard,
|
|
|
|
r#"
|
|
|
|
fn f() {
|
|
|
|
let t = 'a';
|
|
|
|
let chars = "abcd";
|
|
|
|
match t {
|
|
|
|
'\r' => if chars.clone().next().is_some() { <|> },
|
|
|
|
_ => true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
fn f() {
|
|
|
|
let t = 'a';
|
|
|
|
let chars = "abcd";
|
|
|
|
match t {
|
|
|
|
'\r' <|>if chars.clone().next().is_some() => { },
|
|
|
|
_ => true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn move_arm_cond_to_match_guard_if_multiline_body_works() {
|
|
|
|
check_assist(
|
|
|
|
move_arm_cond_to_match_guard,
|
|
|
|
r#"
|
|
|
|
fn f() {
|
|
|
|
let mut t = 'a';
|
|
|
|
let chars = "abcd";
|
|
|
|
match t {
|
|
|
|
'\r' => if chars.clone().next().is_some() {
|
|
|
|
t = 'e';<|>
|
|
|
|
false
|
|
|
|
},
|
|
|
|
_ => true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
fn f() {
|
|
|
|
let mut t = 'a';
|
|
|
|
let chars = "abcd";
|
|
|
|
match t {
|
|
|
|
'\r' <|>if chars.clone().next().is_some() => {
|
|
|
|
t = 'e';
|
|
|
|
false
|
|
|
|
},
|
|
|
|
_ => true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|