Skip to content

Commit 0989890

Browse files
authored
Merge pull request #828 from xuhuanzy/fix
fix hover
2 parents 2491155 + 1ff1419 commit 0989890

File tree

3 files changed

+101
-17
lines changed

3 files changed

+101
-17
lines changed

crates/emmylua_ls/src/handlers/hover/function/mod.rs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,12 @@ fn hover_doc_function_type(
313313
builder.semantic_model,
314314
Some(&LuaType::Ref(type_decl_id.clone())),
315315
);
316+
if is_method {
317+
type_label = "(method) ";
318+
name.push(':');
319+
} else {
320+
name.push('.');
321+
}
316322
}
317323
LuaMemberOwner::Element(element_id) => {
318324
if let Some(LuaType::Ref(type_decl_id) | LuaType::Def(type_decl_id)) =
@@ -326,22 +332,23 @@ fn hover_doc_function_type(
326332
builder.semantic_model,
327333
Some(&LuaType::Ref(type_decl_id.clone())),
328334
);
335+
if is_method {
336+
type_label = "(method) ";
337+
name.push(':');
338+
} else {
339+
name.push('.');
340+
}
329341
} else if let Some(owner_name) =
330342
extract_owner_name_from_element(builder.semantic_model, element_id)
331343
{
332344
name.push_str(&owner_name);
345+
name.push('.');
333346
}
334347
}
335348
_ => {}
336349
}
337350
}
338351

339-
if is_method {
340-
type_label = "(method) ";
341-
name.push(':');
342-
} else {
343-
name.push('.');
344-
}
345352
if let LuaMemberKey::Name(n) = owner_member.get_key() {
346353
name.push_str(n.as_str());
347354
}
@@ -361,11 +368,7 @@ fn hover_doc_function_type(
361368
if index == 0 && is_method && !func.is_colon_define() {
362369
"".to_string()
363370
} else if let Some(ty) = &param.1 {
364-
format!(
365-
"{}: {}",
366-
name,
367-
humanize_type(db, ty, builder.detail_render_level)
368-
)
371+
format!("{}: {}", name, humanize_type(db, ty, RenderLevel::Simple))
369372
} else {
370373
name.to_string()
371374
}

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

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use emmylua_code_analysis::{
77
use emmylua_code_analysis::humanize_type;
88
use emmylua_parser::{
99
LuaAstNode, LuaExpr, LuaIndexExpr, LuaStat, LuaSyntaxId, LuaSyntaxKind, LuaTableExpr,
10+
LuaVarExpr,
1011
};
1112
use rowan::TextRange;
1213

@@ -232,14 +233,34 @@ pub fn extract_owner_name_from_element(
232233
// 通过 TextRange 找到对应的 AST 节点
233234
let node = LuaSyntaxId::to_node_at_range(&root, element_id.value)?;
234235
let stat = LuaStat::cast(node.clone().parent()?)?;
235-
if let LuaStat::LocalStat(local_stat) = stat {
236-
let value = LuaExpr::cast(node)?;
237-
let local_name = local_stat.get_local_name_by_value(value);
238-
if let Some(local_name) = local_name {
239-
return Some(local_name.get_name_token()?.get_name_text().to_string());
236+
match stat {
237+
LuaStat::LocalStat(local_stat) => {
238+
let value = LuaExpr::cast(node)?;
239+
let local_name = local_stat.get_local_name_by_value(value);
240+
if let Some(local_name) = local_name {
241+
return Some(local_name.get_name_token()?.get_name_text().to_string());
242+
}
240243
}
244+
LuaStat::AssignStat(assign_stat) => {
245+
let value = LuaExpr::cast(node)?;
246+
let (vars, values) = assign_stat.get_var_and_expr_list();
247+
let idx = values
248+
.iter()
249+
.position(|v| v.get_syntax_id() == value.get_syntax_id())?;
250+
let var = vars.get(idx)?;
251+
match var {
252+
LuaVarExpr::NameExpr(name_expr) => {
253+
return Some(name_expr.get_name_token()?.get_name_text().to_string());
254+
}
255+
LuaVarExpr::IndexExpr(index_expr) => {
256+
if let Some(index_key) = index_expr.get_index_key() {
257+
return Some(index_key.get_path_part());
258+
}
259+
}
260+
}
261+
}
262+
_ => {}
241263
}
242-
243264
None
244265
}
245266

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

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,4 +584,64 @@ mod tests {
584584
));
585585
Ok(())
586586
}
587+
588+
#[gtest]
589+
fn test_fix_global_index_function_1() -> Result<()> {
590+
let mut ws = ProviderVirtualWorkspace::new();
591+
check!(ws.check_hover(
592+
r#"
593+
M = {}
594+
function M.te<??>st()
595+
end
596+
597+
"#,
598+
VirtualHoverResult {
599+
value: "```lua\nfunction M.test()\n```".to_string(),
600+
},
601+
));
602+
Ok(())
603+
}
604+
605+
#[gtest]
606+
fn test_fix_global_index_function_2() -> Result<()> {
607+
let mut ws = ProviderVirtualWorkspace::new();
608+
// TODO: 构建完整的访问路径
609+
check!(ws.check_hover(
610+
r#"
611+
M = {
612+
K = {}
613+
}
614+
M.K.<??>Value = function()
615+
end
616+
"#,
617+
VirtualHoverResult {
618+
value: "```lua\nfunction Value()\n```".to_string(),
619+
},
620+
));
621+
Ok(())
622+
}
623+
624+
#[gtest]
625+
fn test_fix_ref() -> Result<()> {
626+
let mut ws = ProviderVirtualWorkspace::new();
627+
ws.def(
628+
r#"
629+
---@class Player
630+
---@field name string
631+
632+
---@param player Player
633+
function CreatePlayer(player)
634+
end
635+
"#,
636+
);
637+
check!(ws.check_hover(
638+
r#"
639+
Creat<??>ePlayer({name = "John"})
640+
"#,
641+
VirtualHoverResult {
642+
value: "```lua\nfunction CreatePlayer(player: Player)\n```".to_string(),
643+
},
644+
));
645+
Ok(())
646+
}
587647
}

0 commit comments

Comments
 (0)