From 8b94bf7b2dd4743ce87d12b9892405cab48ac6da Mon Sep 17 00:00:00 2001 From: Lukas Tobias Wirth Date: Tue, 4 May 2021 21:03:35 +0200 Subject: Complete enum variants through type aliases --- .../src/completions/qualified_path.rs | 40 ++++++++++++++++++---- 1 file changed, 33 insertions(+), 7 deletions(-) (limited to 'crates/ide_completion') diff --git a/crates/ide_completion/src/completions/qualified_path.rs b/crates/ide_completion/src/completions/qualified_path.rs index d8f23d1eb..d2ebba65f 100644 --- a/crates/ide_completion/src/completions/qualified_path.rs +++ b/crates/ide_completion/src/completions/qualified_path.rs @@ -52,13 +52,17 @@ pub(crate) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionCon | PathResolution::Def(def @ hir::ModuleDef::TypeAlias(_)) | PathResolution::Def(def @ hir::ModuleDef::BuiltinType(_)) => { if let hir::ModuleDef::Adt(Adt::Enum(e)) = def { - for variant in e.variants(ctx.db) { - acc.add_enum_variant(ctx, variant, None); - } + add_enum_variants(ctx, acc, e); } let ty = match def { hir::ModuleDef::Adt(adt) => adt.ty(ctx.db), - hir::ModuleDef::TypeAlias(a) => a.ty(ctx.db), + hir::ModuleDef::TypeAlias(a) => { + let ty = a.ty(ctx.db); + if let Some(Adt::Enum(e)) = ty.as_adt() { + add_enum_variants(ctx, acc, e); + } + ty + } hir::ModuleDef::BuiltinType(builtin) => { let module = match ctx.scope.module() { Some(it) => it, @@ -122,9 +126,7 @@ pub(crate) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionCon }; if let Some(Adt::Enum(e)) = ty.as_adt() { - for variant in e.variants(ctx.db) { - acc.add_enum_variant(ctx, variant, None); - } + add_enum_variants(ctx, acc, e); } let traits_in_scope = ctx.scope.traits_in_scope(); @@ -151,6 +153,12 @@ pub(crate) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionCon } } +fn add_enum_variants(ctx: &CompletionContext, acc: &mut Completions, e: hir::Enum) { + for variant in e.variants(ctx.db) { + acc.add_enum_variant(ctx, variant, None); + } +} + #[cfg(test)] mod tests { use expect_test::{expect, Expect}; @@ -782,4 +790,22 @@ impl u8 { "#]], ); } + + #[test] + fn completes_through_alias() { + check( + r#" +enum Foo { + Bar +} +type Foo2 = Foo; +fn main() { + Foo2::$0 +} +"#, + expect![[r#" + ev Bar () + "#]], + ); + } } -- cgit v1.2.3