Skip to content

Commit e782e59

Browse files
committed
fix: Calculate completions after type anchors
1 parent b4d652a commit e782e59

File tree

12 files changed

+151
-36
lines changed

12 files changed

+151
-36
lines changed

crates/hir/src/semantics.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,10 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> {
324324
self.imp.resolve_type(ty)
325325
}
326326

327+
pub fn resolve_trait(&self, trait_: &ast::Path) -> Option<Trait> {
328+
self.imp.resolve_trait(trait_)
329+
}
330+
327331
// FIXME: Figure out a nice interface to inspect adjustments
328332
pub fn is_implicit_reborrow(&self, expr: &ast::Expr) -> Option<Mutability> {
329333
self.imp.is_implicit_reborrow(expr)
@@ -1014,6 +1018,20 @@ impl<'db> SemanticsImpl<'db> {
10141018
Some(Type::new_with_resolver(self.db, &analyze.resolver, ty))
10151019
}
10161020

1021+
fn resolve_trait(&self, path: &ast::Path) -> Option<Trait> {
1022+
let analyze = self.analyze(path.syntax())?;
1023+
let hygiene = hir_expand::hygiene::Hygiene::new(self.db.upcast(), analyze.file_id);
1024+
let ctx = body::LowerCtx::with_hygiene(self.db.upcast(), &hygiene);
1025+
let hir_path = Path::from_src(path.clone(), &ctx)?;
1026+
match analyze
1027+
.resolver
1028+
.resolve_path_in_type_ns_fully(self.db.upcast(), hir_path.mod_path())?
1029+
{
1030+
TypeNs::TraitId(id) => Some(Trait { id }),
1031+
_ => None,
1032+
}
1033+
}
1034+
10171035
fn is_implicit_reborrow(&self, expr: &ast::Expr) -> Option<Mutability> {
10181036
self.analyze(expr.syntax())?.is_implicit_reborrow(self.db, expr)
10191037
}

crates/ide-completion/src/completions/attribute.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ pub(crate) fn complete_attribute_path(
115115
});
116116
acc.add_nameref_keywords_with_colon(ctx);
117117
}
118-
Qualified::Infer | Qualified::With { .. } => {}
118+
Qualified::TypeAnchor { .. } | Qualified::With { .. } => {}
119119
}
120120

121121
let attributes = annotated_item_kind.and_then(|kind| {

crates/ide-completion/src/completions/attribute/derive.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ pub(crate) fn complete_derive_path(
9797
});
9898
acc.add_nameref_keywords_with_colon(ctx);
9999
}
100-
Qualified::Infer | Qualified::With { .. } => {}
100+
Qualified::TypeAnchor { .. } | Qualified::With { .. } => {}
101101
}
102102
}
103103

crates/ide-completion/src/completions/expr.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,32 @@ pub(crate) fn complete_expr_path(
4646
};
4747

4848
match qualified {
49-
Qualified::Infer => ctx
49+
Qualified::TypeAnchor { ty: None, trait_: None } => ctx
5050
.traits_in_scope()
5151
.iter()
5252
.flat_map(|&it| hir::Trait::from(it).items(ctx.sema.db))
5353
.for_each(|item| add_assoc_item(acc, item)),
54+
Qualified::TypeAnchor { trait_: Some(trait_), .. } => {
55+
trait_.items(ctx.sema.db).into_iter().for_each(|item| add_assoc_item(acc, item))
56+
}
57+
Qualified::TypeAnchor { ty: Some(ty), trait_: None } => {
58+
if let Some(hir::Adt::Enum(e)) = ty.as_adt() {
59+
cov_mark::hit!(completes_variant_through_alias);
60+
acc.add_enum_variants(ctx, path_ctx, e);
61+
}
62+
63+
ctx.iterate_path_candidates(&ty, |item| {
64+
add_assoc_item(acc, item);
65+
});
66+
67+
// Iterate assoc types separately
68+
ty.iterate_assoc_items(ctx.db, ctx.krate, |item| {
69+
if let hir::AssocItem::TypeAlias(ty) = item {
70+
acc.add_type_alias(ctx, ty)
71+
}
72+
None::<()>
73+
});
74+
}
5475
Qualified::With { resolution: None, .. } => {}
5576
Qualified::With { resolution: Some(resolution), .. } => {
5677
// Add associated types on type parameters and `Self`.

crates/ide-completion/src/completions/item_list.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ pub(crate) fn complete_item_list(
6666
});
6767
acc.add_nameref_keywords_with_colon(ctx);
6868
}
69-
Qualified::Infer | Qualified::No | Qualified::With { .. } => {}
69+
Qualified::TypeAnchor { .. } | Qualified::No | Qualified::With { .. } => {}
7070
}
7171
}
7272

