From 546442df68bcb62e7ff4bc7356c6a003052304da Mon Sep 17 00:00:00 2001 From: Shotaro Yamada Date: Wed, 3 Jul 2019 03:08:39 +0900 Subject: Add completion for type aliases --- crates/ra_hir/src/code_model.rs | 4 +++ crates/ra_ide_api/src/completion/complete_path.rs | 42 ++++++++++++++++++++++- 2 files changed, 45 insertions(+), 1 deletion(-) (limited to 'crates') diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 32f98e394..75914ccb0 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -849,6 +849,10 @@ impl TypeAlias { db.type_alias_data(self).type_ref.clone() } + pub fn ty(self, db: &impl HirDatabase) -> Ty { + db.type_for_def(self.into(), Namespace::Types) + } + pub fn name(self, db: &impl DefDatabase) -> Name { db.type_alias_data(self).name.clone() } diff --git a/crates/ra_ide_api/src/completion/complete_path.rs b/crates/ra_ide_api/src/completion/complete_path.rs index a41cab359..da8fb9d8e 100644 --- a/crates/ra_ide_api/src/completion/complete_path.rs +++ b/crates/ra_ide_api/src/completion/complete_path.rs @@ -37,7 +37,10 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) { acc.add_resolution(ctx, name.to_string(), &res.def.map(hir::Resolution::Def)); } } - hir::ModuleDef::Enum(_) | hir::ModuleDef::Struct(_) | hir::ModuleDef::Union(_) => { + hir::ModuleDef::Enum(_) + | hir::ModuleDef::Struct(_) + | hir::ModuleDef::Union(_) + | hir::ModuleDef::TypeAlias(_) => { if let hir::ModuleDef::Enum(e) = def { for variant in e.variants(ctx.db) { acc.add_enum_variant(ctx, variant); @@ -47,6 +50,7 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) { hir::ModuleDef::Enum(e) => e.ty(ctx.db), hir::ModuleDef::Struct(s) => s.ty(ctx.db), hir::ModuleDef::Union(u) => u.ty(ctx.db), + hir::ModuleDef::TypeAlias(a) => a.ty(ctx.db), _ => unreachable!(), }; let krate = ctx.module.and_then(|m| m.krate(ctx.db)); @@ -544,6 +548,42 @@ mod tests { insert: "bar", kind: Module, }, +]"### + ); + } + + #[test] + fn completes_type_alias() { + assert_debug_snapshot_matches!( + do_reference_completion( + " + struct S; + impl S { fn foo() {} } + type T = S; + impl T { fn bar() {} } + + fn main() { + T::<|>; + } + " + ), + @r###"[ + CompletionItem { + label: "bar", + source_range: [185; 185), + delete: [185; 185), + insert: "bar()$0", + kind: Function, + detail: "fn bar()", + }, + CompletionItem { + label: "foo", + source_range: [185; 185), + delete: [185; 185), + insert: "foo()$0", + kind: Function, + detail: "fn foo()", + }, ]"### ); } -- cgit v1.2.3