From 0d84adad353a09b8b29e862e80bf68da49c4481b Mon Sep 17 00:00:00 2001 From: Manmeet Date: Sun, 10 Jan 2021 20:12:17 +0530 Subject: Postfix like ifl now works with references --- crates/completion/src/completions/postfix.rs | 31 ++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/crates/completion/src/completions/postfix.rs b/crates/completion/src/completions/postfix.rs index 87f0c0b2a..ec91f87f2 100644 --- a/crates/completion/src/completions/postfix.rs +++ b/crates/completion/src/completions/postfix.rs @@ -30,11 +30,17 @@ pub(crate) fn complete_postfix(acc: &mut Completions, ctx: &CompletionContext) { let receiver_text = get_receiver_text(dot_receiver, ctx.dot_receiver_is_ambiguous_float_literal); - let receiver_ty = match ctx.sema.type_of_expr(&dot_receiver) { + let mut receiver_ty = match ctx.sema.type_of_expr(&dot_receiver) { Some(it) => it, None => return, }; + let mut ref_removed = false; + if let Some(removed) = receiver_ty.remove_ref() { + receiver_ty = removed; + ref_removed = true; + } + let cap = match ctx.config.snippet_cap { Some(it) => it, None => return, @@ -85,7 +91,7 @@ pub(crate) fn complete_postfix(acc: &mut Completions, ctx: &CompletionContext) { .add_to(acc); } } - } else if receiver_ty.is_bool() || receiver_ty.is_unknown() { + } else if !ref_removed && (receiver_ty.is_bool() || receiver_ty.is_unknown()) { postfix_snippet( ctx, cap, @@ -496,6 +502,27 @@ fn main() { fn postfix_completion_for_references() { check_edit("dbg", r#"fn main() { &&42.$0 }"#, r#"fn main() { dbg!(&&42) }"#); check_edit("refm", r#"fn main() { &&42.$0 }"#, r#"fn main() { &&&mut 42 }"#); + check_edit( + "ifl", + r#" +enum Option { Some(T), None } + +fn main() { + let bar = &Option::Some(true); + bar.$0 +} +"#, + r#" +enum Option { Some(T), None } + +fn main() { + let bar = &Option::Some(true); + if let Some($1) = bar { + $0 +} +} +"#, + ) } #[test] -- cgit v1.2.3 From 430dc5cd248c709049f5ea6d48170ae79d2a329f Mon Sep 17 00:00:00 2001 From: Manmeet Maan Date: Mon, 11 Jan 2021 11:32:55 +0530 Subject: Apply Suggestions from code review --- crates/completion/src/completions/postfix.rs | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/crates/completion/src/completions/postfix.rs b/crates/completion/src/completions/postfix.rs index ec91f87f2..b07e9325a 100644 --- a/crates/completion/src/completions/postfix.rs +++ b/crates/completion/src/completions/postfix.rs @@ -30,22 +30,19 @@ pub(crate) fn complete_postfix(acc: &mut Completions, ctx: &CompletionContext) { let receiver_text = get_receiver_text(dot_receiver, ctx.dot_receiver_is_ambiguous_float_literal); - let mut receiver_ty = match ctx.sema.type_of_expr(&dot_receiver) { + let receiver_ty = match ctx.sema.type_of_expr(&dot_receiver) { Some(it) => it, None => return, }; - let mut ref_removed = false; - if let Some(removed) = receiver_ty.remove_ref() { - receiver_ty = removed; - ref_removed = true; - } - + let ref_removed_ty = + std::iter::successors(Some(receiver_ty.clone()), |ty| ty.remove_ref()).last().unwrap(); + let cap = match ctx.config.snippet_cap { Some(it) => it, None => return, }; - let try_enum = TryEnum::from_ty(&ctx.sema, &receiver_ty); + let try_enum = TryEnum::from_ty(&ctx.sema, &ref_removed_ty); if let Some(try_enum) = &try_enum { match try_enum { TryEnum::Result => { @@ -91,7 +88,7 @@ pub(crate) fn complete_postfix(acc: &mut Completions, ctx: &CompletionContext) { .add_to(acc); } } - } else if !ref_removed && (receiver_ty.is_bool() || receiver_ty.is_unknown()) { + } else if receiver_ty.is_bool() || receiver_ty.is_unknown() { postfix_snippet( ctx, cap, -- cgit v1.2.3 From f05c14594a8e6bf56043be699035dc39799c027e Mon Sep 17 00:00:00 2001 From: Manmeet Maan Date: Mon, 11 Jan 2021 11:47:38 +0530 Subject: Fix formatting --- crates/completion/src/completions/postfix.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/completion/src/completions/postfix.rs b/crates/completion/src/completions/postfix.rs index b07e9325a..9c34ed0b6 100644 --- a/crates/completion/src/completions/postfix.rs +++ b/crates/completion/src/completions/postfix.rs @@ -37,7 +37,7 @@ pub(crate) fn complete_postfix(acc: &mut Completions, ctx: &CompletionContext) { let ref_removed_ty = std::iter::successors(Some(receiver_ty.clone()), |ty| ty.remove_ref()).last().unwrap(); - + let cap = match ctx.config.snippet_cap { Some(it) => it, None => return, -- cgit v1.2.3