From 8c2670026a4c864a67a06bab654e203ed068f021 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Tue, 28 Apr 2020 00:40:32 +0200 Subject: Complete assoc. items on type parameters --- .../src/completion/complete_qualified_path.rs | 273 ++++++++++++++++++++- 1 file changed, 262 insertions(+), 11 deletions(-) (limited to 'crates/ra_ide') diff --git a/crates/ra_ide/src/completion/complete_qualified_path.rs b/crates/ra_ide/src/completion/complete_qualified_path.rs index dd10f74e6..69e789a49 100644 --- a/crates/ra_ide/src/completion/complete_qualified_path.rs +++ b/crates/ra_ide/src/completion/complete_qualified_path.rs @@ -5,19 +5,30 @@ use ra_syntax::AstNode; use test_utils::tested_by; use crate::completion::{CompletionContext, Completions}; +use rustc_hash::FxHashSet; pub(super) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionContext) { let path = match &ctx.path_prefix { Some(path) => path.clone(), _ => return, }; - let def = match ctx.scope().resolve_hir_path(&path) { - Some(PathResolution::Def(def)) => def, - _ => return, + let scope = ctx.scope(); + let context_module = scope.module(); + + let res = if let Some(res) = scope.resolve_hir_path(&path) { + res + } else { + return; }; - let context_module = ctx.scope().module(); - match def { - hir::ModuleDef::Module(module) => { + + // Add associated types on type parameters and `Self`. + res.assoc_type_shorthand_candidates(ctx.db, |alias| { + acc.add_type_alias(ctx, alias); + None::<()> + }); + + match res { + PathResolution::Def(hir::ModuleDef::Module(module)) => { let module_scope = module.scope(ctx.db, context_module); for (name, def) in module_scope { if ctx.use_item_syntax.is_some() { @@ -35,7 +46,8 @@ pub(super) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionCon acc.add_resolution(ctx, name.to_string(), &def); } } - hir::ModuleDef::Adt(_) | hir::ModuleDef::TypeAlias(_) => { + PathResolution::Def(def @ hir::ModuleDef::Adt(_)) + | PathResolution::Def(def @ hir::ModuleDef::TypeAlias(_)) => { if let hir::ModuleDef::Adt(Adt::Enum(e)) = def { for variant in e.variants(ctx.db) { acc.add_enum_variant(ctx, variant, None); @@ -46,8 +58,10 @@ pub(super) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionCon hir::ModuleDef::TypeAlias(a) => a.ty(ctx.db), _ => unreachable!(), }; - // Iterate assoc types separately - // FIXME: complete T::AssocType + + // XXX: For parity with Rust bug #22519, this does not complete Ty::AssocType. + // (where AssocType is defined on a trait, not an inherent impl) + let krate = ctx.krate; if let Some(krate) = krate { let traits_in_scope = ctx.scope().traits_in_scope(); @@ -65,6 +79,7 @@ pub(super) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionCon None::<()> }); + // Iterate assoc types separately ty.iterate_impl_items(ctx.db, krate, |item| { if context_module.map_or(false, |m| !item.is_visible_from(ctx.db, m)) { return None; @@ -77,7 +92,8 @@ pub(super) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionCon }); } } - hir::ModuleDef::Trait(t) => { + PathResolution::Def(hir::ModuleDef::Trait(t)) => { + // Handles `Trait::assoc` as well as `::assoc`. for item in t.items(ctx.db) { if context_module.map_or(false, |m| !item.is_visible_from(ctx.db, m)) { continue; @@ -91,8 +107,38 @@ pub(super) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionCon } } } + PathResolution::TypeParam(_) | PathResolution::SelfType(_) => { + if let Some(krate) = ctx.krate { + let ty = match res { + PathResolution::TypeParam(param) => param.ty(ctx.db), + PathResolution::SelfType(impl_def) => impl_def.target_ty(ctx.db), + _ => return, + }; + + let traits_in_scope = ctx.scope().traits_in_scope(); + let mut seen = FxHashSet::default(); + ty.iterate_path_candidates(ctx.db, krate, &traits_in_scope, None, |_ty, item| { + if context_module.map_or(false, |m| !item.is_visible_from(ctx.db, m)) { + return None; + } + + // We might iterate candidates of a trait multiple times here, so deduplicate + // them. + if seen.insert(item) { + match item { + hir::AssocItem::Function(func) => { + acc.add_function(ctx, func, None); + } + hir::AssocItem::Const(ct) => acc.add_const(ctx, ct), + hir::AssocItem::TypeAlias(ty) => acc.add_type_alias(ctx, ty), + } + } + None::<()> + }); + } + } _ => {} - }; + } } #[cfg(test)] @@ -843,6 +889,211 @@ mod tests { ); } + #[test] + fn completes_ty_param_assoc_ty() { + assert_debug_snapshot!( + do_reference_completion( + " + //- /lib.rs + trait Super { + type Ty; + const CONST: u8; + fn func() {} + fn method(&self) {} + } + + trait Sub: Super { + type SubTy; + const C2: (); + fn subfunc() {} + fn submethod(&self) {} + } + + fn foo() { + T::<|> + } + " + ), + @r###" + [ + CompletionItem { + label: "C2", + source_range: 219..219, + delete: 219..219, + insert: "C2", + kind: Const, + detail: "const C2: ();", + }, + CompletionItem { + label: "CONST", + source_range: 219..219, + delete: 219..219, + insert: "CONST", + kind: Const, + detail: "const CONST: u8;", + }, + CompletionItem { + label: "SubTy", + source_range: 219..219, + delete: 219..219, + insert: "SubTy", + kind: TypeAlias, + detail: "type SubTy;", + }, + CompletionItem { + label: "Ty", + source_range: 219..219, + delete: 219..219, + insert: "Ty", + kind: TypeAlias, + detail: "type Ty;", + }, + CompletionItem { + label: "func()", + source_range: 219..219, + delete: 219..219, + insert: "func()$0", + kind: Function, + lookup: "func", + detail: "fn func()", + }, + CompletionItem { + label: "method()", + source_range: 219..219, + delete: 219..219, + insert: "method()$0", + kind: Method, + lookup: "method", + detail: "fn method(&self)", + }, + CompletionItem { + label: "subfunc()", + source_range: 219..219, + delete: 219..219, + insert: "subfunc()$0", + kind: Function, + lookup: "subfunc", + detail: "fn subfunc()", + }, + CompletionItem { + label: "submethod()", + source_range: 219..219, + delete: 219..219, + insert: "submethod()$0", + kind: Method, + lookup: "submethod", + detail: "fn submethod(&self)", + }, + ] + "### + ); + } + + #[test] + fn completes_self_param_assoc_ty() { + assert_debug_snapshot!( + do_reference_completion( + " + //- /lib.rs + trait Super { + type Ty; + const CONST: u8 = 0; + fn func() {} + fn method(&self) {} + } + + trait Sub: Super { + type SubTy; + const C2: () = (); + fn subfunc() {} + fn submethod(&self) {} + } + + struct Wrap(T); + impl Super for Wrap {} + impl Sub for Wrap { + fn subfunc() { + // Should be able to assume `Self: Sub + Super` + Self::<|> + } + } + " + ), + @r###" + [ + CompletionItem { + label: "C2", + source_range: 365..365, + delete: 365..365, + insert: "C2", + kind: Const, + detail: "const C2: () = ();", + }, + CompletionItem { + label: "CONST", + source_range: 365..365, + delete: 365..365, + insert: "CONST", + kind: Const, + detail: "const CONST: u8 = 0;", + }, + CompletionItem { + label: "SubTy", + source_range: 365..365, + delete: 365..365, + insert: "SubTy", + kind: TypeAlias, + detail: "type SubTy;", + }, + CompletionItem { + label: "Ty", + source_range: 365..365, + delete: 365..365, + insert: "Ty", + kind: TypeAlias, + detail: "type Ty;", + }, + CompletionItem { + label: "func()", + source_range: 365..365, + delete: 365..365, + insert: "func()$0", + kind: Function, + lookup: "func", + detail: "fn func()", + }, + CompletionItem { + label: "method()", + source_range: 365..365, + delete: 365..365, + insert: "method()$0", + kind: Method, + lookup: "method", + detail: "fn method(&self)", + }, + CompletionItem { + label: "subfunc()", + source_range: 365..365, + delete: 365..365, + insert: "subfunc()$0", + kind: Function, + lookup: "subfunc", + detail: "fn subfunc()", + }, + CompletionItem { + label: "submethod()", + source_range: 365..365, + delete: 365..365, + insert: "submethod()$0", + kind: Method, + lookup: "submethod", + detail: "fn submethod(&self)", + }, + ] + "### + ); + } + #[test] fn completes_type_alias() { assert_debug_snapshot!( -- cgit v1.2.3 From 0cd6a88cf6f80d48669c6136f5fa814fcdafe8e2 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Thu, 30 Apr 2020 00:10:15 +0200 Subject: if let -> match --- crates/ra_ide/src/completion/complete_qualified_path.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'crates/ra_ide') diff --git a/crates/ra_ide/src/completion/complete_qualified_path.rs b/crates/ra_ide/src/completion/complete_qualified_path.rs index 69e789a49..aa56a5cd8 100644 --- a/crates/ra_ide/src/completion/complete_qualified_path.rs +++ b/crates/ra_ide/src/completion/complete_qualified_path.rs @@ -15,10 +15,9 @@ pub(super) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionCon let scope = ctx.scope(); let context_module = scope.module(); - let res = if let Some(res) = scope.resolve_hir_path(&path) { - res - } else { - return; + let res = match scope.resolve_hir_path(&path) { + Some(res) => res, + None => return, }; // Add associated types on type parameters and `Self`. -- cgit v1.2.3