Skip to content

fix(hover): use left token when cursor is before '.', ':', '[', ']' #692

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 7, 2025
Merged
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
18 changes: 14 additions & 4 deletions crates/emmylua_ls/src/handlers/hover/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ mod std_hover;
pub use build_hover::build_hover_content_for_completion;
use build_hover::build_semantic_info_hover;
use emmylua_code_analysis::{EmmyLuaAnalysis, FileId};
use emmylua_parser::LuaAstNode;
use emmylua_parser::{LuaAstNode, LuaTokenKind};
pub use find_origin::{find_all_same_named_members, find_member_origin_owner};
pub use hover_builder::HoverBuilder;
pub use hover_humanize::infer_prefix_global_name;
Expand Down Expand Up @@ -56,10 +56,20 @@ pub fn hover(analysis: &EmmyLuaAnalysis, file_id: FileId, position: Position) ->

let token = match root.syntax().token_at_offset(position_offset) {
TokenAtOffset::Single(token) => token,
TokenAtOffset::Between(_, right) => right,
TokenAtOffset::None => {
return None;
TokenAtOffset::Between(left, right) => {
if matches!(
right.kind().into(),
LuaTokenKind::TkDot
| LuaTokenKind::TkColon
| LuaTokenKind::TkLeftBracket
| LuaTokenKind::TkRightBracket
) {
left
} else {
right
}
}
TokenAtOffset::None => return None,
};
match token {
keywords if is_keyword(keywords.clone()) => {
Expand Down
55 changes: 55 additions & 0 deletions crates/emmylua_ls/src/handlers/test/hover_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,4 +310,59 @@ mod tests {
));
Ok(())
}

#[gtest]
fn test_before_dot_returns_object_info() -> Result<()> {
let mut ws = ProviderVirtualWorkspace::new();
ws.def(
r#"
---@class Node
---@field field number?
---@field method fun(self: Node)

---@type Node
node = {}

function node.method() end
"#,
);

check!(ws.check_hover(
r#"
node<??>.field = nil
"#,
VirtualHoverResult {
value: "```lua\n(global) node: Node {\n field: number?,\n method: function,\n}\n```".to_string(),
},
));

check!(ws.check_hover(
r#"
node<??>:method()
"#,
VirtualHoverResult {
value: "```lua\n(global) node: Node {\n field: number?,\n method: function,\n}\n```".to_string(),
},
));

check!(ws.check_hover(
r#"
node<??>["key"] = "value"
"#,
VirtualHoverResult {
value: "```lua\n(global) node: Node {\n field: number?,\n method: function,\n}\n```".to_string(),
},
));

check!(ws.check_hover(
r#"
node["key"<??>] = "value"
"#,
VirtualHoverResult {
value: "\"key\"".to_string(),
},
));

Ok(())
}
}
Loading