Skip to content

Commit f15f0d1

Browse files
bors[bot]sinkuu
andcommitted
Merge #1472
1472: Add completion for type aliases r=matklad a=sinkuu Co-authored-by: Shotaro Yamada <[email protected]>
2 parents dd698fc + 546442d commit f15f0d1

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

crates/ra_hir/src/code_model.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -849,6 +849,10 @@ impl TypeAlias {
849849
db.type_alias_data(self).type_ref.clone()
850850
}
851851

852+
pub fn ty(self, db: &impl HirDatabase) -> Ty {
853+
db.type_for_def(self.into(), Namespace::Types)
854+
}
855+
852856
pub fn name(self, db: &impl DefDatabase) -> Name {
853857
db.type_alias_data(self).name.clone()
854858
}

crates/ra_ide_api/src/completion/complete_path.rs

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) {
3737
acc.add_resolution(ctx, name.to_string(), &res.def.map(hir::Resolution::Def));
3838
}
3939
}
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(_) => {
4144
if let hir::ModuleDef::Enum(e) = def {
4245
for variant in e.variants(ctx.db) {
4346
acc.add_enum_variant(ctx, variant);
@@ -47,6 +50,7 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) {
4750
hir::ModuleDef::Enum(e) => e.ty(ctx.db),
4851
hir::ModuleDef::Struct(s) => s.ty(ctx.db),
4952
hir::ModuleDef::Union(u) => u.ty(ctx.db),
53+
hir::ModuleDef::TypeAlias(a) => a.ty(ctx.db),
5054
_ => unreachable!(),
5155
};
5256
let krate = ctx.module.and_then(|m| m.krate(ctx.db));
@@ -544,6 +548,42 @@ mod tests {
544548
insert: "bar",
545549
kind: Module,
546550
},
551+
]"###
552+
);
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+
},
547587
]"###
548588
);
549589
}

0 commit comments

Comments
 (0)