crates/ide-completion/src/completions/pattern.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,6 @@ pub(crate) fn complete_pattern_path(
180180

181181
acc.add_nameref_keywords_with_colon(ctx);
182182
}
183-
Qualified::Infer | Qualified::With { .. } => {}
183+
Qualified::TypeAnchor { .. } | Qualified::With { .. } => {}
184184
}
185185
}

crates/ide-completion/src/completions/type.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,27 @@ pub(crate) fn complete_type_path(
4949
};
5050

5151
match qualified {
52-
Qualified::Infer => ctx
52+
Qualified::TypeAnchor { ty: None, trait_: None } => ctx
5353
.traits_in_scope()
5454
.iter()
5555
.flat_map(|&it| hir::Trait::from(it).items(ctx.sema.db))
5656
.for_each(|item| add_assoc_item(acc, item)),
57+
Qualified::TypeAnchor { trait_: Some(trait_), .. } => {
58+
trait_.items(ctx.sema.db).into_iter().for_each(|item| add_assoc_item(acc, item))
59+
}
60+
Qualified::TypeAnchor { ty: Some(ty), trait_: None } => {
61+
ctx.iterate_path_candidates(&ty, |item| {
62+
add_assoc_item(acc, item);
63+
});
64+
65+
// Iterate assoc types separately
66+
ty.iterate_assoc_items(ctx.db, ctx.krate, |item| {
67+
if let hir::AssocItem::TypeAlias(ty) = item {
68+
acc.add_type_alias(ctx, ty)
69+
}
70+
None::<()>
71+
});
72+
}
5773
Qualified::With { resolution: None, .. } => {}
5874
Qualified::With { resolution: Some(resolution), .. } => {
5975
// Add associated types on type parameters and `Self`.

crates/ide-completion/src/completions/use_.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,6 @@ pub(crate) fn complete_use_path(
115115
});
116116
acc.add_nameref_keywords_with_colon(ctx);
117117
}
118-
Qualified::Infer | Qualified::With { resolution: None, .. } => {}
118+
Qualified::TypeAnchor { .. } | Qualified::With { resolution: None, .. } => {}
119119
}
120120
}

crates/ide-completion/src/completions/vis.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ pub(crate) fn complete_vis_path(
2929

3030
acc.add_super_keyword(ctx, *super_chain_len);
3131
}
32-
Qualified::Absolute | Qualified::Infer | Qualified::With { .. } => {}
32+
Qualified::Absolute | Qualified::TypeAnchor { .. } | Qualified::With { .. } => {}
3333
Qualified::No => {
3434
if !has_in_token {
3535
cov_mark::hit!(kw_completion_in);

crates/ide-completion/src/context.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,10 @@ pub(super) enum Qualified {
193193
super_chain_len: Option<usize>,
194194
},
195195
/// <_>::
196-
Infer,
196+
TypeAnchor {
197+
ty: Option<hir::Type>,
198+
trait_: Option<hir::Trait>,
199+
},
197200
/// Whether the path is an absolute path
198201
Absolute,
199202
}

0 commit comments

Comments
 (0)