mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 12:54:58 +00:00
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>
This commit is contained in:
commit
a84bd9e18c
1 changed files with 43 additions and 15 deletions
|
@ -38,8 +38,8 @@ pub(super) fn complete_postfix(acc: &mut Completions, ctx: &CompletionContext) {
|
||||||
Some(it) => it,
|
Some(it) => it,
|
||||||
None => return,
|
None => return,
|
||||||
};
|
};
|
||||||
|
let try_enum = TryEnum::from_ty(&ctx.sema, &receiver_ty);
|
||||||
if let Some(try_enum) = TryEnum::from_ty(&ctx.sema, &receiver_ty) {
|
if let Some(try_enum) = &try_enum {
|
||||||
match try_enum {
|
match try_enum {
|
||||||
TryEnum::Result => {
|
TryEnum::Result => {
|
||||||
postfix_snippet(
|
postfix_snippet(
|
||||||
|
@ -104,7 +104,6 @@ pub(super) fn complete_postfix(acc: &mut Completions, ctx: &CompletionContext) {
|
||||||
)
|
)
|
||||||
.add_to(acc);
|
.add_to(acc);
|
||||||
}
|
}
|
||||||
|
|
||||||
// !&&&42 is a compiler error, ergo process it before considering the references
|
// !&&&42 is a compiler error, ergo process it before considering the references
|
||||||
postfix_snippet(ctx, cap, &dot_receiver, "not", "!expr", &format!("!{}", receiver_text))
|
postfix_snippet(ctx, cap, &dot_receiver, "not", "!expr", &format!("!{}", receiver_text))
|
||||||
.add_to(acc);
|
.add_to(acc);
|
||||||
|
@ -126,7 +125,34 @@ pub(super) fn complete_postfix(acc: &mut Completions, ctx: &CompletionContext) {
|
||||||
let dot_receiver = include_references(dot_receiver);
|
let dot_receiver = include_references(dot_receiver);
|
||||||
let receiver_text =
|
let receiver_text =
|
||||||
get_receiver_text(&dot_receiver, ctx.dot_receiver_is_ambiguous_float_literal);
|
get_receiver_text(&dot_receiver, ctx.dot_receiver_is_ambiguous_float_literal);
|
||||||
|
match try_enum {
|
||||||
|
Some(try_enum) => {
|
||||||
|
match try_enum {
|
||||||
|
TryEnum::Result => {
|
||||||
|
postfix_snippet(
|
||||||
|
ctx,
|
||||||
|
cap,
|
||||||
|
&dot_receiver,
|
||||||
|
"match",
|
||||||
|
"match expr {}",
|
||||||
|
&format!("match {} {{\n Ok(${{1:_}}) => {{$2\\}},\n Err(${{3:_}}) => {{$0\\}},\n}}", receiver_text),
|
||||||
|
)
|
||||||
|
.add_to(acc);
|
||||||
|
}
|
||||||
|
TryEnum::Option => {
|
||||||
|
postfix_snippet(
|
||||||
|
ctx,
|
||||||
|
cap,
|
||||||
|
&dot_receiver,
|
||||||
|
"match",
|
||||||
|
"match expr {}",
|
||||||
|
&format!("match {} {{\n Some(${{1:_}}) => {{$2\\}},\n None => {{$0\\}},\n}}", receiver_text),
|
||||||
|
)
|
||||||
|
.add_to(acc);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None => {
|
||||||
postfix_snippet(
|
postfix_snippet(
|
||||||
ctx,
|
ctx,
|
||||||
cap,
|
cap,
|
||||||
|
@ -136,6 +162,8 @@ pub(super) fn complete_postfix(acc: &mut Completions, ctx: &CompletionContext) {
|
||||||
&format!("match {} {{\n ${{1:_}} => {{$0\\}},\n}}", receiver_text),
|
&format!("match {} {{\n ${{1:_}} => {{$0\\}},\n}}", receiver_text),
|
||||||
)
|
)
|
||||||
.add_to(acc);
|
.add_to(acc);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
postfix_snippet(
|
postfix_snippet(
|
||||||
ctx,
|
ctx,
|
||||||
|
@ -324,7 +352,7 @@ mod tests {
|
||||||
label: "match",
|
label: "match",
|
||||||
source_range: 210..210,
|
source_range: 210..210,
|
||||||
delete: 206..210,
|
delete: 206..210,
|
||||||
insert: "match bar {\n ${1:_} => {$0\\},\n}",
|
insert: "match bar {\n Some(${1:_}) => {$2\\},\n None => {$0\\},\n}",
|
||||||
detail: "match expr {}",
|
detail: "match expr {}",
|
||||||
},
|
},
|
||||||
CompletionItem {
|
CompletionItem {
|
||||||
|
@ -403,7 +431,7 @@ mod tests {
|
||||||
label: "match",
|
label: "match",
|
||||||
source_range: 211..211,
|
source_range: 211..211,
|
||||||
delete: 207..211,
|
delete: 207..211,
|
||||||
insert: "match bar {\n ${1:_} => {$0\\},\n}",
|
insert: "match bar {\n Ok(${1:_}) => {$2\\},\n Err(${3:_}) => {$0\\},\n}",
|
||||||
detail: "match expr {}",
|
detail: "match expr {}",
|
||||||
},
|
},
|
||||||
CompletionItem {
|
CompletionItem {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue