Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion crates/emmylua_ls/src/handlers/hover/build_hover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ pub fn build_semantic_info_hover(
token: LuaSyntaxToken,
range: TextRange,
) -> Option<Hover> {
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()
Expand Down Expand Up @@ -544,6 +547,23 @@ fn table_context_type_for_expr(
(!matches!(ty, LuaType::Unknown)).then_some(ty)
}

/// 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<SemanticId> {
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
}

/// Whether the member access (`x.foo` / `x:foo`) containing the current token is applied to an
/// instantiated generic prefix (`local c ---@type MyTable<int>` -> `c.aaa`), in which case the member
/// type must be projected through the prefix's generic arguments.
Expand Down
57 changes: 57 additions & 0 deletions crates/emmylua_ls/src/handlers/test/hover_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1103,4 +1103,61 @@ mod tests {
));
Ok(())
}

/// #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(())
}
}
Loading