From 3f796fea9fab0222913f9f895af78b80c4c7660b Mon Sep 17 00:00:00 2001 From: Lukas Tobias Wirth Date: Wed, 5 May 2021 22:55:12 +0200 Subject: simplify --- crates/hir/src/lib.rs | 4 ++ crates/ide/src/inlay_hints.rs | 4 +- crates/ide_completion/src/completions.rs | 56 ++++++++++------------ crates/ide_completion/src/completions/pattern.rs | 6 ++- crates/ide_completion/src/completions/postfix.rs | 5 +- .../src/completions/unqualified_path.rs | 6 ++- 6 files changed, 40 insertions(+), 41 deletions(-) diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs index d8ccfde0c..6fcc58f61 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs @@ -1744,6 +1744,10 @@ impl Type { } } + pub fn strip_references(&self) -> Type { + self.derived(self.ty.strip_references().clone()) + } + pub fn is_unknown(&self) -> bool { self.ty.is_unknown() } diff --git a/crates/ide/src/inlay_hints.rs b/crates/ide/src/inlay_hints.rs index d5ef054d8..e0bf660c4 100644 --- a/crates/ide/src/inlay_hints.rs +++ b/crates/ide/src/inlay_hints.rs @@ -218,9 +218,7 @@ fn hint_iterator( ty: &hir::Type, ) -> Option { let db = sema.db; - let strukt = std::iter::successors(Some(ty.clone()), |ty| ty.remove_ref()) - .last() - .and_then(|strukt| strukt.as_adt())?; + let strukt = ty.strip_references().as_adt()?; let krate = strukt.krate(db); if krate != famous_defs.core()? { return None; diff --git a/crates/ide_completion/src/completions.rs b/crates/ide_completion/src/completions.rs index e2994eed4..78154bf3e 100644 --- a/crates/ide_completion/src/completions.rs +++ b/crates/ide_completion/src/completions.rs @@ -203,41 +203,37 @@ impl Completions { fn complete_enum_variants( acc: &mut Completions, ctx: &CompletionContext, - ty: &hir::Type, + enum_data: hir::Enum, cb: impl Fn(&mut Completions, &CompletionContext, hir::Variant, hir::ModPath), ) { - if let Some(hir::Adt::Enum(enum_data)) = - iter::successors(Some(ty.clone()), |ty| ty.remove_ref()).last().and_then(|ty| ty.as_adt()) - { - let variants = enum_data.variants(ctx.db); - - let module = if let Some(module) = ctx.scope.module() { - // Compute path from the completion site if available. - module - } else { - // Otherwise fall back to the enum's definition site. - enum_data.module(ctx.db) - }; - - if let Some(impl_) = ctx.impl_def.as_ref().and_then(|impl_| ctx.sema.to_def(impl_)) { - if impl_.self_ty(ctx.db) == *ty { - for &variant in &variants { - let self_path = hir::ModPath::from_segments( - hir::PathKind::Plain, - iter::once(known::SELF_TYPE).chain(iter::once(variant.name(ctx.db))), - ); - cb(acc, ctx, variant, self_path); - } + let variants = enum_data.variants(ctx.db); + + let module = if let Some(module) = ctx.scope.module() { + // Compute path from the completion site if available. + module + } else { + // Otherwise fall back to the enum's definition site. + enum_data.module(ctx.db) + }; + + if let Some(impl_) = ctx.impl_def.as_ref().and_then(|impl_| ctx.sema.to_def(impl_)) { + if impl_.self_ty(ctx.db).as_adt() == Some(hir::Adt::Enum(enum_data)) { + for &variant in &variants { + let self_path = hir::ModPath::from_segments( + hir::PathKind::Plain, + iter::once(known::SELF_TYPE).chain(iter::once(variant.name(ctx.db))), + ); + cb(acc, ctx, variant, self_path); } } + } - for variant in variants { - if let Some(path) = module.find_use_path(ctx.db, hir::ModuleDef::from(variant)) { - // Variants with trivial paths are already added by the existing completion logic, - // so we should avoid adding these twice - if path.segments().len() > 1 { - cb(acc, ctx, variant, path); - } + for variant in variants { + if let Some(path) = module.find_use_path(ctx.db, hir::ModuleDef::from(variant)) { + // Variants with trivial paths are already added by the existing completion logic, + // so we should avoid adding these twice + if path.segments().len() > 1 { + cb(acc, ctx, variant, path); } } } diff --git a/crates/ide_completion/src/completions/pattern.rs b/crates/ide_completion/src/completions/pattern.rs index 808d7ff7e..8dc9ab73c 100644 --- a/crates/ide_completion/src/completions/pattern.rs +++ b/crates/ide_completion/src/completions/pattern.rs @@ -12,8 +12,10 @@ pub(crate) fn complete_pattern(acc: &mut Completions, ctx: &CompletionContext) { } if !ctx.is_irrefutable_pat_binding { - if let Some(ty) = ctx.expected_type.as_ref() { - super::complete_enum_variants(acc, ctx, ty, |acc, ctx, variant, path| { + if let Some(hir::Adt::Enum(e)) = + ctx.expected_type.as_ref().and_then(|ty| ty.strip_references().as_adt()) + { + super::complete_enum_variants(acc, ctx, e, |acc, ctx, variant, path| { acc.add_qualified_variant_pat(ctx, variant, path.clone()); acc.add_qualified_enum_variant(ctx, variant, path); }); diff --git a/crates/ide_completion/src/completions/postfix.rs b/crates/ide_completion/src/completions/postfix.rs index ac69b720a..962aaf0df 100644 --- a/crates/ide_completion/src/completions/postfix.rs +++ b/crates/ide_completion/src/completions/postfix.rs @@ -35,14 +35,11 @@ pub(crate) fn complete_postfix(acc: &mut Completions, ctx: &CompletionContext) { None => return, }; - 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, &ref_removed_ty); + let try_enum = TryEnum::from_ty(&ctx.sema, &receiver_ty.strip_references()); if let Some(try_enum) = &try_enum { match try_enum { TryEnum::Result => { diff --git a/crates/ide_completion/src/completions/unqualified_path.rs b/crates/ide_completion/src/completions/unqualified_path.rs index 1b8b063e7..7875500c1 100644 --- a/crates/ide_completion/src/completions/unqualified_path.rs +++ b/crates/ide_completion/src/completions/unqualified_path.rs @@ -17,8 +17,10 @@ pub(crate) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionC return; } - if let Some(ty) = &ctx.expected_type { - super::complete_enum_variants(acc, ctx, ty, |acc, ctx, variant, path| { + if let Some(hir::Adt::Enum(e)) = + ctx.expected_type.as_ref().and_then(|ty| ty.strip_references().as_adt()) + { + super::complete_enum_variants(acc, ctx, e, |acc, ctx, variant, path| { acc.add_qualified_enum_variant(ctx, variant, path) }); } -- cgit v1.2.3