Skip to content

Commit f3c650a

Browse files
authored
Merge pull request #840 from xuhuanzy/Generics
fix generic
2 parents 8d82966 + b205b53 commit f3c650a

File tree

2 files changed

+55
-5
lines changed

2 files changed

+55
-5
lines changed

crates/emmylua_code_analysis/src/semantic/generic/instantiate_type/mod.rs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff 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());

crates/emmylua_ls/src/handlers/test/completion_test.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff 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
}

0 commit comments

Comments
 (0)