Cleanup **Move Guard** assist

This commit is contained in:
Aleksey Kladov 2020-08-13 10:32:03 +02:00
parent 982b299252
commit 26b98b07aa
3 changed files with 112 additions and 117 deletions

View File

@ -1,5 +1,5 @@
use syntax::{
ast::{AstNode, IfExpr, MatchArm},
ast::{edit::AstNodeEdit, make, AstNode, IfExpr, MatchArm},
SyntaxKind::WHITESPACE,
};
@ -25,7 +25,9 @@
//
// fn handle(action: Action) {
// match action {
// Action::Move { distance } => if distance > 10 { foo() },
// Action::Move { distance } => if distance > 10 {
// foo()
// },
// _ => (),
// }
// }
@ -35,9 +37,13 @@ pub(crate) fn move_guard_to_arm_body(acc: &mut Assists, ctx: &AssistContext) ->
let guard = match_arm.guard()?;
let space_before_guard = guard.syntax().prev_sibling_or_token();
let guard_conditions = guard.expr()?;
let guard_condition = guard.expr()?;
let arm_expr = match_arm.expr()?;
let buf = format!("if {} {{ {} }}", guard_conditions.syntax().text(), arm_expr.syntax().text());
let if_expr = make::expr_if(
make::condition(guard_condition, None),
make::block_expr(None, Some(arm_expr.clone())),
)
.indent(arm_expr.indent_level());
let target = guard.syntax().text_range();
acc.add(
@ -53,7 +59,7 @@ pub(crate) fn move_guard_to_arm_body(acc: &mut Assists, ctx: &AssistContext) ->
};
edit.delete(guard.syntax().text_range());
edit.replace_node_and_indent(arm_expr.syntax(), buf);
edit.replace_ast(arm_expr, if_expr);
},
)
}
@ -134,16 +140,14 @@ 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,
fn main() {
match 92 {
x <|>if x > 10 => false,
_ => true
}
}
"#,
r#"if chars.clone().next() == Some('\n')"#,
r#"if x > 10"#,
);
}
@ -152,21 +156,19 @@ 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,
fn main() {
match 92 {
x <|>if x > 10 => false,
_ => true
}
}
"#,
r#"
fn f() {
let t = 'a';
let chars = "abcd";
match t {
'\r' => if chars.clone().next() == Some('\n') { false },
fn main() {
match 92 {
x => if x > 10 {
false
},
_ => true
}
}
@ -179,17 +181,19 @@ 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,
fn main() {
match 92 {
<|>x @ 4 | x @ 5 if x > 5 => true,
_ => false
}
}
"#,
r#"
fn f() {
match x {
y @ 4 | y @ 5 => if y > 5 { true },
fn main() {
match 92 {
x @ 4 | x @ 5 => if x > 5 {
true
},
_ => false
}
}
@ -202,21 +206,17 @@ 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 },
fn main() {
match 92 {
x => if x > 10 { <|>false },
_ => true
}
}
"#,
r#"
fn f() {
let t = 'a';
let chars = "abcd";
match t {
'\r' if chars.clone().next() == Some('\n') => false,
fn main() {
match 92 {
x if x > 10 => false,
_ => true
}
}
@ -229,11 +229,9 @@ 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 },
fn main() {
match 92 {
x => if let 62 = x { <|>false },
_ => true
}
}
@ -246,21 +244,17 @@ 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() { <|> },
fn main() {
match 92 {
x => if x > 10 { <|> },
_ => true
}
}
"#,
r#"
fn f() {
let t = 'a';
let chars = "abcd";
match t {
'\r' if chars.clone().next().is_some() => { },
fn main() {
match 92 {
x if x > 10 => { },
_ => true
}
}
@ -273,12 +267,10 @@ 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';<|>
fn main() {
match 92 {
x => if x > 10 {
92;<|>
false
},
_ => true
@ -286,12 +278,10 @@ fn f() {
}
"#,
r#"
fn f() {
let mut t = 'a';
let chars = "abcd";
match t {
'\r' if chars.clone().next().is_some() => {
t = 'e';
fn main() {
match 92 {
x if x > 10 => {
92;
false
},
_ => true

View File

@ -690,7 +690,9 @@ enum Action { Move { distance: u32 }, Stop }
fn handle(action: Action) {
match action {
Action::Move { distance } => if distance > 10 { foo() },
Action::Move { distance } => if distance > 10 {
foo()
},
_ => (),
}
}

View File

@ -601,6 +601,9 @@ fn replace_descendants<D: AstNode>(
}
rewriter.rewrite_ast(self)
}
fn indent_level(&self) -> IndentLevel {
IndentLevel::from_node(self.syntax())
}
#[must_use]
fn indent(&self, level: IndentLevel) -> Self {
Self::cast(level.increase_indent(self.syntax().clone())).unwrap()