Skip to content

Commit 92b2230

Browse files
committed
add if let and while let postfix for Option and Result
Signed-off-by: Benjamin Coenen <5719034+bnjjj@users.noreply.github.com>
1 parent e80903a commit 92b2230

File tree

3 files changed

+46
-62
lines changed

3 files changed

+46
-62
lines changed

crates/ra_assists/src/utils.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,15 +103,15 @@ fn invert_special_case(expr: &ast::Expr) -> Option<ast::Expr> {
103103
}
104104

105105
#[derive(Clone, Copy)]
106-
pub(crate) enum TryEnum {
106+
pub enum TryEnum {
107107
Result,
108108
Option,
109109
}
110110

111111
impl TryEnum {
112112
const ALL: [TryEnum; 2] = [TryEnum::Option, TryEnum::Result];
113113

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> {
115115
let enum_ = match ty.as_adt() {
116116
Some(Adt::Enum(it)) => it,
117117
_ => return None,

crates/ra_hir/src/code_model.rs

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1086,28 +1086,6 @@ impl Type {
10861086
matches!(self.ty.value, Ty::Apply(ApplicationTy { ctor: TypeCtor::Bool, .. }))
10871087
}
10881088

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-
11111089
pub fn is_mutable_reference(&self) -> bool {
11121090
matches!(
11131091
self.ty.value,

crates/ra_ide/src/completion/complete_postfix.rs

Lines changed: 44 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use crate::{
1414
},
1515
CompletionItem,
1616
};
17+
use ra_assists::utils::TryEnum;
1718

1819
pub(super) fn complete_postfix(acc: &mut Completions, ctx: &CompletionContext) {
1920
if !ctx.config.enable_postfix_completions {
@@ -38,46 +39,51 @@ pub(super) fn complete_postfix(acc: &mut Completions, ctx: &CompletionContext) {
3839
None => return,
3940
};
4041

41-
if receiver_ty.is_option(ctx.db) {
42-
postfix_snippet(
43-
ctx,
44-
cap,
45-
&dot_receiver,
46-
"ifl",
47-
"if let Some {}",
48-
&format!("if let Some($1) = {} {{\n $0\n}}", receiver_text),
49-
)
50-
.add_to(acc);
42+
if let Some(try_enum) = TryEnum::from_ty(&ctx.sema, &receiver_ty) {
43+
match try_enum {
44+
TryEnum::Result => {
45+
postfix_snippet(
46+
ctx,
47+
cap,
48+
&dot_receiver,
49+
"ifl",
50+
"if let Ok {}",
51+
&format!("if let Ok($1) = {} {{\n $0\n}}", receiver_text),
52+
)
53+
.add_to(acc);
5154

52-
postfix_snippet(
53-
ctx,
54-
cap,
55-
&dot_receiver,
56-
"while",
57-
"while let Some {}",
58-
&format!("while let Some($1) = {} {{\n $0\n}}", receiver_text),
59-
)
60-
.add_to(acc);
61-
} else if receiver_ty.is_result(ctx.db) {
62-
postfix_snippet(
63-
ctx,
64-
cap,
65-
&dot_receiver,
66-
"ifl",
67-
"if let Ok {}",
68-
&format!("if let Ok($1) = {} {{\n $0\n}}", receiver_text),
69-
)
70-
.add_to(acc);
55+
postfix_snippet(
56+
ctx,
57+
cap,
58+
&dot_receiver,
59+
"while",
60+
"while let Ok {}",
61+
&format!("while let Ok($1) = {} {{\n $0\n}}", receiver_text),
62+
)
63+
.add_to(acc);
64+
}
65+
TryEnum::Option => {
66+
postfix_snippet(
67+
ctx,
68+
cap,
69+
&dot_receiver,
70+
"ifl",
71+
"if let Some {}",
72+
&format!("if let Some($1) = {} {{\n $0\n}}", receiver_text),
73+
)
74+
.add_to(acc);
7175

72-
postfix_snippet(
73-
ctx,
74-
cap,
75-
&dot_receiver,
76-
"while",
77-
"while let Ok {}",
78-
&format!("while let Ok($1) = {} {{\n $0\n}}", receiver_text),
79-
)
80-
.add_to(acc);
76+
postfix_snippet(
77+
ctx,
78+
cap,
79+
&dot_receiver,
80+
"while",
81+
"while let Some {}",
82+
&format!("while let Some($1) = {} {{\n $0\n}}", receiver_text),
83+
)
84+
.add_to(acc);
85+
}
86+
}
8187
} else if receiver_ty.is_bool() || receiver_ty.is_unknown() {
8288
postfix_snippet(
8389
ctx,

0 commit comments

Comments
 (0)