diff options
-rw-r--r-- | crates/ra_assists/src/move_guard_to_arm_body.rs | 42 | ||||
-rw-r--r-- | docs/user/features.md | 18 |
2 files changed, 47 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 | } |
diff --git a/docs/user/features.md b/docs/user/features.md index cbfc491b2..a714574fb 100644 --- a/docs/user/features.md +++ b/docs/user/features.md | |||
@@ -388,6 +388,24 @@ fn foo() { | |||
388 | } | 388 | } |
389 | ``` | 389 | ``` |
390 | 390 | ||
391 | - Move guard expression to match arm body | ||
392 | ```rust | ||
393 | //before: | ||
394 | fn f() { | ||
395 | match x { | ||
396 | <|>y @ 4 | y @ 5 if y > 5 => true, | ||
397 | _ => false | ||
398 | } | ||
399 | } | ||
400 | //after: | ||
401 | fn f() { | ||
402 | match x { | ||
403 | y @ 4 | y @ 5 => if y > 5 { <|>true }, | ||
404 | _ => false | ||
405 | } | ||
406 | } | ||
407 | ``` | ||
408 | |||
391 | ### Magic Completions | 409 | ### Magic Completions |
392 | 410 | ||
393 | In addition to usual reference completion, rust-analyzer provides some ✨magic✨ | 411 | In addition to usual reference completion, rust-analyzer provides some ✨magic✨ |