@@ -5,22 +5,21 @@ use ide_db::FxHashSet;
55use syntax:: { ast, AstNode } ;
66
77use 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
1413pub ( 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
191198pub ( 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