aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShotaro Yamada <[email protected]>2019-07-02 19:08:39 +0100
committerShotaro Yamada <[email protected]>2019-07-02 19:08:39 +0100
commit546442df68bcb62e7ff4bc7356c6a003052304da (patch)
tree635e47b1796a224917e38899549d8ce04d9755cf
parentdd698fc3f7090b7781504566a8b82cf2c3d62420 (diff)
Add completion for type aliases
-rw-r--r--crates/ra_hir/src/code_model.rs4
-rw-r--r--crates/ra_ide_api/src/completion/complete_path.rs42
2 files changed, 45 insertions, 1 deletions
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 {
849 db.type_alias_data(self).type_ref.clone() 849 db.type_alias_data(self).type_ref.clone()
850 } 850 }
851 851
852 pub fn ty(self, db: &impl HirDatabase) -> Ty {
853 db.type_for_def(self.into(), Namespace::Types)
854 }
855
852 pub fn name(self, db: &impl DefDatabase) -> Name { 856 pub fn name(self, db: &impl DefDatabase) -> Name {
853 db.type_alias_data(self).name.clone() 857 db.type_alias_data(self).name.clone()
854 } 858 }
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) {
37 acc.add_resolution(ctx, name.to_string(), &res.def.map(hir::Resolution::Def)); 37 acc.add_resolution(ctx, name.to_string(), &res.def.map(hir::Resolution::Def));
38 } 38 }
39 } 39 }
40 hir::ModuleDef::Enum(_) | hir::ModuleDef::Struct(_) | hir::ModuleDef::Union(_) => { 40 hir::ModuleDef::Enum(_)
41 | hir::ModuleDef::Struct(_)
42 | hir::ModuleDef::Union(_)
43 | hir::ModuleDef::TypeAlias(_) => {
41 if let hir::ModuleDef::Enum(e) = def { 44 if let hir::ModuleDef::Enum(e) = def {
42 for variant in e.variants(ctx.db) { 45 for variant in e.variants(ctx.db) {
43 acc.add_enum_variant(ctx, variant); 46 acc.add_enum_variant(ctx, variant);
@@ -47,6 +50,7 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) {
47 hir::ModuleDef::Enum(e) => e.ty(ctx.db), 50 hir::ModuleDef::Enum(e) => e.ty(ctx.db),
48 hir::ModuleDef::Struct(s) => s.ty(ctx.db), 51 hir::ModuleDef::Struct(s) => s.ty(ctx.db),
49 hir::ModuleDef::Union(u) => u.ty(ctx.db), 52 hir::ModuleDef::Union(u) => u.ty(ctx.db),
53 hir::ModuleDef::TypeAlias(a) => a.ty(ctx.db),
50 _ => unreachable!(), 54 _ => unreachable!(),
51 }; 55 };
52 let krate = ctx.module.and_then(|m| m.krate(ctx.db)); 56 let krate = ctx.module.and_then(|m| m.krate(ctx.db));
@@ -547,4 +551,40 @@ mod tests {
547]"### 551]"###
548 ); 552 );
549 } 553 }
554
555 #[test]
556 fn completes_type_alias() {
557 assert_debug_snapshot_matches!(
558 do_reference_completion(
559 "
560 struct S;
561 impl S { fn foo() {} }
562 type T = S;
563 impl T { fn bar() {} }
564
565 fn main() {
566 T::<|>;
567 }
568 "
569 ),
570 @r###"[
571 CompletionItem {
572 label: "bar",
573 source_range: [185; 185),
574 delete: [185; 185),
575 insert: "bar()$0",
576 kind: Function,
577 detail: "fn bar()",
578 },
579 CompletionItem {
580 label: "foo",
581 source_range: [185; 185),
582 delete: [185; 185),
583 insert: "foo()$0",
584 kind: Function,
585 detail: "fn foo()",
586 },
587]"###
588 );
589 }
550} 590}