11use emmylua_code_analysis:: {
2- InferGuard , LuaDeclId , LuaType , infer_table_field_value_should_be, infer_table_should_be,
2+ DbIndex , InferGuard , LuaDeclId , LuaType , get_real_type, infer_table_field_value_should_be,
3+ infer_table_should_be,
34} ;
45use emmylua_parser:: {
56 BinaryOperator , LuaAst , LuaAstNode , LuaAstToken , LuaBlock , LuaLiteralExpr , LuaTokenKind ,
@@ -83,7 +84,10 @@ fn get_token_should_type(builder: &mut CompletionBuilder) -> Option<Vec<LuaType>
8384 . get_db ( )
8485 . get_type_index ( )
8586 . get_type_cache ( & decl_id. into ( ) ) ?;
86- return Some ( vec ! [ decl_type. as_type( ) . clone( ) ] ) ;
87+ let typ = decl_type. as_type ( ) . clone ( ) ;
88+ if contain_function_types ( builder. semantic_model . get_db ( ) , & typ) . is_none ( ) {
89+ return Some ( vec ! [ typ] ) ;
90+ }
8791 }
8892 LuaAst :: LuaAssignStat ( assign_stat) => {
8993 let ( vars, _) = assign_stat. get_var_and_expr_list ( ) ;
@@ -100,7 +104,10 @@ fn get_token_should_type(builder: &mut CompletionBuilder) -> Option<Vec<LuaType>
100104
101105 let var = vars. first ( ) ?;
102106 let var_type = builder. semantic_model . infer_expr ( var. to_expr ( ) ) ;
103- if let Ok ( typ) = var_type {
107+ if let Ok ( typ) = var_type
108+ // this is to avoid repeating function types in completion
109+ && contain_function_types ( builder. semantic_model . get_db ( ) , & typ) . is_none ( )
110+ {
104111 return Some ( vec ! [ typ] ) ;
105112 }
106113 }
@@ -130,3 +137,30 @@ fn get_token_should_type(builder: &mut CompletionBuilder) -> Option<Vec<LuaType>
130137
131138 None
132139}
140+
141+ pub fn contain_function_types ( db : & DbIndex , typ : & LuaType ) -> Option < ( ) > {
142+ match typ {
143+ LuaType :: Union ( union_typ) => {
144+ for member in union_typ. into_vec ( ) . iter ( ) {
145+ match member {
146+ _ if member. is_function ( ) => {
147+ return Some ( ( ) ) ;
148+ }
149+ _ if member. is_custom_type ( ) => {
150+ let real_type = get_real_type ( db, member) ?;
151+ if real_type. is_function ( ) {
152+ return Some ( ( ) ) ;
153+ }
154+ }
155+ _ => {
156+ continue ;
157+ }
158+ }
159+ }
160+
161+ None
162+ }
163+ _ if typ. is_function ( ) => Some ( ( ) ) ,
164+ _ => None ,
165+ }
166+ }
0 commit comments