Skip to content

Commit de16454

Browse files
committed
refactor tpl pattern match function type
1 parent 906b1ba commit de16454

File tree

1 file changed

+31
-18
lines changed

1 file changed

+31
-18
lines changed

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

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

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

1012
#[allow(unused)]
@@ -178,35 +180,46 @@ fn func_tpl_pattern_match(
178180
) -> Option<()> {
179181
match target {
180182
LuaType::DocFunction(target_doc_func) => {
181-
for i in 0..doc_func.get_params().len() {
182-
let param_type = &doc_func.get_params()[i].clone().1?;
183-
let target_param_type = &target_doc_func.get_params().get(i)?.clone().1?;
184-
tpl_pattern_match(db, config, root, param_type, target_param_type, result);
183+
let params = doc_func.get_params();
184+
let target_params = target_doc_func.get_params();
185+
for (i, param_tuple) in params.iter().enumerate() {
186+
let target_param_tuple = target_params.get(i)?;
187+
if param_tuple.1.is_some() && target_param_tuple.1.is_some() {
188+
let param_type = param_tuple.1.clone()?;
189+
let target_param_type = target_param_tuple.1.clone()?;
190+
tpl_pattern_match(db, config, root, &param_type, &target_param_type, result);
191+
}
185192
}
186193

187-
for i in 0..doc_func.get_ret().len() {
188-
let ret_type = &doc_func.get_ret()[i];
189-
let target_ret_type = &target_doc_func.get_ret().get(i)?;
190-
tpl_pattern_match(db, config, root, ret_type, target_ret_type, result);
194+
let rets = doc_func.get_ret();
195+
for (i, ret_type) in rets.iter().enumerate() {
196+
if let Some(target_ret_type) = &target_doc_func.get_ret().get(i) {
197+
tpl_pattern_match(db, config, root, ret_type, target_ret_type, result);
198+
}
191199
}
192200
}
193201
LuaType::Signature(signature_id) => {
202+
let params = doc_func.get_params();
194203
let signature = db.get_signature_index().get(&signature_id)?;
195204

196-
for i in 0..doc_func.get_params().len() {
197-
let param_type = &doc_func.get_params()[i].clone().1?;
198-
let target_param_type = &signature.param_docs.get(&i)?.type_ref;
199-
tpl_pattern_match(db, config, root, param_type, target_param_type, result);
205+
for (i, param_tuple) in params.iter().enumerate() {
206+
let signature_param_info = signature.get_param_info_by_id(i);
207+
if param_tuple.1.is_some() && signature_param_info.is_some() {
208+
let param_type = param_tuple.1.clone()?;
209+
let target_param_type = &signature_param_info.unwrap().type_ref;
210+
tpl_pattern_match(db, config, root, &param_type, target_param_type, result);
211+
}
200212
}
201213

202-
for i in 0..doc_func.get_ret().len() {
203-
let ret_type = &doc_func.get_ret()[i];
204-
let target_ret_type = &signature.return_docs.get(i)?.type_ref;
205-
tpl_pattern_match(db, config, root, ret_type, target_ret_type, result);
214+
let rets = doc_func.get_ret();
215+
for (i, ret_type) in rets.iter().enumerate() {
216+
if let Some(signature_ret_info) = &signature.return_docs.get(i) {
217+
tpl_pattern_match(db, config, root, ret_type, &signature_ret_info.type_ref, result);
218+
}
206219
}
207220
}
208221
_ => {}
209222
}
210223

211224
Some(())
212-
}
225+
}

0 commit comments

Comments
 (0)