Skip to content

Commit f201a40

Browse files
committed
Remove ImmediateLocation in favor of PathKind::Type
1 parent 6b24629 commit f201a40

File tree

5 files changed

+87
-157
lines changed

5 files changed

+87
-157
lines changed

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use crate::{
44
context::{
55
IdentContext, NameContext, NameKind, NameRefContext, NameRefKind, PathCompletionCtx,
6-
PathKind,
6+
PathKind, TypeLocation,
77
},
88
CompletionContext, Completions,
99
};
@@ -18,7 +18,7 @@ pub(crate) fn complete_field_list(acc: &mut Completions, ctx: &CompletionContext
1818
is_absolute_path: false,
1919
qualifier: None,
2020
parent: None,
21-
kind: PathKind::Type { in_tuple_struct: true, ascription: None },
21+
kind: PathKind::Type { location: TypeLocation::TupleField },
2222
has_type_args: false,
2323
..
2424
})),

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

+5-5
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ use syntax::{AstNode, SyntaxNode, T};
99

1010
use crate::{
1111
context::{
12-
CompletionContext, NameRefContext, NameRefKind, PathCompletionCtx, PathKind, PatternContext,
12+
CompletionContext, NameRefContext, NameRefKind, PathCompletionCtx, PathKind,
13+
PatternContext, TypeLocation,
1314
},
14-
patterns::ImmediateLocation,
1515
render::{render_resolution_with_import, RenderContext},
1616
};
1717

