Skip to content

Commit 221edc8

Browse files
authored
Merge pull request #814 from xuhuanzy/fix
fix
2 parents d97e3b2 + d3e691b commit 221edc8

File tree

5 files changed

+43
-12
lines changed

5 files changed

+43
-12
lines changed

crates/emmylua_code_analysis/src/db_index/signature/signature.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::{
1111
FileId,
1212
db_index::{LuaFunctionType, LuaType},
1313
};
14-
use crate::{SemanticModel, VariadicType};
14+
use crate::{SemanticModel, VariadicType, first_param_may_not_self};
1515

1616
#[derive(Debug)]
1717
pub struct LuaSignature {
@@ -137,11 +137,8 @@ impl LuaSignature {
137137
match owner_type {
138138
Some(owner_type) => {
139139
// 一些类型不应该被视为 method
140-
if let (LuaType::Ref(_) | LuaType::Def(_), _) = (owner_type, param_type)
141-
&& (param_type.is_any()
142-
|| param_type.is_table()
143-
|| param_type.is_class_tpl()
144-
|| param_type.is_str_tpl_ref())
140+
if matches!(owner_type, LuaType::Ref(_) | LuaType::Def(_))
141+
&& first_param_may_not_self(param_type)
145142
{
146143
return false;
147144
}

crates/emmylua_code_analysis/src/db_index/type/mod.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,3 +301,15 @@ fn get_real_type_with_depth<'a>(
301301
_ => Some(typ),
302302
}
303303
}
304+
305+
// 第一个参数是否不应该视为 self
306+
pub fn first_param_may_not_self(typ: &LuaType) -> bool {
307+
if typ.is_table() || matches!(typ, LuaType::Ref(_) | LuaType::Def(_) | LuaType::Any) {
308+
return false;
309+
}
310+
if let LuaType::Union(u) = typ {
311+
return u.into_vec().iter().any(first_param_may_not_self);
312+
}
313+
314+
true
315+
}

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use smol_str::SmolStr;
1212
use crate::{
1313
AsyncState, DbIndex, FileId, InFiled, SemanticModel,
1414
db_index::{LuaMemberKey, LuaSignatureId, r#type::type_visit_trait::TypeVisitTrait},
15+
first_param_may_not_self,
1516
};
1617

1718
use super::{TypeOps, type_decl::LuaTypeDeclId};
@@ -686,11 +687,8 @@ impl LuaFunctionType {
686687
match owner_type {
687688
Some(owner_type) => {
688689
// 一些类型不应该被视为 method
689-
if let (LuaType::Ref(_) | LuaType::Def(_), _) = (owner_type, t)
690-
&& (t.is_any()
691-
|| t.is_table()
692-
|| t.is_class_tpl()
693-
|| t.is_str_tpl_ref())
690+
if matches!(owner_type, LuaType::Ref(_) | LuaType::Def(_))
691+
&& first_param_may_not_self(t)
694692
{
695693
return false;
696694
}

crates/emmylua_ls/src/handlers/hover/function/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,11 @@ fn hover_doc_function_type(
361361
if index == 0 && is_method && !func.is_colon_define() {
362362
"".to_string()
363363
} else if let Some(ty) = &param.1 {
364-
format!("{}: {}", name, humanize_type(db, ty, RenderLevel::Normal))
364+
format!(
365+
"{}: {}",
366+
name,
367+
humanize_type(db, ty, builder.detail_render_level)
368+
)
365369
} else {
366370
name.to_string()
367371
}

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,4 +564,24 @@ mod tests {
564564
));
565565
Ok(())
566566
}
567+
568+
#[gtest]
569+
fn test_fix_method_1() -> Result<()> {
570+
let mut ws = ProviderVirtualWorkspace::new();
571+
check!(ws.check_hover(
572+
r#"
573+
---@class ClassControl
574+
local ClassControl = {}
575+
576+
---@generic T
577+
---@param name `T`|T
578+
function ClassControl.ne<??>w(name)
579+
end
580+
"#,
581+
VirtualHoverResult {
582+
value: "```lua\nfunction ClassControl.new(name: T)\n```".to_string(),
583+
},
584+
));
585+
Ok(())
586+
}
567587
}

0 commit comments

Comments
 (0)