diff options
author | Benjamin Coenen <[email protected]> | 2020-05-10 11:45:35 +0100 |
---|---|---|
committer | Benjamin Coenen <[email protected]> | 2020-05-10 11:45:35 +0100 |
commit | 92b2230fefe61322dbca8194c8721f848c5d1c2f (patch) | |
tree | de64e186fe43e3f317aaa2df0ef5d7f4a6c96414 /crates/ra_ide | |
parent | e80903a96564c2239489a8c630a4748bf21a3659 (diff) |
add if let and while let postfix for Option and Result
Signed-off-by: Benjamin Coenen <[email protected]>
Diffstat (limited to 'crates/ra_ide')
-rw-r--r-- | crates/ra_ide/src/completion/complete_postfix.rs | 82 |
1 files changed, 44 insertions, 38 deletions
diff --git a/crates/ra_ide/src/completion/complete_postfix.rs b/crates/ra_ide/src/completion/complete_postfix.rs index dc32bbee2..c5c4426cc 100644 --- a/crates/ra_ide/src/completion/complete_postfix.rs +++ b/crates/ra_ide/src/completion/complete_postfix.rs | |||
@@ -14,6 +14,7 @@ use crate::{ | |||
14 | }, | 14 | }, |
15 | CompletionItem, | 15 | CompletionItem, |
16 | }; | 16 | }; |
17 | use ra_assists::utils::TryEnum; | ||
17 | 18 | ||
18 | pub(super) fn complete_postfix(acc: &mut Completions, ctx: &CompletionContext) { | 19 | pub(super) fn complete_postfix(acc: &mut Completions, ctx: &CompletionContext) { |
19 | if !ctx.config.enable_postfix_completions { | 20 | if !ctx.config.enable_postfix_completions { |
@@ -38,46 +39,51 @@ pub(super) fn complete_postfix(acc: &mut Completions, ctx: &CompletionContext) { | |||
38 | None => return, | 39 | None => return, |
39 | }; | 40 | }; |
40 | 41 | ||
41 | if receiver_ty.is_option(ctx.db) { | 42 | if let Some(try_enum) = TryEnum::from_ty(&ctx.sema, &receiver_ty) { |
42 | postfix_snippet( | 43 | match try_enum { |
43 | ctx, | 44 | TryEnum::Result => { |
44 | cap, | 45 | postfix_snippet( |
45 | &dot_receiver, | 46 | ctx, |
46 | "ifl", | 47 | cap, |
47 | "if let Some {}", | 48 | &dot_receiver, |
48 | &format!("if let Some($1) = {} {{\n $0\n}}", receiver_text), | 49 | "ifl", |
49 | ) | 50 | "if let Ok {}", |
50 | .add_to(acc); | 51 | &format!("if let Ok($1) = {} {{\n $0\n}}", receiver_text), |
52 | ) | ||
53 | .add_to(acc); | ||
51 | 54 | ||
52 | postfix_snippet( | 55 | postfix_snippet( |
53 | ctx, | 56 | ctx, |
54 | cap, | 57 | cap, |
55 | &dot_receiver, | 58 | &dot_receiver, |
56 | "while", | 59 | "while", |
57 | "while let Some {}", | 60 | "while let Ok {}", |
58 | &format!("while let Some($1) = {} {{\n $0\n}}", receiver_text), | 61 | &format!("while let Ok($1) = {} {{\n $0\n}}", receiver_text), |
59 | ) | 62 | ) |
60 | .add_to(acc); | 63 | .add_to(acc); |
61 | } else if receiver_ty.is_result(ctx.db) { | 64 | } |
62 | postfix_snippet( | 65 | TryEnum::Option => { |
63 | ctx, | 66 | postfix_snippet( |
64 | cap, | 67 | ctx, |
65 | &dot_receiver, | 68 | cap, |
66 | "ifl", | 69 | &dot_receiver, |
67 | "if let Ok {}", | 70 | "ifl", |
68 | &format!("if let Ok($1) = {} {{\n $0\n}}", receiver_text), | 71 | "if let Some {}", |
69 | ) | 72 | &format!("if let Some($1) = {} {{\n $0\n}}", receiver_text), |
70 | .add_to(acc); | 73 | ) |
74 | .add_to(acc); | ||
71 | 75 | ||
72 | postfix_snippet( | 76 | postfix_snippet( |
73 | ctx, | 77 | ctx, |
74 | cap, | 78 | cap, |
75 | &dot_receiver, | 79 | &dot_receiver, |
76 | "while", | 80 | "while", |
77 | "while let Ok {}", | 81 | "while let Some {}", |
78 | &format!("while let Ok($1) = {} {{\n $0\n}}", receiver_text), | 82 | &format!("while let Some($1) = {} {{\n $0\n}}", receiver_text), |
79 | ) | 83 | ) |
80 | .add_to(acc); | 84 | .add_to(acc); |
85 | } | ||
86 | } | ||
81 | } else if receiver_ty.is_bool() || receiver_ty.is_unknown() { | 87 | } else if receiver_ty.is_bool() || receiver_ty.is_unknown() { |
82 | postfix_snippet( | 88 | postfix_snippet( |
83 | ctx, | 89 | ctx, |