aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorFlorian Diebold <[email protected]>2019-10-31 20:21:48 +0000
committerFlorian Diebold <[email protected]>2019-11-01 18:57:08 +0000
commit79cb0a0dab5fd8e3e84cf4a3b927ec29d2b6e65c (patch)
tree88f2ebdf338d230f6019d56acb96bea0878f7248 /crates
parent5da941897d4ed092a219729d7980bd103907850a (diff)
Complete trait assoc items
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_hir/src/lib.rs3
-rw-r--r--crates/ra_hir/src/source_binder.rs8
-rw-r--r--crates/ra_hir/src/ty/method_resolution.rs2
-rw-r--r--crates/ra_ide_api/src/completion/complete_dot.rs22
-rw-r--r--crates/ra_ide_api/src/completion/complete_path.rs38
5 files changed, 56 insertions, 17 deletions
diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs
index 40f5562b4..4cace432e 100644
--- a/crates/ra_hir/src/lib.rs
+++ b/crates/ra_hir/src/lib.rs
@@ -76,7 +76,8 @@ pub use crate::{
76 resolve::ScopeDef, 76 resolve::ScopeDef,
77 source_binder::{PathResolution, ScopeEntryWithSyntax, SourceAnalyzer}, 77 source_binder::{PathResolution, ScopeEntryWithSyntax, SourceAnalyzer},
78 ty::{ 78 ty::{
79 display::HirDisplay, ApplicationTy, CallableDef, Substs, TraitRef, Ty, TypeCtor, TypeWalk, 79 display::HirDisplay, method_resolution::LookupMode, ApplicationTy, CallableDef, Substs,
80 TraitRef, Ty, TypeCtor, TypeWalk,
80 }, 81 },
81}; 82};
82 83
diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs
index 0398806fd..82e6eb852 100644
--- a/crates/ra_hir/src/source_binder.rs
+++ b/crates/ra_hir/src/source_binder.rs
@@ -27,7 +27,7 @@ use crate::{
27 }, 27 },
28 ids::LocationCtx, 28 ids::LocationCtx,
29 resolve::{ScopeDef, TypeNs, ValueNs}, 29 resolve::{ScopeDef, TypeNs, ValueNs},
30 ty::method_resolution::implements_trait, 30 ty::method_resolution::{self, implements_trait},
31 AssocItem, Const, DefWithBody, Either, Enum, FromSource, Function, HasBody, HirFileId, 31 AssocItem, Const, DefWithBody, Either, Enum, FromSource, Function, HasBody, HirFileId,
32 MacroDef, Module, Name, Path, Resolver, Static, Struct, Ty, 32 MacroDef, Module, Name, Path, Resolver, Static, Struct, Ty,
33}; 33};
@@ -327,17 +327,19 @@ impl SourceAnalyzer {
327 db: &impl HirDatabase, 327 db: &impl HirDatabase,
328 ty: Ty, 328 ty: Ty,
329 name: Option<&Name>, 329 name: Option<&Name>,
330 mode: method_resolution::LookupMode,
330 callback: impl FnMut(&Ty, AssocItem) -> Option<T>, 331 callback: impl FnMut(&Ty, AssocItem) -> Option<T>,
331 ) -> Option<T> { 332 ) -> Option<T> {
332 // There should be no inference vars in types passed here 333 // There should be no inference vars in types passed here
333 // FIXME check that? 334 // FIXME check that?
335 // FIXME replace Unknown by bound vars here
334 let canonical = crate::ty::Canonical { value: ty, num_vars: 0 }; 336 let canonical = crate::ty::Canonical { value: ty, num_vars: 0 };
335 crate::ty::method_resolution::iterate_method_candidates( 337 method_resolution::iterate_method_candidates(
336 &canonical, 338 &canonical,
337 db, 339 db,
338 &self.resolver, 340 &self.resolver,
339 name, 341 name,
340 crate::ty::method_resolution::LookupMode::MethodCall, 342 mode,
341 callback, 343 callback,
342 ) 344 )
343 } 345 }
diff --git a/crates/ra_hir/src/ty/method_resolution.rs b/crates/ra_hir/src/ty/method_resolution.rs
index 43b485ec0..9caff422f 100644
--- a/crates/ra_hir/src/ty/method_resolution.rs
+++ b/crates/ra_hir/src/ty/method_resolution.rs
@@ -176,7 +176,7 @@ pub(crate) fn lookup_method(
176} 176}
177 177
178#[derive(Copy, Clone, Debug, PartialEq, Eq)] 178#[derive(Copy, Clone, Debug, PartialEq, Eq)]
179pub(crate) enum LookupMode { 179pub enum LookupMode {
180 MethodCall, 180 MethodCall,
181 Path, 181 Path,
182} 182}
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)
58 58
59fn complete_methods(acc: &mut Completions, ctx: &CompletionContext, receiver: Ty) { 59fn complete_methods(acc: &mut Completions, ctx: &CompletionContext, receiver: Ty) {
60 let mut seen_methods = FxHashSet::default(); 60 let mut seen_methods = FxHashSet::default();
61 ctx.analyzer.iterate_method_candidates(ctx.db, receiver, None, |_ty, item| { 61 ctx.analyzer.iterate_method_candidates(
62 if let hir::AssocItem::Function(func) = item { 62 ctx.db,
63 let data = func.data(ctx.db); 63 receiver,
64 if data.has_self_param() && seen_methods.insert(data.name().clone()) { 64 None,
65 acc.add_function(ctx, func); 65 hir::LookupMode::MethodCall,
66 |_ty, item| {
67 if let hir::AssocItem::Function(func) = item {
68 let data = func.data(ctx.db);
69 if data.has_self_param() && seen_methods.insert(data.name().clone()) {
70 acc.add_function(ctx, func);
71 }
66 } 72 }
67 } 73 None::<()>
68 None::<()> 74 },
69 }); 75 );
70} 76}
71 77
72#[cfg(test)] 78#[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) {
50 hir::ModuleDef::TypeAlias(a) => a.ty(ctx.db), 50 hir::ModuleDef::TypeAlias(a) => a.ty(ctx.db),
51 _ => unreachable!(), 51 _ => unreachable!(),
52 }; 52 };
53 let krate = ctx.module.map(|m| m.krate()); 53 ctx.analyzer.iterate_method_candidates(
54 if let Some(krate) = krate { 54 ctx.db,
55 ty.iterate_impl_items(ctx.db, krate, |item| { 55 ty.clone(),
56 None,
57 hir::LookupMode::Path,
58 |_ty, item| {
56 match item { 59 match item {
57 hir::AssocItem::Function(func) => { 60 hir::AssocItem::Function(func) => {
58 let data = func.data(ctx.db); 61 let data = func.data(ctx.db);
@@ -64,6 +67,18 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) {
64 hir::AssocItem::TypeAlias(ty) => acc.add_type_alias(ctx, ty), 67 hir::AssocItem::TypeAlias(ty) => acc.add_type_alias(ctx, ty),
65 } 68 }
66 None::<()> 69 None::<()>
70 },
71 );
72 // Iterate assoc types separately
73 // FIXME: complete T::AssocType
74 let krate = ctx.module.map(|m| m.krate());
75 if let Some(krate) = krate {
76 ty.iterate_impl_items(ctx.db, krate, |item| {
77 match item {
78 hir::AssocItem::Function(_) | hir::AssocItem::Const(_) => {}
79 hir::AssocItem::TypeAlias(ty) => acc.add_type_alias(ctx, ty),
80 }
81 None::<()>
67 }); 82 });
68 } 83 }
69 } 84 }
@@ -593,7 +608,22 @@ mod tests {
593 fn foo() { let _ = S::<|> } 608 fn foo() { let _ = S::<|> }
594 " 609 "
595 ), 610 ),
596 @"[]" 611 @r###"
612 [
613 CompletionItem {
614 label: "m()",
615 source_range: [99; 99),
616 delete: [99; 99),
617 insert: "m()$0",
618 kind: Function,
619 lookup: "m",
620 detail: "fn m()",
621 documentation: Documentation(
622 "A trait method",
623 ),
624 },
625 ]
626 "###
597 ); 627 );
598 } 628 }
599 629