diff options
Diffstat (limited to 'crates/ra_assists/src')
-rw-r--r-- | crates/ra_assists/src/move_guard_to_arm_body.rs | 42 |
1 files changed, 29 insertions, 13 deletions
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 @@ | |||
1 | use hir::{ | 1 | use hir::db::HirDatabase; |
2 | db::HirDatabase, | ||
3 | }; | ||
4 | use ra_syntax::{ | 2 | use ra_syntax::{ |
5 | TextUnit, | 3 | TextUnit, |
6 | SyntaxElement, | 4 | SyntaxElement, |
@@ -17,9 +15,7 @@ pub(crate) fn move_guard_to_arm_body(mut ctx: AssistCtx<impl HirDatabase>) -> Op | |||
17 | 15 | ||
18 | let guard_conditions = guard.expr()?; | 16 | let guard_conditions = guard.expr()?; |
19 | let arm_expr = match_arm.expr()?; | 17 | let arm_expr = match_arm.expr()?; |
20 | let buf = format!("if {} {{ {} }}", | 18 | let buf = format!("if {} {{ {} }}", guard_conditions.syntax().text(), arm_expr.syntax().text()); |
21 | guard_conditions.syntax().text(), | ||
22 | arm_expr.syntax().text()); | ||
23 | 19 | ||
24 | ctx.add_action(AssistId("move_guard_to_arm_body"), "move guard to arm body", |edit| { | 20 | ctx.add_action(AssistId("move_guard_to_arm_body"), "move guard to arm body", |edit| { |
25 | edit.target(guard.syntax().range()); | 21 | edit.target(guard.syntax().range()); |
@@ -32,16 +28,13 @@ pub(crate) fn move_guard_to_arm_body(mut ctx: AssistCtx<impl HirDatabase>) -> Op | |||
32 | } else { | 28 | } else { |
33 | TextUnit::from(0) | 29 | TextUnit::from(0) |
34 | } | 30 | } |
35 | }, | 31 | } |
36 | _ => TextUnit::from(0) | 32 | _ => TextUnit::from(0), |
37 | }; | 33 | }; |
38 | 34 | ||
39 | edit.delete(guard.syntax().range()); | 35 | edit.delete(guard.syntax().range()); |
40 | edit.replace_node_and_indent(arm_expr.syntax(), buf); | 36 | edit.replace_node_and_indent(arm_expr.syntax(), buf); |
41 | edit.set_cursor( | 37 | edit.set_cursor(arm_expr.syntax().range().start() + TextUnit::from(3) - offseting_amount); |
42 | arm_expr.syntax().range().start() + | ||
43 | TextUnit::from(3) - | ||
44 | offseting_amount); | ||
45 | }); | 38 | }); |
46 | ctx.build() | 39 | ctx.build() |
47 | } | 40 | } |
@@ -93,7 +86,30 @@ mod tests { | |||
93 | _ => true | 86 | _ => true |
94 | } | 87 | } |
95 | } | 88 | } |
96 | "# | 89 | "#, |
90 | ); | ||
91 | } | ||
92 | |||
93 | #[test] | ||
94 | fn move_guard_to_arm_body_works_complex_match() { | ||
95 | check_assist( | ||
96 | move_guard_to_arm_body, | ||
97 | r#" | ||
98 | fn f() { | ||
99 | match x { | ||
100 | <|>y @ 4 | y @ 5 if y > 5 => true, | ||
101 | _ => false | ||
102 | } | ||
103 | } | ||
104 | "#, | ||
105 | r#" | ||
106 | fn f() { | ||
107 | match x { | ||
108 | y @ 4 | y @ 5 => if y > 5 { <|>true }, | ||
109 | _ => false | ||
110 | } | ||
111 | } | ||
112 | "#, | ||
97 | ); | 113 | ); |
98 | } | 114 | } |
99 | } | 115 | } |