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::{ use syntax::{
ast::{AstNode, IfExpr, MatchArm}, ast::{edit::AstNodeEdit, make, AstNode, IfExpr, MatchArm},
SyntaxKind::WHITESPACE, SyntaxKind::WHITESPACE,
}; };
@ -25,7 +25,9 @@ use crate::{AssistContext, AssistId, AssistKind, Assists};
// //
// fn handle(action: Action) { // fn handle(action: Action) {
// match 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 guard = match_arm.guard()?;
let space_before_guard = guard.syntax().prev_sibling_or_token(); 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 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(); let target = guard.syntax().text_range();
acc.add( 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.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 @@ mod tests {
check_assist_target( check_assist_target(
move_guard_to_arm_body, move_guard_to_arm_body,
r#" r#"
fn f() { fn main() {
let t = 'a'; match 92 {
let chars = "abcd"; x <|>if x > 10 => false,
match t { _ => true
'\r' <|>if chars.clone().next() == Some('\n') => false, }
_ => true }
} "#,
} r#"if x > 10"#,
"#,
r#"if chars.clone().next() == Some('\n')"#,
); );
} }
@ -152,25 +156,23 @@ mod tests {
check_assist( check_assist(
move_guard_to_arm_body, move_guard_to_arm_body,
r#" r#"
fn f() { fn main() {
let t = 'a'; match 92 {
let chars = "abcd"; x <|>if x > 10 => false,
match t { _ => true
'\r' <|>if chars.clone().next() == Some('\n') => false, }
_ => true }
} "#,
}
"#,
r#" r#"
fn f() { fn main() {
let t = 'a'; match 92 {
let chars = "abcd"; x => if x > 10 {
match t { false
'\r' => if chars.clone().next() == Some('\n') { false }, },
_ => true _ => true
} }
} }
"#, "#,
); );
} }
@ -179,21 +181,23 @@ mod tests {
check_assist( check_assist(
move_guard_to_arm_body, move_guard_to_arm_body,
r#" r#"
fn f() { fn main() {
match x { match 92 {
<|>y @ 4 | y @ 5 if y > 5 => true, <|>x @ 4 | x @ 5 if x > 5 => true,
_ => false _ => false
} }
} }
"#, "#,
r#" r#"
fn f() { fn main() {
match x { match 92 {
y @ 4 | y @ 5 => if y > 5 { true }, x @ 4 | x @ 5 => if x > 5 {
_ => false true
} },
} _ => false
"#, }
}
"#,
); );
} }
@ -202,25 +206,21 @@ mod tests {
check_assist( check_assist(
move_arm_cond_to_match_guard, move_arm_cond_to_match_guard,
r#" r#"
fn f() { fn main() {
let t = 'a'; match 92 {
let chars = "abcd"; x => if x > 10 { <|>false },
match t { _ => true
'\r' => if chars.clone().next() == Some('\n') { <|>false }, }
_ => true }
} "#,
}
"#,
r#" r#"
fn f() { fn main() {
let t = 'a'; match 92 {
let chars = "abcd"; x if x > 10 => false,
match t { _ => true
'\r' if chars.clone().next() == Some('\n') => false, }
_ => true }
} "#,
}
"#,
); );
} }
@ -229,15 +229,13 @@ mod tests {
check_assist_not_applicable( check_assist_not_applicable(
move_arm_cond_to_match_guard, move_arm_cond_to_match_guard,
r#" r#"
fn f() { fn main() {
let t = 'a'; match 92 {
let chars = "abcd"; x => if let 62 = x { <|>false },
match t { _ => true
'\r' => if let Some(_) = chars.clone().next() { <|>false }, }
_ => true }
} "#,
}
"#,
); );
} }
@ -246,25 +244,21 @@ mod tests {
check_assist( check_assist(
move_arm_cond_to_match_guard, move_arm_cond_to_match_guard,
r#" r#"
fn f() { fn main() {
let t = 'a'; match 92 {
let chars = "abcd"; x => if x > 10 { <|> },
match t { _ => true
'\r' => if chars.clone().next().is_some() { <|> }, }
_ => true }
} "#,
}
"#,
r#" r#"
fn f() { fn main() {
let t = 'a'; match 92 {
let chars = "abcd"; x if x > 10 => { },
match t { _ => true
'\r' if chars.clone().next().is_some() => { }, }
_ => true }
} "#,
}
"#,
); );
} }
@ -273,31 +267,27 @@ mod tests {
check_assist( check_assist(
move_arm_cond_to_match_guard, move_arm_cond_to_match_guard,
r#" r#"
fn f() { fn main() {
let mut t = 'a'; match 92 {
let chars = "abcd"; x => if x > 10 {
match t { 92;<|>
'\r' => if chars.clone().next().is_some() { false
t = 'e';<|> },
false _ => true
}, }
_ => true }
} "#,
}
"#,
r#" r#"
fn f() { fn main() {
let mut t = 'a'; match 92 {
let chars = "abcd"; x if x > 10 => {
match t { 92;
'\r' if chars.clone().next().is_some() => { false
t = 'e'; },
false _ => true
}, }
_ => true }
} "#,
}
"#,
); );
} }
} }

View File

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

View File

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