From 992a47e4b45796679e9871b451c73c374097e80a Mon Sep 17 00:00:00 2001 From: Marek Langiewicz Date: Sat, 24 Oct 2026 02:23:44 +0200 Subject: [PATCH 1/2] fix(hover): show field docs for members read through a union (#1321) `opt.x` with `---@type P?` (also `P|nil`, `P|Q`, and `---@param opts? P`) hovered as the bare type: `resolve_member` leaves union prefixes to flow narrowing, so the access had no member declaration and hover lost the `(field)` line and the description. Hover now looks the key up through the prefix's union type when the access has no declaration of its own. `resolve_member` stays as it is: resolving union prefixes there breaks 8 flow/cast narrowing tests. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01923KEUKXBrPZHHVvqPVpA1 --- .../src/handlers/hover/build_hover.rs | 22 ++++++- .../src/handlers/test/hover_test.rs | 57 +++++++++++++++++++ 2 files changed, 78 insertions(+), 1 deletion(-) diff --git a/crates/emmylua_ls/src/handlers/hover/build_hover.rs b/crates/emmylua_ls/src/handlers/hover/build_hover.rs index 29b9ef029..59c2a42cc 100644 --- a/crates/emmylua_ls/src/handlers/hover/build_hover.rs +++ b/crates/emmylua_ls/src/handlers/hover/build_hover.rs @@ -53,7 +53,10 @@ pub fn build_semantic_info_hover( token: LuaSyntaxToken, range: TextRange, ) -> Option { - let info = model.semantic_info(token.clone().into())?; + let mut info = model.semantic_info(token.clone().into())?; + if info.decl.is_none() { + info.decl = union_member_at_token(model, &token); + } let member_call_typ = if matches!(info.decl, Some(SemanticId::Member(_))) { let in_call = token .parent_ancestors() @@ -546,6 +549,23 @@ fn table_context_type_for_expr( /// Whether the member access (`x.foo` / `x:foo`) containing the current token is applied to an /// instantiated generic prefix (`local c ---@type MyTable` -> `c.aaa`), in which case the member /// type must be projected through the prefix's generic arguments. +/// Member declaration for `x.key` whose prefix is a union (`P?`, `P|Q`). +/// +/// `resolve_member` leaves union prefixes to flow narrowing, so the access has no declaration +/// of its own; hover still shows the field's signature and docs, like `P.key` does. +fn union_member_at_token(model: &SemanticModel<'_>, token: &LuaSyntaxToken) -> Option { + let index_expr = LuaIndexExpr::cast(token.parent()?)?; + if index_expr.get_index_name_token().as_ref() != Some(token) { + return None; + } + let prefix_ty = model.type_of_expr(index_expr.get_prefix_expr()?.get_syntax_id()); + if !matches!(prefix_ty, LuaType::Union(_)) { + return None; + } + let key = LuaMemberKey::Name(token.text().into()); + model.member_info(&prefix_ty, &key)?.id +} + fn member_prefix_is_generic_instance(model: &SemanticModel<'_>, token: &LuaSyntaxToken) -> bool { let Some(index_expr) = token.parent_ancestors().find_map(LuaIndexExpr::cast) else { return false; diff --git a/crates/emmylua_ls/src/handlers/test/hover_test.rs b/crates/emmylua_ls/src/handlers/test/hover_test.rs index 28727db27..74b281ba7 100644 --- a/crates/emmylua_ls/src/handlers/test/hover_test.rs +++ b/crates/emmylua_ls/src/handlers/test/hover_test.rs @@ -955,4 +955,61 @@ mod tests { }; assert!(content.value.contains("a: integer"), "{}", content.value); } + + /// #1321: a field read through a union (`P?`, `P|nil`, `P|Q`) hovers like a direct field. + #[gtest] + fn test_hover_field_through_union() -> Result<()> { + let mut ws = ProviderVirtualWorkspace::new(); + let prelude = r#" + ---@class P + ---@field x boolean doc of P.x + + ---@class Q + ---@field y boolean doc of Q.y + + ---@type P? + local opt = {} + ---@type P|nil + local pnil = {} + ---@type P|Q + local pq = {} + "#; + let x = "```lua\n(field) x: boolean\n```\n\n---\n\ndoc of P.x".to_string(); + let y = "```lua\n(field) y: boolean\n```\n\n---\n\ndoc of Q.y".to_string(); + for (access, expected) in [ + ("print(opt.x)", &x), + ("print(pnil.x)", &x), + ("print(pq.x)", &x), + ("print(pq.y)", &y), + ] { + check!(ws.check_hover( + &format!("{prelude}\n{access}"), + VirtualHoverResult { + value: expected.clone(), + }, + )); + } + Ok(()) + } + + /// #1321: an optional parameter's fields hover with their docs. + #[gtest] + fn test_hover_field_through_optional_param() -> Result<()> { + let mut ws = ProviderVirtualWorkspace::new(); + check!(ws.check_hover( + r#" + ---@class P + ---@field x boolean doc of P.x + + ---@param opts? P + local function f(opts) + print(opts.x) + end + "#, + VirtualHoverResult { + value: "```lua\n(field) x: boolean\n```\n\n---\n\ndoc of P.x".to_string(), + }, + )); + Ok(()) + } } From b44c70c60d00d82c2cfb347d8f11b0c8a9004eb7 Mon Sep 17 00:00:00 2001 From: Marek Langiewicz Date: Sat, 24 Oct 2026 11:00:00 +0200 Subject: [PATCH 2/2] docs(hover): keep the generic-instance doc on its own function The new `union_member_at_token` was inserted between `member_prefix_is_generic_instance` and its doc comment, so that doc attached to the wrong function. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_014NAm41TqAcVsDEzkrkkEDG --- crates/emmylua_ls/src/handlers/hover/build_hover.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/emmylua_ls/src/handlers/hover/build_hover.rs b/crates/emmylua_ls/src/handlers/hover/build_hover.rs index 59c2a42cc..a310c644c 100644 --- a/crates/emmylua_ls/src/handlers/hover/build_hover.rs +++ b/crates/emmylua_ls/src/handlers/hover/build_hover.rs @@ -546,9 +546,6 @@ fn table_context_type_for_expr( (!matches!(ty, LuaType::Unknown)).then_some(ty) } -/// Whether the member access (`x.foo` / `x:foo`) containing the current token is applied to an -/// instantiated generic prefix (`local c ---@type MyTable` -> `c.aaa`), in which case the member -/// type must be projected through the prefix's generic arguments. /// Member declaration for `x.key` whose prefix is a union (`P?`, `P|Q`). /// /// `resolve_member` leaves union prefixes to flow narrowing, so the access has no declaration @@ -566,6 +563,9 @@ fn union_member_at_token(model: &SemanticModel<'_>, token: &LuaSyntaxToken) -> O model.member_info(&prefix_ty, &key)?.id } +/// Whether the member access (`x.foo` / `x:foo`) containing the current token is applied to an +/// instantiated generic prefix (`local c ---@type MyTable` -> `c.aaa`), in which case the member +/// type must be projected through the prefix's generic arguments. fn member_prefix_is_generic_instance(model: &SemanticModel<'_>, token: &LuaSyntaxToken) -> bool { let Some(index_expr) = token.parent_ancestors().find_map(LuaIndexExpr::cast) else { return false;