@@ -10,7 +10,7 @@ use rowan::TextRange;
1010use smol_str:: SmolStr ;
1111
1212use crate :: {
13- db_index:: { LuaMemberKey , LuaSignatureId } ,
13+ db_index:: { r#type :: type_visit_trait :: TypeVisitTrait , LuaMemberKey , LuaSignatureId } ,
1414 DbIndex , InFiled , SemanticModel ,
1515} ;
1616
@@ -450,12 +450,50 @@ impl LuaType {
450450 }
451451}
452452
453+ impl TypeVisitTrait for LuaType {
454+ fn visit_type < F > ( & self , f : & mut F )
455+ where
456+ F : FnMut ( & LuaType ) ,
457+ {
458+ f ( self ) ;
459+ match self {
460+ LuaType :: Array ( base) => base. visit_type ( f) ,
461+ LuaType :: Tuple ( base) => base. visit_type ( f) ,
462+ LuaType :: DocFunction ( base) => base. visit_type ( f) ,
463+ LuaType :: Object ( base) => base. visit_type ( f) ,
464+ LuaType :: Union ( base) => base. visit_type ( f) ,
465+ LuaType :: Intersection ( base) => base. visit_type ( f) ,
466+ LuaType :: Generic ( base) => base. visit_type ( f) ,
467+ LuaType :: Variadic ( multi) => multi. visit_type ( f) ,
468+ LuaType :: TableGeneric ( params) => {
469+ for param in params. iter ( ) {
470+ param. visit_type ( f) ;
471+ }
472+ }
473+ LuaType :: MultiLineUnion ( inner) => inner. visit_type ( f) ,
474+ LuaType :: TypeGuard ( inner) => inner. visit_type ( f) ,
475+ _ => { }
476+ }
477+ }
478+ }
479+
453480#[ derive( Debug , Clone , Hash , PartialEq , Eq ) ]
454481pub struct LuaTupleType {
455482 types : Vec < LuaType > ,
456483 pub status : LuaTupleStatus ,
457484}
458485
486+ impl TypeVisitTrait for LuaTupleType {
487+ fn visit_type < F > ( & self , f : & mut F )
488+ where
489+ F : FnMut ( & LuaType ) ,
490+ {
491+ for ty in & self . types {
492+ ty. visit_type ( f) ;
493+ }
494+ }
495+ }
496+
459497#[ derive( Debug , Clone , Copy , PartialEq , Eq , Hash ) ]
460498pub enum LuaTupleStatus {
461499 DocResolve ,
@@ -538,6 +576,20 @@ pub struct LuaFunctionType {
538576 ret : LuaType ,
539577}
540578
579+ impl TypeVisitTrait for LuaFunctionType {
580+ fn visit_type < F > ( & self , f : & mut F )
581+ where
582+ F : FnMut ( & LuaType ) ,
583+ {
584+ for ( _, t) in & self . params {
585+ if let Some ( t) = t {
586+ t. visit_type ( f) ;
587+ }
588+ }
589+ self . ret . visit_type ( f) ;
590+ }
591+ }
592+
541593impl LuaFunctionType {
542594 pub fn new (
543595 is_async : bool ,
@@ -649,6 +701,21 @@ pub struct LuaObjectType {
649701 index_access : Vec < ( LuaType , LuaType ) > ,
650702}
651703
704+ impl TypeVisitTrait for LuaObjectType {
705+ fn visit_type < F > ( & self , f : & mut F )
706+ where
707+ F : FnMut ( & LuaType ) ,
708+ {
709+ for t in self . fields . values ( ) {
710+ t. visit_type ( f) ;
711+ }
712+ for ( key, value_type) in & self . index_access {
713+ key. visit_type ( f) ;
714+ value_type. visit_type ( f) ;
715+ }
716+ }
717+ }
718+
652719impl LuaObjectType {
653720 pub fn new ( object_fields : Vec < ( LuaIndexAccessKey , LuaType ) > ) -> Self {
654721 let mut fields = HashMap :: new ( ) ;
@@ -757,6 +824,22 @@ pub enum LuaUnionType {
757824 Multi ( Vec < LuaType > ) ,
758825}
759826
827+ impl TypeVisitTrait for LuaUnionType {
828+ fn visit_type < F > ( & self , f : & mut F )
829+ where
830+ F : FnMut ( & LuaType ) ,
831+ {
832+ match self {
833+ LuaUnionType :: Nullable ( ty) => ty. visit_type ( f) ,
834+ LuaUnionType :: Multi ( types) => {
835+ for ty in types {
836+ ty. visit_type ( f) ;
837+ }
838+ }
839+ }
840+ }
841+ }
842+
760843impl LuaUnionType {
761844 pub fn from_set ( mut set : HashSet < LuaType > ) -> Self {
762845 if set. len ( ) == 2 && set. contains ( & LuaType :: Nil ) {
@@ -872,6 +955,17 @@ pub struct LuaIntersectionType {
872955 types : Vec < LuaType > ,
873956}
874957
958+ impl TypeVisitTrait for LuaIntersectionType {
959+ fn visit_type < F > ( & self , f : & mut F )
960+ where
961+ F : FnMut ( & LuaType ) ,
962+ {
963+ for ty in & self . types {
964+ ty. visit_type ( f) ;
965+ }
966+ }
967+ }
968+
875969impl LuaIntersectionType {
876970 pub fn new ( types : Vec < LuaType > ) -> Self {
877971 Self { types }
@@ -914,6 +1008,17 @@ pub struct LuaAliasCallType {
9141008 operand : Vec < LuaType > ,
9151009}
9161010
1011+ impl TypeVisitTrait for LuaAliasCallType {
1012+ fn visit_type < F > ( & self , f : & mut F )
1013+ where
1014+ F : FnMut ( & LuaType ) ,
1015+ {
1016+ for t in & self . operand {
1017+ t. visit_type ( f) ;
1018+ }
1019+ }
1020+ }
1021+
9171022impl LuaAliasCallType {
9181023 pub fn new ( call_kind : LuaAliasCallKind , operand : Vec < LuaType > ) -> Self {
9191024 Self { call_kind, operand }
@@ -938,6 +1043,17 @@ pub struct LuaGenericType {
9381043 params : Vec < LuaType > ,
9391044}
9401045
1046+ impl TypeVisitTrait for LuaGenericType {
1047+ fn visit_type < F > ( & self , f : & mut F )
1048+ where
1049+ F : FnMut ( & LuaType ) ,
1050+ {
1051+ for param in & self . params {
1052+ param. visit_type ( f) ;
1053+ }
1054+ }
1055+ }
1056+
9411057impl LuaGenericType {
9421058 pub fn new ( base : LuaTypeDeclId , params : Vec < LuaType > ) -> Self {
9431059 Self { base, params }
@@ -976,6 +1092,22 @@ pub enum VariadicType {
9761092 Base ( LuaType ) ,
9771093}
9781094
1095+ impl TypeVisitTrait for VariadicType {
1096+ fn visit_type < F > ( & self , f : & mut F )
1097+ where
1098+ F : FnMut ( & LuaType ) ,
1099+ {
1100+ match self {
1101+ VariadicType :: Multi ( types) => {
1102+ for ty in types {
1103+ ty. visit_type ( f) ;
1104+ }
1105+ }
1106+ VariadicType :: Base ( t) => t. visit_type ( f) ,
1107+ }
1108+ }
1109+ }
1110+
9791111impl VariadicType {
9801112 pub fn get_type ( & self , idx : usize ) -> Option < & LuaType > {
9811113 match self {
@@ -1105,6 +1237,15 @@ pub struct LuaInstanceType {
11051237 range : InFiled < TextRange > ,
11061238}
11071239
1240+ impl TypeVisitTrait for LuaInstanceType {
1241+ fn visit_type < F > ( & self , f : & mut F )
1242+ where
1243+ F : FnMut ( & LuaType ) ,
1244+ {
1245+ self . base . visit_type ( f) ;
1246+ }
1247+ }
1248+
11081249impl LuaInstanceType {
11091250 pub fn new ( base : LuaType , range : InFiled < TextRange > ) -> Self {
11101251 Self { base, range }
@@ -1202,6 +1343,17 @@ pub struct LuaMultiLineUnion {
12021343 unions : Vec < ( LuaType , Option < String > ) > ,
12031344}
12041345
1346+ impl TypeVisitTrait for LuaMultiLineUnion {
1347+ fn visit_type < F > ( & self , f : & mut F )
1348+ where
1349+ F : FnMut ( & LuaType ) ,
1350+ {
1351+ for ( t, _) in & self . unions {
1352+ t. visit_type ( f) ;
1353+ }
1354+ }
1355+ }
1356+
12051357impl LuaMultiLineUnion {
12061358 pub fn new ( unions : Vec < ( LuaType , Option < String > ) > ) -> Self {
12071359 Self { unions }
@@ -1231,6 +1383,15 @@ pub struct LuaArrayType {
12311383 len : LuaArrayLen ,
12321384}
12331385
1386+ impl TypeVisitTrait for LuaArrayType {
1387+ fn visit_type < F > ( & self , f : & mut F )
1388+ where
1389+ F : FnMut ( & LuaType ) ,
1390+ {
1391+ self . base . visit_type ( f) ;
1392+ }
1393+ }
1394+
12341395#[ derive( Debug , Clone , Hash , PartialEq , Eq ) ]
12351396pub enum LuaArrayLen {
12361397 None ,
0 commit comments