Skip to content

Commit 467c12e

Browse files
committed
add variadic type
1 parent 6d08bdb commit 467c12e

File tree

5 files changed

+59
-6
lines changed

5 files changed

+59
-6
lines changed

crates/code_analysis/src/compilation/analyzer/doc/infer_type.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ use std::sync::Arc;
22

33
use emmylua_parser::{
44
LuaAst, LuaAstNode, LuaDocBinaryType, LuaDocFuncType, LuaDocGenericType, LuaDocObjectFieldKey,
5-
LuaDocObjectType, LuaDocStrTplType, LuaDocType, LuaDocUnaryType, LuaLiteralToken,
6-
LuaTypeBinaryOperator, LuaTypeUnaryOperator, LuaVarExpr,
5+
LuaDocObjectType, LuaDocStrTplType, LuaDocType, LuaDocUnaryType, LuaDocVariadicType,
6+
LuaLiteralToken, LuaTypeBinaryOperator, LuaTypeUnaryOperator, LuaVarExpr,
77
};
88
use rowan::TextRange;
99
use smol_str::SmolStr;
@@ -99,8 +99,10 @@ pub fn infer_type(analyzer: &mut DocAnalyzer, node: LuaDocType) -> LuaType {
9999
LuaDocType::StrTpl(str_tpl) => {
100100
return infer_str_tpl(analyzer, str_tpl);
101101
}
102+
LuaDocType::Variadic(variadic_type) => {
103+
return infer_variadic_type(analyzer, variadic_type).unwrap_or(LuaType::Unknown);
104+
}
102105
_ => {} // LuaDocType::Conditional(lua_doc_conditional_type) => todo!(),
103-
// LuaDocType::Variadic(lua_doc_variadic_type) => todo!(),
104106
}
105107
LuaType::Unknown
106108
}
@@ -413,3 +415,11 @@ fn infer_str_tpl(analyzer: &mut DocAnalyzer, str_tpl: LuaDocStrTplType) -> LuaTy
413415
}
414416
LuaType::Unknown
415417
}
418+
419+
fn infer_variadic_type(analyzer: &mut DocAnalyzer, variadic_type: LuaDocVariadicType) -> Option<LuaType> {
420+
let name_type = variadic_type.get_name_type()?;
421+
let name = name_type.get_name_text()?;
422+
let base = infer_buildin_or_ref_type(analyzer, &name, name_type.get_range());
423+
424+
Some(LuaType::Variadic(base.into()))
425+
}

crates/code_analysis/src/db_index/type/humanize_type.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ pub fn humanize_type(db: &DbIndex, ty: &LuaType) -> String {
5858
LuaType::Instance(ins) => humanize_instance_type(db, ins),
5959
LuaType::Signature(signature_id) => humanize_signature_type(db, signature_id),
6060
LuaType::Namespace(ns) => format!("{{ {} }}", ns),
61+
LuaType::Variadic(inner) => format!("{}...", humanize_type(db, inner)),
6162
_ => "unknown".to_string(),
6263
}
6364
}

crates/code_analysis/src/db_index/type/types.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ pub enum LuaType {
5656
DocStringConst(ArcIntern<SmolStr>),
5757
DocIntegerConst(i64),
5858
Namespace(ArcIntern<SmolStr>),
59+
Variadic(Arc<LuaType>),
5960
}
6061

