From 895238088417b292e35705e72182ff8cc3ab6f63 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Fri, 1 Nov 2019 20:01:21 +0100 Subject: [PATCH] Change SourceAnalyzer method resoltion API --- crates/ra_hir/src/lib.rs | 3 +- crates/ra_hir/src/source_binder.rs | 27 ++++++++++++++++-- .../ra_ide_api/src/completion/complete_dot.rs | 22 +++++---------- .../src/completion/complete_path.rs | 28 ++++++++----------- 4 files changed, 44 insertions(+), 36 deletions(-) diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs index 4cace432e3..40f5562b40 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs @@ -76,8 +76,7 @@ pub use crate::{ resolve::ScopeDef, source_binder::{PathResolution, ScopeEntryWithSyntax, SourceAnalyzer}, ty::{ - display::HirDisplay, method_resolution::LookupMode, ApplicationTy, CallableDef, Substs, - TraitRef, Ty, TypeCtor, TypeWalk, + display::HirDisplay, ApplicationTy, CallableDef, Substs, TraitRef, Ty, TypeCtor, TypeWalk, }, }; diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs index 82e6eb852c..a4ca59bba1 100644 --- a/crates/ra_hir/src/source_binder.rs +++ b/crates/ra_hir/src/source_binder.rs @@ -327,7 +327,30 @@ impl SourceAnalyzer { db: &impl HirDatabase, ty: Ty, name: Option<&Name>, - mode: method_resolution::LookupMode, + mut callback: impl FnMut(&Ty, Function) -> Option, + ) -> Option { + // There should be no inference vars in types passed here + // FIXME check that? + // FIXME replace Unknown by bound vars here + let canonical = crate::ty::Canonical { value: ty, num_vars: 0 }; + method_resolution::iterate_method_candidates( + &canonical, + db, + &self.resolver, + name, + method_resolution::LookupMode::MethodCall, + |ty, it| match it { + AssocItem::Function(f) => callback(ty, f), + _ => None, + }, + ) + } + + pub fn iterate_path_candidates( + &self, + db: &impl HirDatabase, + ty: Ty, + name: Option<&Name>, callback: impl FnMut(&Ty, AssocItem) -> Option, ) -> Option { // There should be no inference vars in types passed here @@ -339,7 +362,7 @@ impl SourceAnalyzer { db, &self.resolver, name, - mode, + method_resolution::LookupMode::Path, callback, ) } diff --git a/crates/ra_ide_api/src/completion/complete_dot.rs b/crates/ra_ide_api/src/completion/complete_dot.rs index fe32e7366f..b4df6ee2ad 100644 --- a/crates/ra_ide_api/src/completion/complete_dot.rs +++ b/crates/ra_ide_api/src/completion/complete_dot.rs @@ -58,21 +58,13 @@ 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, - 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::<()> - }, - ); + ctx.analyzer.iterate_method_candidates(ctx.db, receiver, None, |_ty, func| { + let data = func.data(ctx.db); + if data.has_self_param() && seen_methods.insert(data.name().clone()) { + acc.add_function(ctx, func); + } + 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 2aec8eb260..9ac9768afb 100644 --- a/crates/ra_ide_api/src/completion/complete_path.rs +++ b/crates/ra_ide_api/src/completion/complete_path.rs @@ -50,25 +50,19 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) { hir::ModuleDef::TypeAlias(a) => a.ty(ctx.db), _ => unreachable!(), }; - 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); - if !data.has_self_param() { - acc.add_function(ctx, func); - } + ctx.analyzer.iterate_path_candidates(ctx.db, ty.clone(), None, |_ty, item| { + match item { + hir::AssocItem::Function(func) => { + let data = func.data(ctx.db); + if !data.has_self_param() { + acc.add_function(ctx, func); } - hir::AssocItem::Const(ct) => acc.add_const(ctx, ct), - hir::AssocItem::TypeAlias(ty) => acc.add_type_alias(ctx, ty), } - None::<()> - }, - ); + hir::AssocItem::Const(ct) => acc.add_const(ctx, ct), + 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());