From d37f960dfaac2272de3065ad586b25dc1cdb7dbd Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sat, 29 Jun 2019 12:40:01 +0200 Subject: Complete associated methods on enums (and unions) as well --- crates/ra_ide_api/src/completion/complete_path.rs | 55 ++++++++++++++++++++--- 1 file changed, 49 insertions(+), 6 deletions(-) (limited to 'crates/ra_ide_api/src/completion/complete_path.rs') diff --git a/crates/ra_ide_api/src/completion/complete_path.rs b/crates/ra_ide_api/src/completion/complete_path.rs index c14af593b..b42b7c458 100644 --- a/crates/ra_ide_api/src/completion/complete_path.rs +++ b/crates/ra_ide_api/src/completion/complete_path.rs @@ -37,13 +37,18 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) { acc.add_resolution(ctx, name.to_string(), &res.def.map(hir::Resolution::Def)); } } - hir::ModuleDef::Enum(e) => { - for variant in e.variants(ctx.db) { - acc.add_enum_variant(ctx, variant); + hir::ModuleDef::Enum(_) | hir::ModuleDef::Struct(_) | hir::ModuleDef::Union(_) => { + if let hir::ModuleDef::Enum(e) = def { + for variant in e.variants(ctx.db) { + acc.add_enum_variant(ctx, variant); + } } - } - hir::ModuleDef::Struct(s) => { - let ty = s.ty(ctx.db); + let ty = match def { + hir::ModuleDef::Enum(e) => e.ty(ctx.db), + hir::ModuleDef::Struct(s) => s.ty(ctx.db), + hir::ModuleDef::Union(u) => u.ty(ctx.db), + _ => unreachable!(), + }; let krate = ctx.module.and_then(|m| m.krate(ctx.db)); if let Some(krate) = krate { ty.iterate_impl_items(ctx.db, krate, |item| { @@ -280,6 +285,44 @@ mod tests { ); } + #[test] + fn completes_enum_associated_method() { + check_reference_completion( + "enum_associated_method", + " + //- /lib.rs + /// An enum + enum S {}; + + impl S { + /// An associated method + fn m() { } + } + + fn foo() { let _ = S::<|> } + ", + ); + } + + #[test] + fn completes_union_associated_method() { + check_reference_completion( + "union_associated_method", + " + //- /lib.rs + /// A union + union U {}; + + impl U { + /// An associated method + fn m() { } + } + + fn foo() { let _ = U::<|> } + ", + ); + } + #[test] fn completes_use_paths_across_crates() { check_reference_completion( -- cgit v1.2.3