From ec9ef9c28325ca8d2c520d4316e2d82281c0cbf1 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Fri, 11 Jun 2021 23:12:30 +0200 Subject: Complete associated types in dyn and impl trait --- .../src/completions/unqualified_path.rs | 34 +++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) (limited to 'crates/ide_completion/src/completions/unqualified_path.rs') diff --git a/crates/ide_completion/src/completions/unqualified_path.rs b/crates/ide_completion/src/completions/unqualified_path.rs index b1e6b2b77..952f052a1 100644 --- a/crates/ide_completion/src/completions/unqualified_path.rs +++ b/crates/ide_completion/src/completions/unqualified_path.rs @@ -1,8 +1,9 @@ //! Completion of names from the current scope, e.g. locals and imported items. use hir::ScopeDef; +use syntax::{ast, AstNode}; -use crate::{CompletionContext, Completions}; +use crate::{patterns::ImmediateLocation, CompletionContext, Completions}; pub(crate) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionContext) { if ctx.is_path_disallowed() || !ctx.is_trivial_path() { @@ -43,6 +44,20 @@ pub(crate) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionC }); } + if let Some(ImmediateLocation::GenericArgList(arg_list)) = &ctx.completion_location { + if let Some(path_seg) = arg_list.syntax().parent().and_then(ast::PathSegment::cast) { + if let Some(hir::PathResolution::Def(hir::ModuleDef::Trait(trait_))) = + ctx.sema.resolve_path(&path_seg.parent_path()) + { + trait_.items(ctx.sema.db).into_iter().for_each(|it| { + if let hir::AssocItem::TypeAlias(alias) = it { + acc.add_type_alias_with_eq(ctx, alias) + } + }); + } + } + } + ctx.scope.process_all_names(&mut |name, res| { if let ScopeDef::GenericParam(hir::GenericParam::LifetimeParam(_)) = res { cov_mark::hit!(skip_lifetime_completion); @@ -777,4 +792,21 @@ $0 "#]], ) } + + #[test] + fn completes_assoc_types_in_dynimpl_trait() { + check( + r#" +trait Foo { + type Bar; +} + +fn foo(_: impl Foo) {} +"#, + expect![[r#" + ta Bar = type Bar; + tt Foo + "#]], + ); + } } -- cgit v1.2.3