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_assists/src/utils.rs | 4 +- crates/ra_hir/src/code_model.rs | 22 ------- crates/ra_ide/src/completion/complete_postfix.rs | 82 +++++++++++++----------- 3 files changed, 46 insertions(+), 62 deletions(-) (limited to 'crates') diff --git a/crates/ra_assists/src/utils.rs b/crates/ra_assists/src/utils.rs index 2f15a3f15..f3fc92ebf 100644 --- a/crates/ra_assists/src/utils.rs +++ b/crates/ra_assists/src/utils.rs @@ -103,7 +103,7 @@ fn invert_special_case(expr: &ast::Expr) -> Option { } #[derive(Clone, Copy)] -pub(crate) enum TryEnum { +pub enum TryEnum { Result, Option, } @@ -111,7 +111,7 @@ pub(crate) enum TryEnum { impl TryEnum { const ALL: [TryEnum; 2] = [TryEnum::Option, TryEnum::Result]; - pub(crate) fn from_ty(sema: &Semantics, ty: &Type) -> Option { + pub fn from_ty(sema: &Semantics, ty: &Type) -> Option { let enum_ = match ty.as_adt() { Some(Adt::Enum(it)) => it, _ => return None, diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 6d8444462..be18c845c 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -1086,28 +1086,6 @@ impl Type { matches!(self.ty.value, Ty::Apply(ApplicationTy { ctor: TypeCtor::Bool, .. })) } - pub fn is_option(&self, db: &dyn HirDatabase) -> bool { - if let Some(adt_ty) = self.as_adt() { - if let Adt::Enum(_) = adt_ty { - if self.display(db).to_string().starts_with("Option<") { - return true; - } - } - } - false - } - - pub fn is_result(&self, db: &dyn HirDatabase) -> bool { - if let Some(adt_ty) = self.as_adt() { - if let Adt::Enum(_) = adt_ty { - if self.display(db).to_string().starts_with("Result<") { - return true; - } - } - } - false - } - pub fn is_mutable_reference(&self) -> bool { matches!( self.ty.value, 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