Skip to content

Commit a84bd9e

Browse files
bors[bot]bnjjj
andauthored
Merge #4434
4434: add more specific match postfix for Result and Option r=matklad a=bnjjj In order to have the same behavior than `if let` and `while let` Co-authored-by: Benjamin Coenen <5719034+bnjjj@users.noreply.github.com>
2 parents 9594fe3 + df33022 commit a84bd9e

File tree

1 file changed

+43
-15
lines changed

1 file changed

+43
-15
lines changed

crates/ra_ide/src/completion/complete_postfix.rs

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ pub(super) fn complete_postfix(acc: &mut Completions, ctx: &CompletionContext) {
3838
Some(it) => it,
3939
None => return,
4040
};
41-
42-
if let Some(try_enum) = TryEnum::from_ty(&ctx.sema, &receiver_ty) {
41+
let try_enum = TryEnum::from_ty(&ctx.sema, &receiver_ty);
42+
if let Some(try_enum) = &try_enum {
4343
match try_enum {
4444
TryEnum::Result => {
4545
postfix_snippet(
@@ -104,7 +104,6 @@ pub(super) fn complete_postfix(acc: &mut Completions, ctx: &CompletionContext) {
104104
)
105105
.add_to(acc);
106106
}
107-
108107
// !&&&42 is a compiler error, ergo process it before considering the references
109108
postfix_snippet(ctx, cap, &dot_receiver, "not", "!expr", &format!("!{}", receiver_text))
110109
.add_to(acc);
@@ -126,16 +125,45 @@ pub(super) fn complete_postfix(acc: &mut Completions, ctx: &CompletionContext) {
126125
let dot_receiver = include_references(dot_receiver);
127126
let receiver_text =
128127
get_receiver_text(&dot_receiver, ctx.dot_receiver_is_ambiguous_float_literal);
129-
130-
postfix_snippet(
131-
ctx,
132-
cap,
133-
&dot_receiver,
134-
"match",
135-
"match expr {}",
136-
&format!("match {} {{\n ${{1:_}} => {{$0\\}},\n}}", receiver_text),
137-
)
138-
.add_to(acc);
128+
match try_enum {
129+
Some(try_enum) => {
130+
match try_enum {
131+
TryEnum::Result => {
132+
postfix_snippet(
133+
ctx,
134+
cap,
135+
&dot_receiver,
136+
"match",
137+
"match expr {}",
138+
&format!("match {} {{\n Ok(${{1:_}}) => {{$2\\}},\n Err(${{3:_}}) => {{$0\\}},\n}}", receiver_text),
139+
)
140+
.add_to(acc);
141+
}
142+
TryEnum::Option => {
143+
postfix_snippet(
144+
ctx,
145+
cap,
146+
&dot_receiver,
147+
"match",
148+
"match expr {}",
149+
&format!("match {} {{\n Some(${{1:_}}) => {{$2\\}},\n None => {{$0\\}},\n}}", receiver_text),
150+
)
151+
.add_to(acc);
152+
}
153+
}
154+
}
155+
None => {
156+
postfix_snippet(
157+
ctx,
158+
cap,
159+
&dot_receiver,
160+
"match",
161+
"match expr {}",
162+
&format!("match {} {{\n ${{1:_}} => {{$0\\}},\n}}", receiver_text),
163+
)
164+
.add_to(acc);
165+
}
166+
}
139167

140168
postfix_snippet(
141169
ctx,
@@ -324,7 +352,7 @@ mod tests {
324352
label: "match",
325353
source_range: 210..210,
326354
delete: 206..210,
327-
insert: "match bar {\n ${1:_} => {$0\\},\n}",
355+
insert: "match bar {\n Some(${1:_}) => {$2\\},\n None => {$0\\},\n}",
328356
detail: "match expr {}",
329357
},
330358
CompletionItem {
@@ -403,7 +431,7 @@ mod tests {
403431
label: "match",
404432
source_range: 211..211,
405433
delete: 207..211,
406-
insert: "match bar {\n ${1:_} => {$0\\},\n}",
434+
insert: "match bar {\n Ok(${1:_}) => {$2\\},\n Err(${3:_}) => {$0\\},\n}",
407435
detail: "match expr {}",
408436
},
409437
CompletionItem {

0 commit comments

Comments
 (0)