From f83e452b1e69e8a6cc0d36715651418e3ce50afb Mon Sep 17 00:00:00 2001 From: Unreal Hoang Date: Thu, 2 May 2019 00:42:00 +0900 Subject: move guard to arm body assist. --- crates/ra_assists/src/lib.rs | 2 + crates/ra_assists/src/move_guard_to_arm_body.rs | 99 +++++++++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 crates/ra_assists/src/move_guard_to_arm_body.rs (limited to 'crates') diff --git a/crates/ra_assists/src/lib.rs b/crates/ra_assists/src/lib.rs index 4c330c907..a2998ae59 100644 --- a/crates/ra_assists/src/lib.rs +++ b/crates/ra_assists/src/lib.rs @@ -101,6 +101,7 @@ mod split_import; mod remove_dbg; pub mod auto_import; mod add_missing_impl_members; +mod move_guard_to_arm_body; fn all_assists() -> &'static [fn(AssistCtx) -> Option] { &[ @@ -120,6 +121,7 @@ fn all_assists() -> &'static [fn(AssistCtx) -> Option) -> Option { + let match_arm = ctx.node_at_offset::()?; + 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| { + edit.target(guard.syntax().range()); + let offseting_amount = match space_before_guard { + Some(SyntaxElement::Token(tok)) => { + if let Some(_) = ast::Whitespace::cast(tok) { + let ele = space_before_guard.unwrap().range(); + edit.delete(ele); + ele.len() + } else { + TextUnit::from(0) + } + }, + _ => TextUnit::from(0) + }; + + edit.delete(guard.syntax().range()); + edit.replace_node_and_indent(arm_expr.syntax(), buf); + edit.set_cursor( + arm_expr.syntax().range().start() + + TextUnit::from(3) - + offseting_amount); + }); + ctx.build() +} + +#[cfg(test)] +mod tests { + use super::*; + + use crate::helpers::{ check_assist, check_assist_target }; + + #[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 + } + } + "# + ); + } +} -- cgit v1.2.3 From d1c21b85cf7c8ca55bf0f83f2359d9018e321705 Mon Sep 17 00:00:00 2001 From: Unreal Hoang Date: Thu, 2 May 2019 00:57:16 +0900 Subject: add complex match case and documentation --- crates/ra_assists/src/move_guard_to_arm_body.rs | 42 +++++++++++++++++-------- 1 file changed, 29 insertions(+), 13 deletions(-) (limited to 'crates') diff --git a/crates/ra_assists/src/move_guard_to_arm_body.rs b/crates/ra_assists/src/move_guard_to_arm_body.rs index d6240527f..a8ca19f5d 100644 --- a/crates/ra_assists/src/move_guard_to_arm_body.rs +++ b/crates/ra_assists/src/move_guard_to_arm_body.rs @@ -1,6 +1,4 @@ -use hir::{ - db::HirDatabase, -}; +use hir::db::HirDatabase; use ra_syntax::{ TextUnit, SyntaxElement, @@ -17,9 +15,7 @@ pub(crate) fn move_guard_to_arm_body(mut ctx: AssistCtx) -> Op let guard_conditions = guard.expr()?; let arm_expr = match_arm.expr()?; - let buf = format!("if {} {{ {} }}", - guard_conditions.syntax().text(), - arm_expr.syntax().text()); + 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| { edit.target(guard.syntax().range()); @@ -32,16 +28,13 @@ pub(crate) fn move_guard_to_arm_body(mut ctx: AssistCtx) -> Op } else { TextUnit::from(0) } - }, - _ => TextUnit::from(0) + } + _ => TextUnit::from(0), }; edit.delete(guard.syntax().range()); edit.replace_node_and_indent(arm_expr.syntax(), buf); - edit.set_cursor( - arm_expr.syntax().range().start() + - TextUnit::from(3) - - offseting_amount); + edit.set_cursor(arm_expr.syntax().range().start() + TextUnit::from(3) - offseting_amount); }); ctx.build() } @@ -93,7 +86,30 @@ mod tests { _ => 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 + } + } + "#, ); } } -- cgit v1.2.3