Skip to content

Commit

Permalink
Add completion for type aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
sinkuu committed Jul 2, 2019
1 parent dd698fc commit 546442d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
4 changes: 4 additions & 0 deletions crates/ra_hir/src/code_model.rs
Expand Up @@ -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()
}
Expand Down
42 changes: 41 additions & 1 deletion crates/ra_ide_api/src/completion/complete_path.rs
Expand Up @@ -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);
Expand All @@ -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));
Expand Down Expand Up @@ -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()",
},
]"###
);
}
Expand Down

0 comments on commit 546442d

Please sign in to comment.