Skip to content

Commit 6335bda

Browse files
committed
update hover
1 parent 4bd67b0 commit 6335bda

File tree

2 files changed

+45
-4
lines changed

2 files changed

+45
-4
lines changed

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

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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) => union
251+
.into_vec()
252+
.iter()
253+
.any(|typ| table_is_class(typ, depth + 1)),
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)