Skip to content

Commit 4c572b5

Browse files
committed
support alias function document
1 parent bcba283 commit 4c572b5

File tree

1 file changed

+64
-12
lines changed

1 file changed

+64
-12
lines changed

crates/emmylua_ls/src/handlers/signature_helper/build_signature_helper.rs

Lines changed: 64 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
use emmylua_code_analysis::{
22
DbIndex, InFiled, LuaCompilation, LuaFunctionType, LuaGenericType, LuaInstanceType,
3-
LuaOperatorMetaMethod, LuaOperatorOwner, LuaSignatureId, LuaType, LuaTypeDeclId, RenderLevel,
4-
SemanticModel, TypeSubstitutor,
3+
LuaOperatorMetaMethod, LuaOperatorOwner, LuaSemanticDeclId, LuaSignatureId, LuaType,
4+
LuaTypeDeclId, RenderLevel, SemanticModel, TypeSubstitutor,
55
};
66
use emmylua_parser::{LuaAstNode, LuaCallExpr, LuaSyntaxToken, LuaTokenKind};
77
use lsp_types::{
8-
Documentation, MarkupContent, ParameterInformation, ParameterLabel, SignatureHelp,
8+
Documentation, MarkupContent, MarkupKind, ParameterInformation, ParameterLabel, SignatureHelp,
99
SignatureInformation,
1010
};
1111
use rowan::{NodeOrToken, TextRange};
@@ -27,7 +27,7 @@ pub fn build_signature_helper(
2727
let current_idx = get_current_param_index(&call_expr, &token)?;
2828
let help = match prefix_expr_type {
2929
LuaType::DocFunction(func_type) => {
30-
build_doc_function_signature_help(&builder, &func_type, colon_call, current_idx)
30+
build_doc_function_signature_help(&builder, &func_type, colon_call, current_idx, None)
3131
}
3232
LuaType::Signature(signature_id) => {
3333
build_sig_id_signature_help(&builder, signature_id, colon_call, current_idx, false)
@@ -86,6 +86,7 @@ fn build_doc_function_signature_help(
8686
func_type: &LuaFunctionType,
8787
colon_call: bool,
8888
current_idx: usize,
89+
descriotion: Option<String>,
8990
) -> Option<SignatureHelp> {
9091
let semantic_model = builder.semantic_model;
9192
let db = semantic_model.get_db();
@@ -142,9 +143,16 @@ fn build_doc_function_signature_help(
142143
func_type.get_ret(),
143144
);
144145

146+
let documentation = descriotion.map(|description| {
147+
Documentation::MarkupContent(MarkupContent {
148+
kind: MarkupKind::Markdown,
149+
value: description,
150+
})
151+
});
152+
145153
let signature_info = SignatureInformation {
146154
label,
147-
documentation: None,
155+
documentation,
148156
parameters: Some(param_infos),
149157
active_parameter: Some(current_idx as u32),
150158
};
@@ -247,8 +255,13 @@ fn build_sig_id_signature_help(
247255

248256
let mut signatures = vec![signature_info];
249257
for overload in &signature.overloads {
250-
let signature =
251-
build_doc_function_signature_help(builder, overload, colon_call, origin_current_idx);
258+
let signature = build_doc_function_signature_help(
259+
builder,
260+
overload,
261+
colon_call,
262+
origin_current_idx,
263+
None,
264+
);
252265
if let Some(mut signature) = signature {
253266
signature.signatures[0].documentation = builder.description.clone();
254267
signatures.push(signature.signatures[0].clone());
@@ -273,7 +286,20 @@ fn build_type_signature_help(
273286
if let Some(type_decl) = db.get_type_index().get_type_decl(type_decl_id)
274287
&& let Some(LuaType::DocFunction(f)) = type_decl.get_alias_origin(db, None)
275288
{
276-
return build_doc_function_signature_help(builder, &f, colon_call, current_idx);
289+
let semantic_id = LuaSemanticDeclId::TypeDecl(type_decl_id.clone());
290+
let description = db
291+
.get_property_index()
292+
.get_property(&semantic_id)
293+
.and_then(|p| p.description.clone())
294+
.map(|description| *description);
295+
296+
return build_doc_function_signature_help(
297+
builder,
298+
&f,
299+
colon_call,
300+
current_idx,
301+
description,
302+
);
277303
}
278304

279305
let operator_ids = db
@@ -290,6 +316,7 @@ fn build_type_signature_help(
290316
&func_type,
291317
colon_call,
292318
current_idx,
319+
None,
293320
);
294321
}
295322
LuaType::Signature(signature_id) => {
@@ -345,7 +372,13 @@ fn build_table_call_signature_help(
345372
let call_type = operator.get_operator_func(db);
346373
match call_type {
347374
LuaType::DocFunction(func_type) => {
348-
return build_doc_function_signature_help(builder, &func_type, colon_call, current_idx);
375+
return build_doc_function_signature_help(
376+
builder,
377+
&func_type,
378+
colon_call,
379+
current_idx,
380+
None,
381+
);
349382
}
350383
LuaType::Signature(signature_id) => {
351384
return build_sig_id_signature_help(
@@ -373,8 +406,13 @@ fn build_union_type_signature_help(
373406
for typ in union_types {
374407
match typ {
375408
LuaType::DocFunction(func_type) => {
376-
let sig =
377-
build_doc_function_signature_help(builder, &func_type, colon_call, current_idx);
409+
let sig = build_doc_function_signature_help(
410+
builder,
411+
&func_type,
412+
colon_call,
413+
current_idx,
414+
None,
415+
);
378416

379417
if let Some(sig) = sig {
380418
signatures.push(sig.signatures[0].clone());
@@ -530,7 +568,20 @@ fn build_generic_signature_help(
530568
if let Some(type_decl) = db.get_type_index().get_type_decl(type_decl_id) {
531569
let substitutor = TypeSubstitutor::from_type_array(generic_params.clone());
532570
if let Some(LuaType::DocFunction(f)) = type_decl.get_alias_origin(db, Some(&substitutor)) {
533-
return build_doc_function_signature_help(builder, &f, colon_call, current_idx);
571+
let semantic_id = LuaSemanticDeclId::TypeDecl(type_decl_id.clone());
572+
let description = db
573+
.get_property_index()
574+
.get_property(&semantic_id)
575+
.and_then(|p| p.description.clone())
576+
.map(|description| *description);
577+
578+
return build_doc_function_signature_help(
579+
builder,
580+
&f,
581+
colon_call,
582+
current_idx,
583+
description,
584+
);
534585
}
535586
}
536587

@@ -548,6 +599,7 @@ fn build_generic_signature_help(
548599
&func_type,
549600
colon_call,
550601
current_idx,
602+
None,
551603
);
552604
}
553605
LuaType::Signature(signature_id) => {

0 commit comments

Comments
 (0)