Skip to content

Commit 0153829

Browse files
committed
update hover function
感觉需要重构, 太臃肿了
1 parent 9816f3d commit 0153829

File tree

4 files changed

+69
-6
lines changed

4 files changed

+69
-6
lines changed

crates/emmylua_ls/src/handlers/definition/goto_function.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,8 @@ fn handle_signature_match(
173173
}
174174
}
175175

176-
fn extract_semantic_decl_from_signature(
176+
/// 从函数签名中提取其定义的语义ID
177+
pub fn extract_semantic_decl_from_signature(
177178
compilation: &LuaCompilation,
178179
signature_id: &LuaSignatureId,
179180
) -> Option<LuaSemanticDeclId> {

crates/emmylua_ls/src/handlers/definition/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use rowan::TokenAtOffset;
2020
use tokio_util::sync::CancellationToken;
2121

2222
use crate::context::ServerContextSnapshot;
23+
pub use goto_function::extract_semantic_decl_from_signature;
2324

2425
use super::RegisterCapabilities;
2526

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

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,15 @@ use emmylua_code_analysis::{
77
};
88
use emmylua_parser::{LuaAstNode, LuaDocTagField, LuaDocType};
99

10-
use crate::handlers::hover::{
11-
hover_humanize::{
12-
extract_description_from_property_owner, extract_owner_name_from_element,
13-
hover_humanize_type, DescriptionInfo,
10+
use crate::handlers::{
11+
definition::extract_semantic_decl_from_signature,
12+
hover::{
13+
hover_humanize::{
14+
extract_description_from_property_owner, extract_owner_name_from_element,
15+
hover_humanize_type, DescriptionInfo,
16+
},
17+
infer_prefix_global_name, HoverBuilder,
1418
},
15-
infer_prefix_global_name, HoverBuilder,
1619
};
1720

1821
#[derive(Debug, Clone)]
@@ -87,6 +90,41 @@ pub fn hover_function_type(
8790
_ => None,
8891
};
8992

93+
// 如果函数定义来自于其他文件, 我们需要添加原始的注释信息. 参考`test_other_file_function`
94+
if let LuaType::Signature(signature_id) = typ {
95+
if let Some(semantic_id) =
96+
extract_semantic_decl_from_signature(builder.compilation, &signature_id)
97+
{
98+
if semantic_id != *semantic_decl_id {
99+
// signature 的原始定义的描述信息
100+
if let Some(origin_description) = extract_description_from_property_owner(
101+
&builder.semantic_model,
102+
&semantic_id,
103+
) {
104+
match &mut function_info.description {
105+
Some(current_description) => {
106+
// 如果描述不为空, 则合并描述
107+
if let Some(description) = origin_description.description {
108+
if current_description.description.is_none() {
109+
current_description.description = Some(description);
110+
} else {
111+
current_description.description = Some(format!(
112+
"{}\n{}",
113+
current_description.description.take()?,
114+
description
115+
));
116+
}
117+
}
118+
}
119+
None => {
120+
function_info.description = Some(origin_description);
121+
}
122+
}
123+
}
124+
}
125+
}
126+
}
127+
90128
// 如果当前类型是 Union,传入已处理的类型集合
91129
let result = match typ {
92130
LuaType::Union(_) => process_single_function_type_with_exclusions(

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,4 +426,27 @@ mod tests {
426426
},
427427
));
428428
}
429+
#[test]
430+
fn test_other_file_function() {
431+
let mut ws = ProviderVirtualWorkspace::new();
432+
ws.def_file(
433+
"a.lua",
434+
r#"
435+
---测试
436+
local function zipLatest(...)
437+
end
438+
return zipLatest
439+
440+
"#,
441+
);
442+
assert!(ws.check_hover(
443+
r#"
444+
local zipLatest = require("a")
445+
<??>zipLatest()
446+
"#,
447+
VirtualHoverResult {
448+
value: "```lua\nlocal function zipLatest(...)\n```\n\n---\n\n测试".to_string(),
449+
},
450+
));
451+
}
429452
}

0 commit comments

Comments
 (0)