@@ -5,22 +5,21 @@ use ide_db::FxHashSet;
5
5
use syntax:: { ast, AstNode } ;
6
6
7
7
use crate :: {
8
- context:: { PathCompletionCtx , PathKind , PathQualifierCtx , TypeAscriptionTarget } ,
9
- patterns:: ImmediateLocation ,
8
+ context:: { PathCompletionCtx , PathKind , PathQualifierCtx , TypeAscriptionTarget , TypeLocation } ,
10
9
render:: render_type_inference,
11
10
CompletionContext , Completions ,
12
11
} ;
13
12
14
13
pub ( crate ) fn complete_type_path ( acc : & mut Completions , ctx : & CompletionContext ) {
15
14
let _p = profile:: span ( "complete_type_path" ) ;
16
15
17
- let ( & is_absolute_path, qualifier) = match ctx. path_context ( ) {
16
+ let ( & is_absolute_path, location , qualifier) = match ctx. path_context ( ) {
18
17
Some ( PathCompletionCtx {
19
- kind : PathKind :: Type { .. } ,
18
+ kind : PathKind :: Type { location } ,
20
19
is_absolute_path,
21
20
qualifier,
22
21
..
23
- } ) => ( is_absolute_path, qualifier) ,
22
+ } ) => ( is_absolute_path, location , qualifier) ,
24
23
_ => return ,
25
24
} ;
26
25
@@ -32,7 +31,7 @@ pub(crate) fn complete_type_path(acc: &mut Completions, ctx: &CompletionContext)
32
31
ScopeDef :: ModuleDef ( Function ( _) | Variant ( _) | Static ( _) ) | ScopeDef :: Local ( _) => false ,
33
32
// unless its a constant in a generic arg list position
34
33
ScopeDef :: ModuleDef ( Const ( _) ) | ScopeDef :: GenericParam ( ConstParam ( _) ) => {
35
- ctx . expects_generic_arg ( )
34
+ matches ! ( location , TypeLocation :: GenericArgList ( _ ) )
36
35
}
37
36
ScopeDef :: ImplSelfType ( _) => {
38
37
!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)
47
46
}
48
47
} ;
49
48
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
+
50
57
match qualifier {
51
58
Some ( PathQualifierCtx { is_infer_qualifier, resolution, .. } ) => {
52
59
if * is_infer_qualifier {
53
60
ctx. traits_in_scope ( )
54
61
. 0
55
62
. into_iter ( )
56
63
. 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) ) ;
58
65
return ;
59
66
}
60
67
let resolution = match resolution {
@@ -98,7 +105,7 @@ pub(crate) fn complete_type_path(acc: &mut Completions, ctx: &CompletionContext)
98
105
Some ( ctx. module ) ,
99
106
None ,
100
107
|item| {
101
- add_assoc_item ( acc, ctx , item) ;
108
+ add_assoc_item ( acc, item) ;
102
109
None :: < ( ) >
103
110
} ,
104
111
) ;
@@ -114,7 +121,7 @@ pub(crate) fn complete_type_path(acc: &mut Completions, ctx: &CompletionContext)
114
121
hir:: PathResolution :: Def ( hir:: ModuleDef :: Trait ( t) ) => {
115
122
// Handles `Trait::assoc` as well as `<Ty as Trait>::assoc`.
116
123
for item in t. items ( ctx. db ) {
117
- add_assoc_item ( acc, ctx , item) ;
124
+ add_assoc_item ( acc, item) ;
118
125
}
119
126
}
120
127
hir:: PathResolution :: TypeParam ( _) | hir:: PathResolution :: SelfType ( _) => {
@@ -135,7 +142,7 @@ pub(crate) fn complete_type_path(acc: &mut Completions, ctx: &CompletionContext)
135
142
// We might iterate candidates of a trait multiple times here, so deduplicate
136
143
// them.
137
144
if seen. insert ( item) {
138
- add_assoc_item ( acc, ctx , item) ;
145
+ add_assoc_item ( acc, item) ;
139
146
}
140
147
None :: < ( ) >
141
148
} ,
@@ -147,7 +154,7 @@ pub(crate) fn complete_type_path(acc: &mut Completions, ctx: &CompletionContext)
147
154
None if is_absolute_path => acc. add_crate_roots ( ctx) ,
148
155
None => {
149
156
acc. add_nameref_keywords_with_colon ( ctx) ;
150
- if let Some ( ImmediateLocation :: TypeBound ) = & ctx . completion_location {
157
+ if let TypeLocation :: TypeBound = location {
151
158
ctx. process_all_names ( & mut |name, res| {
152
159
let add_resolution = match res {
153
160
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)
162
169
} ) ;
163
170
return ;
164
171
}
165
- if let Some ( ImmediateLocation :: GenericArgList ( arg_list) ) = & ctx . completion_location {
172
+ if let TypeLocation :: GenericArgList ( Some ( arg_list) ) = location {
166
173
if let Some ( path_seg) = arg_list. syntax ( ) . parent ( ) . and_then ( ast:: PathSegment :: cast)
167
174
{
168
175
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)
189
196
}
190
197
191
198
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 ( ) {
193
200
Some (
194
201
ctx @ PathCompletionCtx {
195
- kind : PathKind :: Type { ascription : Some ( ascription) , .. } ,
202
+ kind : PathKind :: Type { location : TypeLocation :: TypeAscription ( ascription) , .. } ,
196
203
..
197
204
} ,
198
205
) if ctx. is_trivial_path ( ) => ascription,
@@ -211,11 +218,3 @@ pub(crate) fn complete_inferred_type(acc: &mut Completions, ctx: &CompletionCont
211
218
acc. add ( render_type_inference ( ty_string, ctx) ) ;
212
219
None
213
220
}
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