1+ use std:: sync:: Arc ;
2+
13use emmylua_parser:: { LuaAstNode , LuaTableExpr , LuaVarExpr } ;
24
35use crate :: {
46 infer_call_expr_func, infer_expr, infer_member_map, infer_table_should_be, DbIndex ,
57 InferFailReason , InferGuard , LuaDocParamInfo , LuaDocReturnInfo , LuaFunctionType , LuaInferCache ,
6- LuaMemberInfo , LuaSemanticDeclId , LuaSignatureId , LuaType , LuaTypeDeclId ,
7- SignatureReturnStatus ,
8+ LuaMemberInfo , LuaSemanticDeclId , LuaSignatureId , LuaType , LuaTypeDeclId , LuaUnionType ,
9+ SignatureReturnStatus , TypeOps ,
810} ;
911
1012use super :: {
@@ -232,7 +234,6 @@ pub fn try_resolve_closure_parent_params(
232234 }
233235 } ;
234236 self_type = Some ( typ. clone ( ) ) ;
235-
236237 find_best_function_type ( db, cache, & typ, & closure_params. signature_id )
237238 }
238239 _ => return Some ( true ) ,
@@ -277,8 +278,9 @@ pub fn try_resolve_closure_parent_params(
277278 }
278279 } ;
279280
280- let Some ( member_type) = member_type else {
281- return Some ( true ) ;
281+ let member_type = match member_type {
282+ Some ( member_type) => member_type,
283+ None => return Some ( true ) ,
282284 } ;
283285
284286 match & member_type {
@@ -298,6 +300,58 @@ pub fn try_resolve_closure_parent_params(
298300 Some ( true )
299301 }
300302 }
303+ LuaType :: Union ( union_types) => {
304+ let mut final_params = signature. get_type_params ( ) . to_vec ( ) ;
305+ for typ in union_types. get_types ( ) {
306+ let LuaType :: DocFunction ( doc_func) = typ else {
307+ continue ;
308+ } ;
309+ let mut doc_params = doc_func. get_params ( ) . to_vec ( ) ;
310+ match ( doc_func. is_colon_define ( ) , signature. is_colon_define ) {
311+ ( true , true ) | ( false , false ) => { }
312+ ( true , false ) => {
313+ // 原始签名是冒号定义, 但未解析的签名不是冒号定义, 即要插入第一个参数
314+ doc_params. insert ( 0 , ( "self" . to_string ( ) , Some ( LuaType :: SelfInfer ) ) ) ;
315+ }
316+ ( false , true ) => {
317+ // 原始签名不是冒号定义, 但未解析的签名是冒号定义, 即要删除第一个参数
318+ doc_params. remove ( 0 ) ;
319+ }
320+ }
321+ // 如果第一个参数是 self, 则需要将 self 的类型设置为 self_type
322+ if doc_params. get ( 0 ) . map_or ( false , |( _, typ) | match typ {
323+ Some ( LuaType :: SelfInfer ) => true ,
324+ _ => false ,
325+ } ) {
326+ if let Some ( self_type) = & self_type {
327+ doc_params[ 0 ] . 1 = Some ( self_type. clone ( ) ) ;
328+ }
329+ }
330+ for ( idx, param) in doc_params. iter ( ) . enumerate ( ) {
331+ if let Some ( final_param) = final_params. get ( idx) {
332+ if final_param. 0 == "..." {
333+ continue ;
334+ }
335+ let new_type = TypeOps :: Union . apply (
336+ final_param. 1 . as_ref ( ) . unwrap_or ( & LuaType :: Unknown ) ,
337+ param. 1 . as_ref ( ) . unwrap_or ( & LuaType :: Unknown ) ,
338+ ) ;
339+ final_params[ idx] = ( final_param. 0 . clone ( ) , Some ( new_type) ) ;
340+ }
341+ }
342+ }
343+ resolve_doc_function (
344+ db,
345+ closure_params,
346+ & LuaFunctionType :: new (
347+ signature. is_async ,
348+ signature. is_colon_define ,
349+ final_params,
350+ signature. get_return_types ( ) ,
351+ ) ,
352+ self_type,
353+ )
354+ }
301355 _ => Some ( true ) ,
302356 }
303357}
@@ -363,7 +417,6 @@ fn resolve_doc_function(
363417 description : None ,
364418 } ) ;
365419 }
366-
367420 Some ( true )
368421}
369422
@@ -385,47 +438,80 @@ fn find_best_function_type(
385438 prefix_type : & LuaType ,
386439 signature_id : & LuaSignatureId ,
387440) -> Option < LuaType > {
388- let member_info_map = infer_member_map ( db, & prefix_type) ?;
441+ let member_info_map = infer_member_map ( db, prefix_type) ?;
389442 let mut current_type_id = None ;
390- // 如果找不到证明是重定义
443+
391444 let target_infos = member_info_map. into_values ( ) . find ( |infos| {
392- infos. iter ( ) . any ( |info| match & info . typ {
393- LuaType :: Signature ( id) => {
445+ infos. iter ( ) . any ( |info| {
446+ if let LuaType :: Signature ( id) = & info . typ {
394447 if id == signature_id {
395448 current_type_id = get_owner_type_id ( db, info) ;
396449 return true ;
397450 }
398- false
399451 }
400- _ => false ,
452+ false
401453 } )
402454 } ) ?;
403- // 找到第一个具有实际参数类型的签名
404- target_infos. iter ( ) . find_map ( |info| {
405- // 所有者类型一致, 但我们找的是父类型
406- if get_owner_type_id ( db , info ) == current_type_id {
407- return None ;
408- }
455+
456+ let mut current_function_types = Vec :: with_capacity ( target_infos. len ( ) ) ;
457+ // 父类或许也应该返回联合类型
458+ let mut parent_function_type = None ;
459+
460+ for info in target_infos {
409461 let function_type =
410- get_final_function_type ( db, cache, & info. typ ) . unwrap_or ( info. typ . clone ( ) ) ;
411- let param_type_len = match & function_type {
462+ get_final_function_type ( db, cache, & info. typ ) . unwrap_or_else ( || info. typ . clone ( ) ) ;
463+
464+ // 所有者类型一致, 不是父类
465+ if get_owner_type_id ( db, & info) == current_type_id {
466+ match & function_type {
467+ LuaType :: Signature ( id) => {
468+ if let Some ( cur_signature) = db. get_signature_index ( ) . get ( id) {
469+ // 只需要重载声明
470+ if cur_signature. param_docs . is_empty ( ) {
471+ current_function_types. extend ( cur_signature. overloads . iter ( ) . cloned ( ) ) ;
472+ }
473+ }
474+ }
475+ LuaType :: DocFunction ( doc_func) => {
476+ // 使用迭代器优化参数计数
477+ if doc_func. get_params ( ) . iter ( ) . any ( |( _, typ) | typ. is_some ( ) ) {
478+ current_function_types. push ( doc_func. clone ( ) ) ;
479+ }
480+ }
481+ _ => { }
482+ }
483+ continue ;
484+ }
485+
486+ // 父类处理
487+ let has_params = match & function_type {
412488 LuaType :: Signature ( id) => db
413489 . get_signature_index ( )
414- . get ( & id)
415- . map ( |sig| sig. param_docs . len ( ) )
416- . unwrap_or ( 0 ) ,
417- LuaType :: DocFunction ( doc_func) => doc_func
418- . get_params ( )
419- . iter ( )
420- . filter ( |( _, typ) | typ. is_some ( ) )
421- . count ( ) ,
422- _ => 0 , // 跳过其他类型
490+ . get ( id)
491+ . map_or ( false , |sig| !sig. param_docs . is_empty ( ) ) ,
492+ LuaType :: DocFunction ( doc_func) => {
493+ doc_func. get_params ( ) . iter ( ) . any ( |( _, typ) | typ. is_some ( ) )
494+ }
495+ _ => false ,
423496 } ;
424- if param_type_len > 0 {
425- return Some ( function_type. clone ( ) ) ;
497+
498+ if has_params {
499+ parent_function_type = Some ( function_type) ;
426500 }
427- None
428- } )
501+ }
502+ match current_function_types. len ( ) {
503+ 0 => parent_function_type,
504+ 1 => current_function_types
505+ . into_iter ( )
506+ . next ( )
507+ . map ( LuaType :: DocFunction ) ,
508+ _ => Some ( LuaType :: Union ( Arc :: new ( LuaUnionType :: new (
509+ current_function_types
510+ . into_iter ( )
511+ . map ( LuaType :: DocFunction )
512+ . collect ( ) ,
513+ ) ) ) ) ,
514+ }
429515}
430516
431517fn get_final_function_type (
0 commit comments