Skip to content

Commit 3c4e376

Browse files
committed
update hover
1 parent 4bd67b0 commit 3c4e376

File tree

2 files changed

+47
-6
lines changed

2 files changed

+47
-6
lines changed

crates/emmylua_ls/src/handlers/hover/find_origin.rs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use std::collections::HashSet;
22

33
use emmylua_code_analysis::{
4-
LuaCompilation, LuaDeclId, LuaMemberId, LuaSemanticDeclId, LuaType, SemanticDeclLevel,
5-
SemanticModel,
4+
LuaCompilation, LuaDeclId, LuaMemberId, LuaSemanticDeclId, LuaType, LuaUnionType,
5+
SemanticDeclLevel, SemanticModel,
66
};
77
use emmylua_parser::{LuaAssignStat, LuaAstNode, LuaSyntaxKind, LuaTableExpr, LuaTableField};
88

@@ -240,6 +240,21 @@ fn resolve_member_owner(
240240
}
241241
}
242242

243+
// 判断`table`是否为类
244+
fn table_is_class(table_type: &LuaType, depth: usize) -> bool {
245+
if depth > 10 {
246+
return false;
247+
}
248+
match table_type {
249+
LuaType::Ref(_) | LuaType::Def(_) | LuaType::Generic(_) => true,
250+
LuaType::Union(union) => match union.as_ref() {
251+
LuaUnionType::Nullable(t) => table_is_class(t, depth + 1),
252+
LuaUnionType::Multi(ts) => ts.iter().any(|t| table_is_class(t, depth + 1)),
253+
},
254+
_ => false,
255+
}
256+
}
257+
243258
fn resolve_table_field_through_type_inference(
244259
semantic_model: &SemanticModel,
245260
table_field: &LuaTableField,
@@ -248,10 +263,8 @@ fn resolve_table_field_through_type_inference(
248263
let table_expr = LuaTableExpr::cast(parent)?;
249264
let table_type = semantic_model.infer_table_should_be(table_expr)?;
250265

251-
if !matches!(
252-
table_type,
253-
LuaType::Ref(_) | LuaType::Def(_) | LuaType::Generic(_)
254-
) {
266+
// 必须为类我们才搜索其成员
267+
if !table_is_class(&table_type, 0) {
255268
return None;
256269
}
257270

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,4 +413,32 @@ mod tests {
413413

414414
Ok(())
415415
}
416+
417+
#[gtest]
418+
fn test_class_with_nil() -> Result<()> {
419+
let mut ws = ProviderVirtualWorkspace::new();
420+
ws.def(
421+
r#"
422+
---@class A
423+
---@field aAnnotation? string a标签
424+
425+
---@class B
426+
---@field bAnnotation? string b标签
427+
"#,
428+
);
429+
check!(ws.check_hover(
430+
r#"
431+
---@type A|B|nil
432+
local defaultOpt = {
433+
aAnnota<??>tion = "a",
434+
}
435+
"#,
436+
VirtualHoverResult {
437+
value:
438+
"```lua\n(field) aAnnotation: string = \"a\"\n```\n\n---\n\na标签".to_string(),
439+
},
440+
));
441+
442+
Ok(())
443+
}
416444
}

0 commit comments

Comments
 (0)