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') 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