diff options
author | Aleksey Kladov <[email protected]> | 2020-05-20 23:01:08 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2020-05-20 23:01:08 +0100 |
commit | 4ac0abd2960acf1b3a357c681e64b3cddba6fc8e (patch) | |
tree | bef3928f908fd9dec53eae60303f6abfe32ffd9d | |
parent | 8300132ed0676496ec4adda49eb682ac24bdb5b9 (diff) |
Snippetify unwrap -> match
-rw-r--r-- | crates/ra_assists/src/handlers/replace_unwrap_with_match.rs | 41 | ||||
-rw-r--r-- | crates/ra_assists/src/tests/generated.rs | 2 | ||||
-rw-r--r-- | docs/user/assists.md | 2 |
3 files changed, 31 insertions, 14 deletions
diff --git a/crates/ra_assists/src/handlers/replace_unwrap_with_match.rs b/crates/ra_assists/src/handlers/replace_unwrap_with_match.rs index b379b55a8..cff7dfb81 100644 --- a/crates/ra_assists/src/handlers/replace_unwrap_with_match.rs +++ b/crates/ra_assists/src/handlers/replace_unwrap_with_match.rs | |||
@@ -9,7 +9,10 @@ use ra_syntax::{ | |||
9 | AstNode, | 9 | AstNode, |
10 | }; | 10 | }; |
11 | 11 | ||
12 | use crate::{utils::TryEnum, AssistContext, AssistId, Assists}; | 12 | use crate::{ |
13 | utils::{render_snippet, Cursor, TryEnum}, | ||
14 | AssistContext, AssistId, Assists, | ||
15 | }; | ||
13 | 16 | ||
14 | // Assist: replace_unwrap_with_match | 17 | // Assist: replace_unwrap_with_match |
15 | // | 18 | // |
@@ -29,7 +32,7 @@ use crate::{utils::TryEnum, AssistContext, AssistId, Assists}; | |||
29 | // let x: Result<i32, i32> = Result::Ok(92); | 32 | // let x: Result<i32, i32> = Result::Ok(92); |
30 | // let y = match x { | 33 | // let y = match x { |
31 | // Ok(a) => a, | 34 | // Ok(a) => a, |
32 | // _ => unreachable!(), | 35 | // $0_ => unreachable!(), |
33 | // }; | 36 | // }; |
34 | // } | 37 | // } |
35 | // ``` | 38 | // ``` |
@@ -43,7 +46,7 @@ pub(crate) fn replace_unwrap_with_match(acc: &mut Assists, ctx: &AssistContext) | |||
43 | let ty = ctx.sema.type_of_expr(&caller)?; | 46 | let ty = ctx.sema.type_of_expr(&caller)?; |
44 | let happy_variant = TryEnum::from_ty(&ctx.sema, &ty)?.happy_case(); | 47 | let happy_variant = TryEnum::from_ty(&ctx.sema, &ty)?.happy_case(); |
45 | let target = method_call.syntax().text_range(); | 48 | let target = method_call.syntax().text_range(); |
46 | acc.add(AssistId("replace_unwrap_with_match"), "Replace unwrap with match", target, |edit| { | 49 | acc.add(AssistId("replace_unwrap_with_match"), "Replace unwrap with match", target, |builder| { |
47 | let ok_path = make::path_unqualified(make::path_segment(make::name_ref(happy_variant))); | 50 | let ok_path = make::path_unqualified(make::path_segment(make::name_ref(happy_variant))); |
48 | let it = make::bind_pat(make::name("a")).into(); | 51 | let it = make::bind_pat(make::name("a")).into(); |
49 | let ok_tuple = make::tuple_struct_pat(ok_path, iter::once(it)).into(); | 52 | let ok_tuple = make::tuple_struct_pat(ok_path, iter::once(it)).into(); |
@@ -58,16 +61,30 @@ pub(crate) fn replace_unwrap_with_match(acc: &mut Assists, ctx: &AssistContext) | |||
58 | let match_expr = make::expr_match(caller.clone(), match_arm_list) | 61 | let match_expr = make::expr_match(caller.clone(), match_arm_list) |
59 | .indent(IndentLevel::from_node(method_call.syntax())); | 62 | .indent(IndentLevel::from_node(method_call.syntax())); |
60 | 63 | ||
61 | edit.set_cursor(caller.syntax().text_range().start()); | 64 | let range = method_call.syntax().text_range(); |
62 | edit.replace_ast::<ast::Expr>(method_call.into(), match_expr); | 65 | match ctx.config.snippet_cap { |
66 | Some(cap) => { | ||
67 | let err_arm = match_expr | ||
68 | .syntax() | ||
69 | .descendants() | ||
70 | .filter_map(ast::MatchArm::cast) | ||
71 | .last() | ||
72 | .unwrap(); | ||
73 | let snippet = | ||
74 | render_snippet(cap, match_expr.syntax(), Cursor::Before(err_arm.syntax())); | ||
75 | builder.replace_snippet(cap, range, snippet) | ||
76 | } | ||
77 | None => builder.replace(range, match_expr.to_string()), | ||
78 | } | ||
63 | }) | 79 | }) |
64 | } | 80 | } |
65 | 81 | ||
66 | #[cfg(test)] | 82 | #[cfg(test)] |
67 | mod tests { | 83 | mod tests { |
68 | use super::*; | ||
69 | use crate::tests::{check_assist, check_assist_target}; | 84 | use crate::tests::{check_assist, check_assist_target}; |
70 | 85 | ||
86 | use super::*; | ||
87 | |||
71 | #[test] | 88 | #[test] |
72 | fn test_replace_result_unwrap_with_match() { | 89 | fn test_replace_result_unwrap_with_match() { |
73 | check_assist( | 90 | check_assist( |
@@ -85,9 +102,9 @@ enum Result<T, E> { Ok(T), Err(E) } | |||
85 | fn i<T>(a: T) -> T { a } | 102 | fn i<T>(a: T) -> T { a } |
86 | fn main() { | 103 | fn main() { |
87 | let x: Result<i32, i32> = Result::Ok(92); | 104 | let x: Result<i32, i32> = Result::Ok(92); |
88 | let y = <|>match i(x) { | 105 | let y = match i(x) { |
89 | Ok(a) => a, | 106 | Ok(a) => a, |
90 | _ => unreachable!(), | 107 | $0_ => unreachable!(), |
91 | }; | 108 | }; |
92 | } | 109 | } |
93 | ", | 110 | ", |
@@ -111,9 +128,9 @@ enum Option<T> { Some(T), None } | |||
111 | fn i<T>(a: T) -> T { a } | 128 | fn i<T>(a: T) -> T { a } |
112 | fn main() { | 129 | fn main() { |
113 | let x = Option::Some(92); | 130 | let x = Option::Some(92); |
114 | let y = <|>match i(x) { | 131 | let y = match i(x) { |
115 | Some(a) => a, | 132 | Some(a) => a, |
116 | _ => unreachable!(), | 133 | $0_ => unreachable!(), |
117 | }; | 134 | }; |
118 | } | 135 | } |
119 | ", | 136 | ", |
@@ -137,9 +154,9 @@ enum Result<T, E> { Ok(T), Err(E) } | |||
137 | fn i<T>(a: T) -> T { a } | 154 | fn i<T>(a: T) -> T { a } |
138 | fn main() { | 155 | fn main() { |
139 | let x: Result<i32, i32> = Result::Ok(92); | 156 | let x: Result<i32, i32> = Result::Ok(92); |
140 | let y = <|>match i(x) { | 157 | let y = match i(x) { |
141 | Ok(a) => a, | 158 | Ok(a) => a, |
142 | _ => unreachable!(), | 159 | $0_ => unreachable!(), |
143 | }.count_zeroes(); | 160 | }.count_zeroes(); |
144 | } | 161 | } |
145 | ", | 162 | ", |
diff --git a/crates/ra_assists/src/tests/generated.rs b/crates/ra_assists/src/tests/generated.rs index 0eeb5c199..250e56a69 100644 --- a/crates/ra_assists/src/tests/generated.rs +++ b/crates/ra_assists/src/tests/generated.rs | |||
@@ -764,7 +764,7 @@ fn main() { | |||
764 | let x: Result<i32, i32> = Result::Ok(92); | 764 | let x: Result<i32, i32> = Result::Ok(92); |
765 | let y = match x { | 765 | let y = match x { |
766 | Ok(a) => a, | 766 | Ok(a) => a, |
767 | _ => unreachable!(), | 767 | $0_ => unreachable!(), |
768 | }; | 768 | }; |
769 | } | 769 | } |
770 | "#####, | 770 | "#####, |
diff --git a/docs/user/assists.md b/docs/user/assists.md index a6e27d67f..4ad7ea59d 100644 --- a/docs/user/assists.md +++ b/docs/user/assists.md | |||
@@ -733,7 +733,7 @@ fn main() { | |||
733 | let x: Result<i32, i32> = Result::Ok(92); | 733 | let x: Result<i32, i32> = Result::Ok(92); |
734 | let y = match x { | 734 | let y = match x { |
735 | Ok(a) => a, | 735 | Ok(a) => a, |
736 | _ => unreachable!(), | 736 | $0_ => unreachable!(), |
737 | }; | 737 | }; |
738 | } | 738 | } |
739 | ``` | 739 | ``` |