Skip to content

Commit b1f4eb5

Browse files
committed
move resolve signature
1 parent 212c2ca commit b1f4eb5

File tree

2 files changed

+91
-81
lines changed

2 files changed

+91
-81
lines changed

crates/emmylua_code_analysis/src/semantic/overload_resolve/mod.rs

Lines changed: 4 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
mod resolve_signature_by_args;
2+
13
use std::{ops::Deref, sync::Arc};
24

35
use emmylua_parser::{LuaCallExpr, LuaExpr};
@@ -12,9 +14,10 @@ use super::{
1214
LuaInferCache,
1315
generic::instantiate_func_generic,
1416
infer::{InferCallFuncResult, InferFailReason},
15-
type_check::check_type_compact,
1617
};
1718

19+
use resolve_signature_by_args::resolve_signature_by_args;
20+
1821
pub fn resolve_signature(
1922
db: &DbIndex,
2023
cache: &mut LuaInferCache,
@@ -65,86 +68,6 @@ fn resolve_signature_by_generic(
6568
)
6669
}
6770

68-
fn resolve_signature_by_args(
69-
db: &DbIndex,
70-
overloads: &[Arc<LuaFunctionType>],
71-
expr_types: &[LuaType],
72-
is_colon_call: bool,
73-
arg_count: Option<usize>,
74-
) -> InferCallFuncResult {
75-
let arg_count = arg_count.unwrap_or(0);
76-
let mut opt_funcs = Vec::with_capacity(overloads.len());
77-
78-
for func in overloads {
79-
let params = func.get_params();
80-
if params.len() < arg_count {
81-
continue;
82-
}
83-
let mut total_weight = 0; // 总权重
84-
85-
let mut fake_expr_len = expr_types.len();
86-
let jump_param;
87-
if is_colon_call && !func.is_colon_define() {
88-
jump_param = 1;
89-
fake_expr_len += 1;
90-
} else {
91-
jump_param = 0;
92-
};
93-
94-
// 如果在不计算可空参数的情况下, 参数数量完全匹配, 则认为其权重更高
95-
if params.len() == fake_expr_len {
96-
total_weight += params.len() as i32 + 1;
97-
}
98-
99-
// 冒号定义且冒号调用
100-
if is_colon_call && func.is_colon_define() {
101-
total_weight += 100;
102-
}
103-
104-
// 检查每个参数的匹配情况
105-
for (i, param) in params.iter().enumerate() {
106-
if i == 0 && jump_param > 0 {
107-
// 非冒号定义但是冒号调用, 直接认为匹配
108-
total_weight += 100;
109-
continue;
110-
}
111-
let param_type = param.1.as_ref().unwrap_or(&LuaType::Any);
112-
let expr_idx = i - jump_param;
113-
114-
if expr_idx >= expr_types.len() {
115-
// 没有传入参数, 但参数是可空类型
116-
if param_type.is_nullable() {
117-
total_weight += 1;
118-
fake_expr_len += 1;
119-
}
120-
continue;
121-
}
122-
123-
let expr_type = &expr_types[expr_idx];
124-
if *param_type == LuaType::Any || check_type_compact(db, param_type, expr_type).is_ok()
125-
{
126-
total_weight += 100; // 类型完全匹配
127-
}
128-
}
129-
// 如果参数数量完全匹配, 则认为其权重更高
130-
if params.len() == fake_expr_len {
131-
total_weight += 50000;
132-
}
133-
134-
opt_funcs.push((func, total_weight));
135-
}
136-
137-
// 按权重降序排序
138-
opt_funcs.sort_by(|a, b| b.1.cmp(&a.1));
139-
// 返回权重最高的签名,若无则取最后一个重载作为默认
140-
opt_funcs
141-
.first()
142-
.filter(|(_, weight)| *weight > i32::MIN) // 确保不是无效签名
143-
.map(|(func, _)| Arc::clone(func))
144-
.or_else(|| overloads.last().cloned())
145-
.ok_or(InferFailReason::None)
146-
}
147-
14871
fn infer_expr_list_types(
14972
db: &DbIndex,
15073
cache: &mut LuaInferCache,
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
use std::sync::Arc;
2+
3+
use crate::{
4+
InferFailReason, check_type_compact,
5+
db_index::{DbIndex, LuaFunctionType, LuaType},
6+
semantic::infer::InferCallFuncResult,
7+
};
8+
9+
pub fn resolve_signature_by_args(
10+
db: &DbIndex,
11+
overloads: &[Arc<LuaFunctionType>],
12+
expr_types: &[LuaType],
13+
is_colon_call: bool,
14+
arg_count: Option<usize>,
15+
) -> InferCallFuncResult {
16+
let arg_count = arg_count.unwrap_or(0);
17+
let mut opt_funcs = Vec::with_capacity(overloads.len());
18+
19+
for func in overloads {
20+
let params = func.get_params();
21+
if params.len() < arg_count {
22+
continue;
23+
}
24+
let mut total_weight = 0; // 总权重
25+
26+
let mut fake_expr_len = expr_types.len();
27+
let jump_param;
28+
if is_colon_call && !func.is_colon_define() {
29+
jump_param = 1;
30+
fake_expr_len += 1;
31+
} else {
32+
jump_param = 0;
33+
};
34+
35+
// 如果在不计算可空参数的情况下, 参数数量完全匹配, 则认为其权重更高
36+
if params.len() == fake_expr_len {
37+
total_weight += params.len() as i32 + 1;
38+
}
39+
40+
// 冒号定义且冒号调用
41+
if is_colon_call && func.is_colon_define() {
42+
total_weight += 100;
43+
}
44+
45+
// 检查每个参数的匹配情况
46+
for (i, param) in params.iter().enumerate() {
47+
if i == 0 && jump_param > 0 {
48+
// 非冒号定义但是冒号调用, 直接认为匹配
49+
total_weight += 100;
50+
continue;
51+
}
52+
let param_type = param.1.as_ref().unwrap_or(&LuaType::Any);
53+
let expr_idx = i - jump_param;
54+
55+
if expr_idx >= expr_types.len() {
56+
// 没有传入参数, 但参数是可空类型
57+
if param_type.is_nullable() {
58+
total_weight += 1;
59+
fake_expr_len += 1;
60+
}
61+
continue;
62+
}
63+
64+
let expr_type = &expr_types[expr_idx];
65+
if *param_type == LuaType::Any || check_type_compact(db, param_type, expr_type).is_ok()
66+
{
67+
total_weight += 100; // 类型完全匹配
68+
}
69+
}
70+
// 如果参数数量完全匹配, 则认为其权重更高
71+
if params.len() == fake_expr_len {
72+
total_weight += 50000;
73+
}
74+
75+
opt_funcs.push((func, total_weight));
76+
}
77+
78+
// 按权重降序排序
79+
opt_funcs.sort_by(|a, b| b.1.cmp(&a.1));
80+
// 返回权重最高的签名,若无则取最后一个重载作为默认
81+
opt_funcs
82+
.first()
83+
.filter(|(_, weight)| *weight > i32::MIN) // 确保不是无效签名
84+
.map(|(func, _)| Arc::clone(func))
85+
.or_else(|| overloads.last().cloned())
86+
.ok_or(InferFailReason::None)
87+
}

0 commit comments

Comments
 (0)