aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_assists/src/utils.rs4
-rw-r--r--crates/ra_hir/src/code_model.rs22
-rw-r--r--crates/ra_ide/src/completion/complete_postfix.rs82
3 files changed, 46 insertions, 62 deletions
diff --git a/crates/ra_assists/src/utils.rs b/crates/ra_assists/src/utils.rs
index 2f15a3f15..f3fc92ebf 100644
--- a/crates/ra_assists/src/utils.rs
+++ b/crates/ra_assists/src/utils.rs
@@ -103,7 +103,7 @@ fn invert_special_case(expr: &ast::Expr) -> Option<ast::Expr> {
103} 103}
104 104
105#[derive(Clone, Copy)] 105#[derive(Clone, Copy)]
106pub(crate) enum TryEnum { 106pub enum TryEnum {
107 Result, 107 Result,
108 Option, 108 Option,
109} 109}
@@ -111,7 +111,7 @@ pub(crate) enum TryEnum {
111impl TryEnum { 111impl TryEnum {
112 const ALL: [TryEnum; 2] = [TryEnum::Option, TryEnum::Result]; 112 const ALL: [TryEnum; 2] = [TryEnum::Option, TryEnum::Result];
113 113
114 pub(crate) fn from_ty(sema: &Semantics<RootDatabase>, ty: &Type) -> Option<TryEnum> { 114 pub fn from_ty(sema: &Semantics<RootDatabase>, ty: &Type) -> Option<TryEnum> {
115 let enum_ = match ty.as_adt() { 115 let enum_ = match ty.as_adt() {
116 Some(Adt::Enum(it)) => it, 116 Some(Adt::Enum(it)) => it,
117 _ => return None, 117 _ => return None,
diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs
index 6d8444462..be18c845c 100644
--- a/crates/ra_hir/src/code_model.rs
+++ b/crates/ra_hir/src/code_model.rs
@@ -1086,28 +1086,6 @@ impl Type {
1086 matches!(self.ty.value, Ty::Apply(ApplicationTy { ctor: TypeCtor::Bool, .. })) 1086 matches!(self.ty.value, Ty::Apply(ApplicationTy { ctor: TypeCtor::Bool, .. }))
1087 } 1087 }
1088 1088
1089 pub fn is_option(&self, db: &dyn HirDatabase) -> bool {
1090 if let Some(adt_ty) = self.as_adt() {
1091 if let Adt::Enum(_) = adt_ty {
1092 if self.display(db).to_string().starts_with("Option<") {
1093 return true;
1094 }
1095 }
1096 }
1097 false
1098 }
1099
1100 pub fn is_result(&self, db: &dyn HirDatabase) -> bool {
1101 if let Some(adt_ty) = self.as_adt() {
1102 if let Adt::Enum(_) = adt_ty {
1103 if self.display(db).to_string().starts_with("Result<") {
1104 return true;
1105 }
1106 }
1107 }
1108 false
1109 }
1110
1111 pub fn is_mutable_reference(&self) -> bool { 1089 pub fn is_mutable_reference(&self) -> bool {
1112 matches!( 1090 matches!(
1113 self.ty.value, 1091 self.ty.value,
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};
17use ra_assists::utils::TryEnum;
17 18
18pub(super) fn complete_postfix(acc: &mut Completions, ctx: &CompletionContext) { 19pub(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,