From 6203e9c4faee288f16d93dbb7dd0f1f8df487d83 Mon Sep 17 00:00:00 2001 From: Benjamin Coenen <5719034+bnjjj@users.noreply.github.com> Date: Thu, 7 May 2020 11:23:38 +0200 Subject: add if let and while let postfix for Option and Result #4348 Signed-off-by: Benjamin Coenen <5719034+bnjjj@users.noreply.github.com> --- crates/ra_ide/src/completion/complete_postfix.rs | 200 ++++++++++++++++++++++- 1 file changed, 199 insertions(+), 1 deletion(-) (limited to 'crates/ra_ide/src/completion') diff --git a/crates/ra_ide/src/completion/complete_postfix.rs b/crates/ra_ide/src/completion/complete_postfix.rs index 6a0f0c72e..dc32bbee2 100644 --- a/crates/ra_ide/src/completion/complete_postfix.rs +++ b/crates/ra_ide/src/completion/complete_postfix.rs @@ -38,7 +38,47 @@ pub(super) fn complete_postfix(acc: &mut Completions, ctx: &CompletionContext) { None => return, }; - if receiver_ty.is_bool() || receiver_ty.is_unknown() { + if receiver_ty.is_option(ctx.db) { + postfix_snippet( + ctx, + cap, + &dot_receiver, + "ifl", + "if let Some {}", + &format!("if let Some($1) = {} {{\n $0\n}}", receiver_text), + ) + .add_to(acc); + + postfix_snippet( + ctx, + cap, + &dot_receiver, + "while", + "while let Some {}", + &format!("while let Some($1) = {} {{\n $0\n}}", receiver_text), + ) + .add_to(acc); + } else if receiver_ty.is_result(ctx.db) { + postfix_snippet( + ctx, + cap, + &dot_receiver, + "ifl", + "if let Ok {}", + &format!("if let Ok($1) = {} {{\n $0\n}}", receiver_text), + ) + .add_to(acc); + + postfix_snippet( + ctx, + cap, + &dot_receiver, + "while", + "while let Ok {}", + &format!("while let Ok($1) = {} {{\n $0\n}}", receiver_text), + ) + .add_to(acc); + } else if receiver_ty.is_bool() || receiver_ty.is_unknown() { postfix_snippet( ctx, cap, @@ -235,6 +275,164 @@ mod tests { ); } + #[test] + fn postfix_completion_works_for_option() { + assert_debug_snapshot!( + do_postfix_completion( + r#" + enum Option { + Some(T), + None, + } + + fn main() { + let bar = Option::Some(true); + bar.<|> + } + "#, + ), + @r###" + [ + CompletionItem { + label: "box", + source_range: 210..210, + delete: 206..210, + insert: "Box::new(bar)", + detail: "Box::new(expr)", + }, + CompletionItem { + label: "dbg", + source_range: 210..210, + delete: 206..210, + insert: "dbg!(bar)", + detail: "dbg!(expr)", + }, + CompletionItem { + label: "ifl", + source_range: 210..210, + delete: 206..210, + insert: "if let Some($1) = bar {\n $0\n}", + detail: "if let Some {}", + }, + CompletionItem { + label: "match", + source_range: 210..210, + delete: 206..210, + insert: "match bar {\n ${1:_} => {$0\\},\n}", + detail: "match expr {}", + }, + CompletionItem { + label: "not", + source_range: 210..210, + delete: 206..210, + insert: "!bar", + detail: "!expr", + }, + CompletionItem { + label: "ref", + source_range: 210..210, + delete: 206..210, + insert: "&bar", + detail: "&expr", + }, + CompletionItem { + label: "refm", + source_range: 210..210, + delete: 206..210, + insert: "&mut bar", + detail: "&mut expr", + }, + CompletionItem { + label: "while", + source_range: 210..210, + delete: 206..210, + insert: "while let Some($1) = bar {\n $0\n}", + detail: "while let Some {}", + }, + ] + "### + ); + } + + #[test] + fn postfix_completion_works_for_result() { + assert_debug_snapshot!( + do_postfix_completion( + r#" + enum Result { + Ok(T), + Err(E), + } + + fn main() { + let bar = Result::Ok(true); + bar.<|> + } + "#, + ), + @r###" + [ + CompletionItem { + label: "box", + source_range: 211..211, + delete: 207..211, + insert: "Box::new(bar)", + detail: "Box::new(expr)", + }, + CompletionItem { + label: "dbg", + source_range: 211..211, + delete: 207..211, + insert: "dbg!(bar)", + detail: "dbg!(expr)", + }, + CompletionItem { + label: "ifl", + source_range: 211..211, + delete: 207..211, + insert: "if let Ok($1) = bar {\n $0\n}", + detail: "if let Ok {}", + }, + CompletionItem { + label: "match", + source_range: 211..211, + delete: 207..211, + insert: "match bar {\n ${1:_} => {$0\\},\n}", + detail: "match expr {}", + }, + CompletionItem { + label: "not", + source_range: 211..211, + delete: 207..211, + insert: "!bar", + detail: "!expr", + }, + CompletionItem { + label: "ref", + source_range: 211..211, + delete: 207..211, + insert: "&bar", + detail: "&expr", + }, + CompletionItem { + label: "refm", + source_range: 211..211, + delete: 207..211, + insert: "&mut bar", + detail: "&mut expr", + }, + CompletionItem { + label: "while", + source_range: 211..211, + delete: 207..211, + insert: "while let Ok($1) = bar {\n $0\n}", + detail: "while let Ok {}", + }, + ] + "### + ); + } + #[test] fn some_postfix_completions_ignored() { assert_debug_snapshot!( -- cgit v1.2.3 From 92b2230fefe61322dbca8194c8721f848c5d1c2f Mon Sep 17 00:00:00 2001 From: Benjamin Coenen <5719034+bnjjj@users.noreply.github.com> Date: Sun, 10 May 2020 12:45:35 +0200 Subject: add if let and while let postfix for Option and Result Signed-off-by: Benjamin Coenen <5719034+bnjjj@users.noreply.github.com> --- crates/ra_ide/src/completion/complete_postfix.rs | 82 +++++++++++++----------- 1 file changed, 44 insertions(+), 38 deletions(-) (limited to 'crates/ra_ide/src/completion') diff --git a/crates/ra_ide/src/completion/complete_postfix.rs b/crates/ra_ide/src/completion/complete_postfix.rs index dc32bbee2..c5c4426cc 100644 --- a/crates/ra_ide/src/completion/complete_postfix.rs +++ b/crates/ra_ide/src/completion/complete_postfix.rs @@ -14,6 +14,7 @@ use crate::{ }, CompletionItem, }; +use ra_assists::utils::TryEnum; pub(super) fn complete_postfix(acc: &mut Completions, ctx: &CompletionContext) { if !ctx.config.enable_postfix_completions { @@ -38,46 +39,51 @@ pub(super) fn complete_postfix(acc: &mut Completions, ctx: &CompletionContext) { None => return, }; - if receiver_ty.is_option(ctx.db) { - postfix_snippet( - ctx, - cap, - &dot_receiver, - "ifl", - "if let Some {}", - &format!("if let Some($1) = {} {{\n $0\n}}", receiver_text), - ) - .add_to(acc); + if let Some(try_enum) = TryEnum::from_ty(&ctx.sema, &receiver_ty) { + match try_enum { + TryEnum::Result => { + postfix_snippet( + ctx, + cap, + &dot_receiver, + "ifl", + "if let Ok {}", + &format!("if let Ok($1) = {} {{\n $0\n}}", receiver_text), + ) + .add_to(acc); - postfix_snippet( - ctx, - cap, - &dot_receiver, - "while", - "while let Some {}", - &format!("while let Some($1) = {} {{\n $0\n}}", receiver_text), - ) - .add_to(acc); - } else if receiver_ty.is_result(ctx.db) { - postfix_snippet( - ctx, - cap, - &dot_receiver, - "ifl", - "if let Ok {}", - &format!("if let Ok($1) = {} {{\n $0\n}}", receiver_text), - ) - .add_to(acc); + postfix_snippet( + ctx, + cap, + &dot_receiver, + "while", + "while let Ok {}", + &format!("while let Ok($1) = {} {{\n $0\n}}", receiver_text), + ) + .add_to(acc); + } + TryEnum::Option => { + postfix_snippet( + ctx, + cap, + &dot_receiver, + "ifl", + "if let Some {}", + &format!("if let Some($1) = {} {{\n $0\n}}", receiver_text), + ) + .add_to(acc); - postfix_snippet( - ctx, - cap, - &dot_receiver, - "while", - "while let Ok {}", - &format!("while let Ok($1) = {} {{\n $0\n}}", receiver_text), - ) - .add_to(acc); + postfix_snippet( + ctx, + cap, + &dot_receiver, + "while", + "while let Some {}", + &format!("while let Some($1) = {} {{\n $0\n}}", receiver_text), + ) + .add_to(acc); + } + } } else if receiver_ty.is_bool() || receiver_ty.is_unknown() { postfix_snippet( ctx, -- cgit v1.2.3