6162
impl PartialEq for LuaType {
@@ -104,6 +105,7 @@ impl PartialEq for LuaType {
104105
(LuaType::DocStringConst(a), LuaType::DocStringConst(b)) => a == b,
105106
(LuaType::DocIntegerConst(a), LuaType::DocIntegerConst(b)) => a == b,
106107
(LuaType::Namespace(a), LuaType::Namespace(b)) => a == b,
108+
(LuaType::Variadic(a), LuaType::Variadic(b)) => a == b,
107109
_ => false, // 不同变体之间不相等
108110
}
109111
}
@@ -178,6 +180,7 @@ impl Hash for LuaType {
178180
LuaType::DocStringConst(a) => (40, a).hash(state),
179181
LuaType::DocIntegerConst(a) => (41, a).hash(state),
180182
LuaType::Namespace(a) => (42, a).hash(state),
183+
LuaType::Variadic(a) => (43, a).hash(state),
181184
}
182185
}
183186
}
@@ -366,6 +369,7 @@ impl LuaType {
366369
LuaType::MuliReturn(multi) => multi.contain_tpl(),
367370
LuaType::ExistField(field) => field.contain_tpl(),
368371
LuaType::TableGeneric(params) => params.iter().any(|p| p.contain_tpl()),
372+
LuaType::Variadic(inner) => inner.contain_tpl(),
369373
LuaType::TplRef(_) => true,
370374
LuaType::StrTplRef(_) => true,
371375
LuaType::FuncTplRef(_) => true,
@@ -376,6 +380,10 @@ impl LuaType {
376380
pub fn is_namespace(&self) -> bool {
377381
matches!(self, LuaType::Namespace(_))
378382
}
383+
384+
pub fn is_variadic(&self) -> bool {
385+
matches!(self, LuaType::Variadic(_))
386+
}
379387
}
380388

381389
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
@@ -704,6 +712,13 @@ impl LuaMultiReturn {
704712
}
705713
}
706714

715+
pub fn get_len(&self) -> usize {
716+
match self {
717+
LuaMultiReturn::Multi(types) => types.len(),
718+
LuaMultiReturn::Base(_) => 1,
719+
}
720+
}
721+
707722
pub fn contain_tpl(&self) -> bool {
708723
match self {
709724
LuaMultiReturn::Multi(types) => types.iter().any(|t| t.contain_tpl()),

crates/code_analysis/src/semantic/infer/infer_call.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::sync::Arc;
1+
use std::{ops::Deref, sync::Arc};
22

33
use emmylua_parser::{LuaAstNode, LuaCallExpr, LuaExpr, LuaSyntaxKind};
44

@@ -334,6 +334,9 @@ fn unwrapp_return_type(
334334

335335
return multi.get_type(0).cloned();
336336
}
337+
LuaType::Variadic(inner) => {
338+
return Some(inner.deref().clone());
339+
}
337340
LuaType::SelfInfer => {
338341
let prefix_expr = call_expr.get_prefix_expr();
339342
if let Some(prefix_expr) = prefix_expr {

crates/code_analysis/src/semantic/instantiate/tpl_pattern.rs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ use emmylua_parser::{LuaAstNode, LuaSyntaxId, LuaSyntaxNode, LuaTableExpr};
44
use smol_str::SmolStr;
55

66
use crate::{
7-
db_index::{DbIndex, LuaGenericType, LuaType},
8-
semantic::{infer_expr, LuaInferConfig}, LuaUnionType,
7+
db_index::{DbIndex, LuaGenericType, LuaType}, semantic::{infer_expr, LuaInferConfig}, LuaFunctionType, LuaUnionType
98
};
109

1110
#[allow(unused)]
@@ -48,6 +47,9 @@ pub fn tpl_pattern_match(
4847
LuaType::Union(union) => {
4948
union_tpl_pattern_match(db, config, root, union, target, result);
5049
}
50+
LuaType::DocFunction(doc_func) => {
51+
func_tpl_pattern_match(db, config, root, doc_func, target, result);
52+
}
5153
_ => {}
5254
}
5355

@@ -162,5 +164,27 @@ fn union_tpl_pattern_match(
162164
tpl_pattern_match(db, config, root, u, target, result);
163165
}
164166

167+
Some(())
168+
}
169+
170+
#[allow(unused)]
171+
fn func_tpl_pattern_match(
172+
db: &DbIndex,
173+
config: &mut LuaInferConfig,
174+
root: &LuaSyntaxNode,
175+
doc_func: &LuaFunctionType,
176+
target: &LuaType,
177+
result: &mut HashMap<usize, LuaType>,
178+
) -> Option<()> {
179+
match target {
180+
LuaType::DocFunction(target_doc_func) => {
181+
// todo
182+
}
183+
LuaType::Signature(signature_id) => {
184+
// todo
185+
}
186+
_ => {}
187+
}
188+
165189
Some(())
166190
}

0 commit comments

Comments
 (0)