Skip to content

Commit 57934ac

Browse files
committed
Add postfix completion for let else
1 parent 7219414 commit 57934ac

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed

crates/ide-completion/src/completions/postfix.rs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ pub(crate) fn complete_postfix(
7575

7676
let try_enum = TryEnum::from_ty(&ctx.sema, &receiver_ty.strip_references());
7777
if let Some(try_enum) = &try_enum {
78+
let in_loop = dot_receiver
79+
.syntax()
80+
.ancestors()
81+
.any(|n| matches!(n.kind(), WHILE_EXPR | LOOP_EXPR | FOR_EXPR));
82+
7883
match try_enum {
7984
TryEnum::Result => {
8085
postfix_snippet(
@@ -84,6 +89,17 @@ pub(crate) fn complete_postfix(
8489
)
8590
.add_to(acc, ctx.db);
8691

92+
postfix_snippet(
93+
"lete",
94+
"let Ok else {}",
95+
&if in_loop {
96+
format!("let Ok($1) = {receiver_text} else {{\n ${{2|continue,break,return|}};\n}};\n$0")
97+
} else {
98+
format!("let Ok($1) = {receiver_text} else {{\n return;\n}};\n$0")
99+
},
100+
)
101+
.add_to(acc, ctx.db);
102+
87103
postfix_snippet(
88104
"while",
89105
"while let Ok {}",
@@ -99,6 +115,17 @@ pub(crate) fn complete_postfix(
99115
)
100116
.add_to(acc, ctx.db);
101117

118+
postfix_snippet(
119+
"lete",
120+
"let Some else {}",
121+
&if in_loop {
122+
format!("let Some($1) = {receiver_text} else {{\n ${{2|continue,break,return|}};\n}};\n$0")
123+
} else {
124+
format!("let Some($1) = {receiver_text} else {{\n return;\n}};\n$0")
125+
},
126+
)
127+
.add_to(acc, ctx.db);
128+
102129
postfix_snippet(
103130
"while",
104131
"while let Some {}",
@@ -469,6 +496,56 @@ fn main() {
469496
);
470497
}
471498

499+
#[test]
500+
fn option_letelse() {
501+
check_edit(
502+
"lete",
503+
r#"
504+
//- minicore: option
505+
fn main() {
506+
let bar = Some(true);
507+
bar.$0
508+
}
509+
"#,
510+
r#"
511+
fn main() {
512+
let bar = Some(true);
513+
let Some($1) = bar else {
514+
return;
515+
};
516+
$0
517+
}
518+
"#,
519+
);
520+
}
521+
522+
#[test]
523+
fn option_letelse_loop() {
524+
check_edit(
525+
"lete",
526+
r#"
527+
//- minicore: option
528+
fn main() {
529+
let bar = Some(true);
530+
loop {
531+
bar.$0
532+
}
533+
}
534+
"#,
535+
r#"
536+
fn main() {
537+
let bar = Some(true);
538+
loop {
539+
let Some($1) = bar else {
540+
${2|continue,break,return|};
541+
};
542+
$0
543+
}
544+
}
545+
"#,
546+
);
547+
}
548+
472549
#[test]
473550
fn result_match() {
474551
check_edit(

0 commit comments

Comments
 (0)