File tree Expand file tree Collapse file tree 2 files changed +55
-5
lines changed
emmylua_code_analysis/src/semantic/generic/instantiate_type
emmylua_ls/src/handlers/test Expand file tree Collapse file tree 2 files changed +55
-5
lines changed Original file line number Diff line number Diff line change @@ -837,12 +837,28 @@ fn instantiate_mapped_type(
837837 }
838838
839839 if !fields. is_empty ( ) || !index_access. is_empty ( ) {
840+ // key 从 0 开始递增才被视为元组
840841 if constraint. is_tuple ( ) {
841- let types = fields. into_iter ( ) . map ( |( _, ty) | ty) . collect ( ) ;
842- // return LuaType::Variadic(VariadicType::Multi(types).into());
843- return LuaType :: Tuple (
844- LuaTupleType :: new ( types, LuaTupleStatus :: InferResolve ) . into ( ) ,
845- ) ;
842+ let mut index = 0 ;
843+ let mut is_tuple = true ;
844+ for ( key, _) in & fields {
845+ if let LuaMemberKey :: Integer ( i) = key {
846+ if * i != index {
847+ is_tuple = false ;
848+ break ;
849+ }
850+ index += 1 ;
851+ } else {
852+ is_tuple = false ;
853+ break ;
854+ }
855+ }
856+ if is_tuple {
857+ let types = fields. into_iter ( ) . map ( |( _, ty) | ty) . collect ( ) ;
858+ return LuaType :: Tuple (
859+ LuaTupleType :: new ( types, LuaTupleStatus :: InferResolve ) . into ( ) ,
860+ ) ;
861+ }
846862 }
847863 let field_map: HashMap < LuaMemberKey , LuaType > = fields. into_iter ( ) . collect ( ) ;
848864 return LuaType :: Object ( LuaObjectType :: new_with_fields ( field_map, index_access) . into ( ) ) ;
Original file line number Diff line number Diff line change @@ -2158,4 +2158,38 @@ mod tests {
21582158 ) ) ;
21592159 Ok ( ( ) )
21602160 }
2161+
2162+ #[ gtest]
2163+ fn test_generic_partial ( ) -> Result < ( ) > {
2164+ let mut ws = ProviderVirtualWorkspace :: new ( ) ;
2165+ ws. def (
2166+ r#"
2167+ ---@alias Partial<T> { [P in keyof T]?: T[P]; }
2168+ "# ,
2169+ ) ;
2170+ check ! ( ws. check_completion(
2171+ r#"
2172+ ---@class AA
2173+ ---@field a string
2174+ ---@field b number
2175+
2176+ ---@type Partial<AA>
2177+ local a = {}
2178+ a.<??>
2179+ "# ,
2180+ vec![
2181+ VirtualCompletionItem {
2182+ label: "a" . to_string( ) ,
2183+ kind: CompletionItemKind :: VARIABLE ,
2184+ ..Default :: default ( )
2185+ } ,
2186+ VirtualCompletionItem {
2187+ label: "b" . to_string( ) ,
2188+ kind: CompletionItemKind :: VARIABLE ,
2189+ ..Default :: default ( )
2190+ }
2191+ ] ,
2192+ ) ) ;
2193+ Ok ( ( ) )
2194+ }
21612195}
You can’t perform that action at this time.
0 commit comments