11//! Completion of names from the current scope in expression position.
22
33use hir:: ScopeDef ;
4- use ide_db:: FxHashSet ;
54
65use crate :: {
76 context:: { ExprCtx , PathCompletionCtx , Qualified } ,
@@ -33,24 +32,24 @@ pub(crate) fn complete_expr_path(
3332 let wants_mut_token =
3433 ref_expr_parent. as_ref ( ) . map ( |it| it. mut_token ( ) . is_none ( ) ) . unwrap_or ( false ) ;
3534
36- let scope_def_applicable = |def| {
37- match def {
38- ScopeDef :: GenericParam ( hir:: GenericParam :: LifetimeParam ( _) ) | ScopeDef :: Label ( _) => {
39- false
40- }
41- // Don't suggest attribute macros and derives.
42- ScopeDef :: ModuleDef ( hir:: ModuleDef :: Macro ( mac) ) => mac. is_fn_like ( ctx. db ) ,
43- _ => true ,
44- }
35+ let scope_def_applicable = |def| match def {
36+ ScopeDef :: GenericParam ( hir:: GenericParam :: LifetimeParam ( _) ) | ScopeDef :: Label ( _) => false ,
37+ ScopeDef :: ModuleDef ( hir:: ModuleDef :: Macro ( mac) ) => mac. is_fn_like ( ctx. db ) ,
38+ _ => true ,
39+ } ;
40+
41+ let add_assoc_item = |acc : & mut Completions , item| match item {
42+ hir:: AssocItem :: Function ( func) => acc. add_function ( ctx, path_ctx, func, None ) ,
43+ hir:: AssocItem :: Const ( ct) => acc. add_const ( ctx, ct) ,
44+ hir:: AssocItem :: TypeAlias ( ty) => acc. add_type_alias ( ctx, ty) ,
4545 } ;
4646
4747 match qualified {
4848 Qualified :: Infer => ctx
4949 . traits_in_scope ( )
50- . 0
51- . into_iter ( )
52- . flat_map ( |it| hir:: Trait :: from ( it) . items ( ctx. sema . db ) )
53- . for_each ( |item| add_assoc_item ( acc, ctx, path_ctx, item) ) ,
50+ . iter ( )
51+ . flat_map ( |& it| hir:: Trait :: from ( it) . items ( ctx. sema . db ) )
52+ . for_each ( |item| add_assoc_item ( acc, item) ) ,
5453 Qualified :: With { resolution : None , .. } => { }
5554 Qualified :: With { resolution : Some ( resolution) , .. } => {
5655 // Add associated types on type parameters and `Self`.
@@ -67,46 +66,32 @@ pub(crate) fn complete_expr_path(
6766 }
6867 }
6968 }
70-
7169 hir:: PathResolution :: Def (
7270 def @ ( hir:: ModuleDef :: Adt ( _)
7371 | hir:: ModuleDef :: TypeAlias ( _)
7472 | hir:: ModuleDef :: BuiltinType ( _) ) ,
7573 ) => {
76- if let & hir:: ModuleDef :: Adt ( hir:: Adt :: Enum ( e) ) = def {
77- add_enum_variants ( acc, ctx, path_ctx, e) ;
78- }
7974 let ty = match def {
8075 hir:: ModuleDef :: Adt ( adt) => adt. ty ( ctx. db ) ,
81- hir:: ModuleDef :: TypeAlias ( a) => {
82- let ty = a. ty ( ctx. db ) ;
83- if let Some ( hir:: Adt :: Enum ( e) ) = ty. as_adt ( ) {
84- cov_mark:: hit!( completes_variant_through_alias) ;
85- add_enum_variants ( acc, ctx, path_ctx, e) ;
86- }
87- ty
88- }
76+ hir:: ModuleDef :: TypeAlias ( a) => a. ty ( ctx. db ) ,
8977 hir:: ModuleDef :: BuiltinType ( builtin) => {
9078 cov_mark:: hit!( completes_primitive_assoc_const) ;
9179 builtin. ty ( ctx. db )
9280 }
93- _ => unreachable ! ( ) ,
81+ _ => return ,
9482 } ;
9583
84+ if let Some ( hir:: Adt :: Enum ( e) ) = ty. as_adt ( ) {
85+ cov_mark:: hit!( completes_variant_through_alias) ;
86+ acc. add_enum_variants ( ctx, path_ctx, e) ;
87+ }
88+
9689 // XXX: For parity with Rust bug #22519, this does not complete Ty::AssocType.
9790 // (where AssocType is defined on a trait, not an inherent impl)
9891
99- ty. iterate_path_candidates (
100- ctx. db ,
101- & ctx. scope ,
102- & ctx. traits_in_scope ( ) . 0 ,
103- Some ( ctx. module ) ,
104- None ,
105- |item| {
106- add_assoc_item ( acc, ctx, path_ctx, item) ;
107- None :: < ( ) >
108- } ,
109- ) ;
92+ ctx. iterate_path_candidates ( & ty, |item| {
93+ add_assoc_item ( acc, item) ;
94+ } ) ;
11095
11196 // Iterate assoc types separately
11297 ty. iterate_assoc_items ( ctx. db , ctx. krate , |item| {
@@ -119,7 +104,7 @@ pub(crate) fn complete_expr_path(
119104 hir:: PathResolution :: Def ( hir:: ModuleDef :: Trait ( t) ) => {
120105 // Handles `Trait::assoc` as well as `<Ty as Trait>::assoc`.
121106 for item in t. items ( ctx. db ) {
122- add_assoc_item ( acc, ctx , path_ctx , item) ;
107+ add_assoc_item ( acc, item) ;
123108 }
124109 }
125110 hir:: PathResolution :: TypeParam ( _) | hir:: PathResolution :: SelfType ( _) => {
@@ -130,24 +115,12 @@ pub(crate) fn complete_expr_path(
130115 } ;
131116
132117 if let Some ( hir:: Adt :: Enum ( e) ) = ty. as_adt ( ) {
133- add_enum_variants ( acc , ctx, path_ctx, e) ;
118+ acc . add_enum_variants ( ctx, path_ctx, e) ;
134119 }
135- let mut seen = FxHashSet :: default ( ) ;
136- ty. iterate_path_candidates (
137- ctx. db ,
138- & ctx. scope ,
139- & ctx. traits_in_scope ( ) . 0 ,
140- Some ( ctx. module ) ,
141- None ,
142- |item| {
143- // We might iterate candidates of a trait multiple times here, so deduplicate
144- // them.
145- if seen. insert ( item) {
146- add_assoc_item ( acc, ctx, path_ctx, item) ;
147- }
148- None :: < ( ) >
149- } ,
150- ) ;
120+
121+ ctx. iterate_path_candidates ( & ty, |item| {
122+ add_assoc_item ( acc, item) ;
123+ } ) ;
151124 }
152125 _ => ( ) ,
153126 }
@@ -212,7 +185,7 @@ pub(crate) fn complete_expr_path(
212185
213186 if is_func_update. is_none ( ) {
214187 let mut add_keyword =
215- |kw, snippet| acc. add_keyword_snippet_expr ( ctx, kw , snippet , incomplete_let ) ;
188+ |kw, snippet| acc. add_keyword_snippet_expr ( ctx, incomplete_let , kw , snippet ) ;
216189
217190 if !in_block_expr {
218191 add_keyword ( "unsafe" , "unsafe {\n $0\n }" ) ;
@@ -265,27 +238,3 @@ pub(crate) fn complete_expr_path(
265238 }
266239 }
267240}
268-
269- fn add_assoc_item (
270- acc : & mut Completions ,
271- ctx : & CompletionContext ,
272- path_ctx : & PathCompletionCtx ,
273- item : hir:: AssocItem ,
274- ) {
275- match item {
276- hir:: AssocItem :: Function ( func) => acc. add_function ( ctx, path_ctx, func, None ) ,
277- hir:: AssocItem :: Const ( ct) => acc. add_const ( ctx, ct) ,
278- hir:: AssocItem :: TypeAlias ( ty) => acc. add_type_alias ( ctx, ty) ,
279- }
280- }
281-
282- fn add_enum_variants (
283- acc : & mut Completions ,
284- ctx : & CompletionContext ,
285- path_ctx : & PathCompletionCtx ,
286- e : hir:: Enum ,
287- ) {
288- e. variants ( ctx. db )
289- . into_iter ( )
290- . for_each ( |variant| acc. add_enum_variant ( ctx, path_ctx, variant, None ) ) ;
291- }
0 commit comments