@@ -112,7 +112,7 @@ pub(crate) fn import_on_the_fly(acc: &mut Completions, ctx: &CompletionContext)
112112
if !ctx.config.enable_imports_on_the_fly {
113113
return None;
114114
}
115-
let path_kind = match dbg!(ctx.nameref_ctx()) {
115+
let path_kind = match ctx.nameref_ctx() {
116116
Some(NameRefContext {
117117
kind:
118118
Some(NameRefKind::Path(PathCompletionCtx {
@@ -176,8 +176,8 @@ pub(crate) fn import_on_the_fly(acc: &mut Completions, ctx: &CompletionContext)
176176
(PathKind::Pat, ItemInNs::Types(_)) => true,
177177
(PathKind::Pat, ItemInNs::Values(def)) => matches!(def, hir::ModuleDef::Const(_)),
178178

179-
(PathKind::Type { .. }, ItemInNs::Types(ty)) => {
180-
if matches!(ctx.completion_location, Some(ImmediateLocation::TypeBound)) {
179+
(PathKind::Type { location }, ItemInNs::Types(ty)) => {
180+
if matches!(location, TypeLocation::TypeBound) {
181181
matches!(ty, ModuleDef::Trait(_))
182182
} else {
183183
true

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

+21-22
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,21 @@ use ide_db::FxHashSet;
55
use syntax::{ast, AstNode};
66

77
use crate::{
8-
context::{PathCompletionCtx, PathKind, PathQualifierCtx, TypeAscriptionTarget},
9-
patterns::ImmediateLocation,
8+
context::{PathCompletionCtx, PathKind, PathQualifierCtx, TypeAscriptionTarget, TypeLocation},
109
render::render_type_inference,
1110
CompletionContext, Completions,
1211
};
1312

1413
pub(crate) fn complete_type_path(acc: &mut Completions, ctx: &CompletionContext) {
1514
let _p = profile::span("complete_type_path");
1615

17-
let (&is_absolute_path, qualifier) = match ctx.path_context() {
16+
let (&is_absolute_path, location, qualifier) = match ctx.path_context() {
1817
Some(PathCompletionCtx {
19-
kind: PathKind::Type { .. },
18+
kind: PathKind::Type { location },
2019
is_absolute_path,
2120
qualifier,
2221
..
23-
}) => (is_absolute_path, qualifier),
22+
}) => (is_absolute_path, location, qualifier),
2423
_ => return,
2524
};
2625

@@ -32,7 +31,7 @@ pub(crate) fn complete_type_path(acc: &mut Completions, ctx: &CompletionContext)
3231
ScopeDef::ModuleDef(Function(_) | Variant(_) | Static(_)) | ScopeDef::Local(_) => false,
3332
// unless its a constant in a generic arg list position
3433
ScopeDef::ModuleDef(Const(_)) | ScopeDef::GenericParam(ConstParam(_)) => {
35-
ctx.expects_generic_arg()
34+
matches!(location, TypeLocation::GenericArgList(_))
3635
}
3736
ScopeDef::ImplSelfType(_) => {
3837
!ctx.previous_token_is(syntax::T![impl]) && !ctx.previous_token_is(syntax::T![for])
@@ -47,14 +46,22 @@ pub(crate) fn complete_type_path(acc: &mut Completions, ctx: &CompletionContext)
4746
}
4847
};
4948

49+
let add_assoc_item = |acc: &mut Completions, item| match item {
50+
hir::AssocItem::Const(ct) if matches!(location, TypeLocation::GenericArgList(_)) => {
51+
acc.add_const(ctx, ct)
52+
}
53+
hir::AssocItem::Function(_) | hir::AssocItem::Const(_) => (),
54+
hir::AssocItem::TypeAlias(ty) => acc.add_type_alias(ctx, ty),
55+
};
56+
5057
match qualifier {
5158
Some(PathQualifierCtx { is_infer_qualifier, resolution, .. }) => {
5259
if *is_infer_qualifier {
5360
ctx.traits_in_scope()
5461
.0
5562
.into_iter()
5663
.flat_map(|it| hir::Trait::from(it).items(ctx.sema.db))
57-
.for_each(|item| add_assoc_item(acc, ctx, item));
64+
.for_each(|item| add_assoc_item(acc, item));
5865
return;
5966
}
6067
let resolution = match resolution {
@@ -98,7 +105,7 @@ pub(crate) fn complete_type_path(acc: &mut Completions, ctx: &CompletionContext)
98105
Some(ctx.module),
99106
None,
100107
|item| {
101-
add_assoc_item(acc, ctx, item);
108+
add_assoc_item(acc, item);
102109
None::<()>
103110
},
104111
);
@@ -114,7 +121,7 @@ pub(crate) fn complete_type_path(acc: &mut Completions, ctx: &CompletionContext)
114121
hir::PathResolution::Def(hir::ModuleDef::Trait(t)) => {
115122
// Handles `Trait::assoc` as well as `<Ty as Trait>::assoc`.
116123
for item in t.items(ctx.db) {
117-
add_assoc_item(acc, ctx, item);
124+
add_assoc_item(acc, item);
118125
}
119126
}
120127
hir::PathResolution::TypeParam(_) | hir::PathResolution::SelfType(_) => {
@@ -135,7 +142,7 @@ pub(crate) fn complete_type_path(acc: &mut Completions, ctx: &CompletionContext)
135142
// We might iterate candidates of a trait multiple times here, so deduplicate
136143
// them.
137144
if seen.insert(item) {
138-
add_assoc_item(acc, ctx, item);
145+
add_assoc_item(acc, item);
139146
}
140147
None::<()>
141148
},
@@ -147,7 +154,7 @@ pub(crate) fn complete_type_path(acc: &mut Completions, ctx: &CompletionContext)
147154
None if is_absolute_path => acc.add_crate_roots(ctx),
148155
None => {
149156
acc.add_nameref_keywords_with_colon(ctx);
150-
if let Some(ImmediateLocation::TypeBound) = &ctx.completion_location {
157+
if let TypeLocation::TypeBound = location {
151158
ctx.process_all_names(&mut |name, res| {
152159
let add_resolution = match res {
153160
ScopeDef::ModuleDef(hir::ModuleDef::Macro(mac)) => mac.is_fn_like(ctx.db),
@@ -162,7 +169,7 @@ pub(crate) fn complete_type_path(acc: &mut Completions, ctx: &CompletionContext)
162169
});
163170
return;
164171
}
165-
if let Some(ImmediateLocation::GenericArgList(arg_list)) = &ctx.completion_location {
172+
if let TypeLocation::GenericArgList(Some(arg_list)) = location {
166173
if let Some(path_seg) = arg_list.syntax().parent().and_then(ast::PathSegment::cast)
167174
{
168175
if path_seg.syntax().ancestors().find_map(ast::TypeBound::cast).is_some() {
@@ -189,10 +196,10 @@ pub(crate) fn complete_type_path(acc: &mut Completions, ctx: &CompletionContext)
189196
}
190197

191198
pub(crate) fn complete_inferred_type(acc: &mut Completions, ctx: &CompletionContext) -> Option<()> {
192-
let pat = match dbg!(ctx.path_context()) {
199+
let pat = match ctx.path_context() {
193200
Some(
194201
ctx @ PathCompletionCtx {
195-
kind: PathKind::Type { ascription: Some(ascription), .. },
202+
kind: PathKind::Type { location: TypeLocation::TypeAscription(ascription), .. },
196203
..
197204
},
198205
) if ctx.is_trivial_path() => ascription,
@@ -211,11 +218,3 @@ pub(crate) fn complete_inferred_type(acc: &mut Completions, ctx: &CompletionCont
211218
acc.add(render_type_inference(ty_string, ctx));
212219
None
213220
}
214-
215-
fn add_assoc_item(acc: &mut Completions, ctx: &CompletionContext, item: hir::AssocItem) {
216-
match item {
217-
hir::AssocItem::Const(ct) if ctx.expects_generic_arg() => acc.add_const(ctx, ct),
218-
hir::AssocItem::Function(_) | hir::AssocItem::Const(_) => (),
219-
hir::AssocItem::TypeAlias(ty) => acc.add_type_alias(ctx, ty),
220-
}
221-
}

0 commit comments

Comments
 (0)