From 79cb0a0dab5fd8e3e84cf4a3b927ec29d2b6e65c Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Thu, 31 Oct 2019 21:21:48 +0100 Subject: Complete trait assoc items --- crates/ra_ide_api/src/completion/complete_dot.rs | 22 ++++++++----- crates/ra_ide_api/src/completion/complete_path.rs | 38 ++++++++++++++++++++--- 2 files changed, 48 insertions(+), 12 deletions(-) (limited to 'crates/ra_ide_api/src/completion') diff --git a/crates/ra_ide_api/src/completion/complete_dot.rs b/crates/ra_ide_api/src/completion/complete_dot.rs index 7135f481d..fe32e7366 100644 --- a/crates/ra_ide_api/src/completion/complete_dot.rs +++ b/crates/ra_ide_api/src/completion/complete_dot.rs @@ -58,15 +58,21 @@ fn complete_fields(acc: &mut Completions, ctx: &CompletionContext, receiver: Ty) fn complete_methods(acc: &mut Completions, ctx: &CompletionContext, receiver: Ty) { let mut seen_methods = FxHashSet::default(); - ctx.analyzer.iterate_method_candidates(ctx.db, receiver, None, |_ty, item| { - if let hir::AssocItem::Function(func) = item { - let data = func.data(ctx.db); - if data.has_self_param() && seen_methods.insert(data.name().clone()) { - acc.add_function(ctx, func); + ctx.analyzer.iterate_method_candidates( + ctx.db, + receiver, + None, + hir::LookupMode::MethodCall, + |_ty, item| { + if let hir::AssocItem::Function(func) = item { + let data = func.data(ctx.db); + if data.has_self_param() && seen_methods.insert(data.name().clone()) { + acc.add_function(ctx, func); + } } - } - None::<()> - }); + None::<()> + }, + ); } #[cfg(test)] diff --git a/crates/ra_ide_api/src/completion/complete_path.rs b/crates/ra_ide_api/src/completion/complete_path.rs index b471787eb..940858342 100644 --- a/crates/ra_ide_api/src/completion/complete_path.rs +++ b/crates/ra_ide_api/src/completion/complete_path.rs @@ -50,9 +50,12 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) { hir::ModuleDef::TypeAlias(a) => a.ty(ctx.db), _ => unreachable!(), }; - let krate = ctx.module.map(|m| m.krate()); - if let Some(krate) = krate { - ty.iterate_impl_items(ctx.db, krate, |item| { + ctx.analyzer.iterate_method_candidates( + ctx.db, + ty.clone(), + None, + hir::LookupMode::Path, + |_ty, item| { match item { hir::AssocItem::Function(func) => { let data = func.data(ctx.db); @@ -64,6 +67,18 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) { hir::AssocItem::TypeAlias(ty) => acc.add_type_alias(ctx, ty), } None::<()> + }, + ); + // Iterate assoc types separately + // FIXME: complete T::AssocType + let krate = ctx.module.map(|m| m.krate()); + if let Some(krate) = krate { + ty.iterate_impl_items(ctx.db, krate, |item| { + match item { + hir::AssocItem::Function(_) | hir::AssocItem::Const(_) => {} + hir::AssocItem::TypeAlias(ty) => acc.add_type_alias(ctx, ty), + } + None::<()> }); } } @@ -593,7 +608,22 @@ mod tests { fn foo() { let _ = S::<|> } " ), - @"[]" + @r###" + [ + CompletionItem { + label: "m()", + source_range: [99; 99), + delete: [99; 99), + insert: "m()$0", + kind: Function, + lookup: "m", + detail: "fn m()", + documentation: Documentation( + "A trait method", + ), + }, + ] + "### ); } -- cgit v1.2.3