Skip to content

Commit d3a1563

Browse files
committed
refactor(hover): no longer special rendering for @return_overload
1 parent 313da4f commit d3a1563

3 files changed

Lines changed: 81 additions & 180 deletions

File tree

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

Lines changed: 14 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,7 @@ use super::{
1010
extract_function_member,
1111
generic::generic_type_substitutor,
1212
get_function_description,
13-
render::{
14-
FunctionRenderContext, build_function_return_overload_rows,
15-
instantiate_call_return_overloads, process_function_type, render_function,
16-
},
13+
render::process_function_type,
1714
};
1815

1916
pub(super) fn build_function_call_hover(
@@ -41,7 +38,7 @@ pub(super) fn build_function_call_hover(
4138
}
4239

4340
for matched_decl in matched_decls {
44-
let info = build_call_hover_function_info(builder, db, matched_decl, call_expr);
41+
let info = build_call_hover_function_info(builder, db, matched_decl);
4542
if let Some(info) = info {
4643
function_infos.push(info);
4744
}
@@ -91,61 +88,19 @@ fn build_call_hover_function_info(
9188
builder: &mut HoverBuilder,
9289
db: &DbIndex,
9390
matched_decl: MatchedCallDecl<'_>,
94-
call_expr: &LuaCallExpr,
9591
) -> Option<HoverFunctionInfo> {
9692
let match_semantic_decl = matched_decl.decl.id();
97-
let match_typ = matched_decl.decl.typ();
9893
let function_member = extract_function_member(db, match_semantic_decl);
99-
let call_func = matched_decl.func;
100-
101-
let contents = if let LuaType::Signature(signature_id) = match_typ {
102-
let signature = db.get_signature_index().get(signature_id)?;
103-
let base_function = signature.to_doc_func_type();
104-
let instantiated_signature = infer_call_generic(
105-
db,
106-
&mut builder.semantic_model.get_cache().borrow_mut(),
107-
&base_function,
108-
call_expr.clone(),
109-
)
110-
.ok()?;
94+
let call_type = LuaType::DocFunction(matched_decl.func);
11195

112-
if !signature.return_overloads.is_empty()
113-
&& call_func.get_async_state() == instantiated_signature.get_async_state()
114-
&& call_func.is_colon_define() == instantiated_signature.is_colon_define()
115-
&& call_func.is_variadic() == instantiated_signature.is_variadic()
116-
&& call_func.get_params() == instantiated_signature.get_params()
117-
{
118-
let return_overloads =
119-
instantiate_call_return_overloads(builder, db, call_expr, signature);
120-
let ret_detail = build_function_return_overload_rows(builder, &return_overloads);
121-
let ctx = FunctionRenderContext {
122-
func: call_func.as_ref(),
123-
semantic_decl: match_semantic_decl,
124-
owner_member: function_member,
125-
return_docs: Vec::new(),
126-
ret_detail: Some(ret_detail),
127-
};
128-
vec![render_function(builder, db, ctx)?]
129-
} else {
130-
process_function_type(
131-
builder,
132-
db,
133-
&LuaType::DocFunction(call_func.clone()),
134-
match_semantic_decl,
135-
function_member,
136-
None,
137-
)?
138-
}
139-
} else {
140-
process_function_type(
141-
builder,
142-
db,
143-
&LuaType::DocFunction(call_func.clone()),
144-
match_semantic_decl,
145-
function_member,
146-
None,
147-
)?
148-
};
96+
let contents = process_function_type(
97+
builder,
98+
db,
99+
&call_type,
100+
match_semantic_decl,
101+
function_member,
102+
None,
103+
)?;
149104

150105
let description = get_function_description(builder, db, match_semantic_decl);
151106
HoverFunctionInfo::from_contents(contents, description)
@@ -187,7 +142,7 @@ fn find_callable_for_call(
187142
let mut visiting_aliases = HashSet::new();
188143
collect_callable_functions(db, decl_type, &mut overloads, &mut visiting_aliases);
189144

190-
overloads.iter().find_map(|func| {
145+
overloads.into_iter().find_map(|func| {
191146
let func = if func.contain_tpl() {
192147
infer_call_generic(
193148
db,
@@ -196,9 +151,9 @@ fn find_callable_for_call(
196151
call_expr.clone(),
197152
)
198153
.map(Arc::new)
199-
.unwrap_or_else(|_| func.clone())
154+
.unwrap_or(func)
200155
} else {
201-
func.clone()
156+
func
202157
};
203158

204159
builder

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

Lines changed: 11 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use std::{collections::HashSet, fmt::Write, sync::Arc};
22

33
use emmylua_code_analysis::{
4-
AsyncState, DbIndex, LuaDocReturnInfo, LuaDocReturnOverloadInfo, LuaFunctionType, LuaMember,
5-
LuaMemberOwner, LuaSemanticDeclId, LuaSignature, LuaType, RenderLevel, VariadicType,
6-
humanize_type, infer_call_generic,
4+
AsyncState, DbIndex, LuaDocReturnInfo, LuaFunctionType, LuaMember, LuaMemberOwner,
5+
LuaSemanticDeclId, LuaSignature, LuaType, RenderLevel, VariadicType, humanize_type,
6+
infer_call_generic,
77
};
88
use emmylua_parser::LuaCallExpr;
99

@@ -21,7 +21,6 @@ pub(super) struct FunctionRenderContext<'a> {
2121
pub semantic_decl: &'a LuaSemanticDeclId,
2222
pub owner_member: Option<&'a LuaMember>,
2323
pub return_docs: Vec<LuaDocReturnInfo>,
24-
pub ret_detail: Option<String>,
2524
}
2625

2726
/// 根据函数类型分派渲染
@@ -41,7 +40,6 @@ pub(super) fn process_function_type(
4140
semantic_decl,
4241
owner_member: function_member,
4342
return_docs: convert_function_return_to_docs(lua_func.as_ref()),
44-
ret_detail: None,
4543
};
4644
let content = render_function(builder, db, ctx)?;
4745
Some(vec![content])
@@ -55,23 +53,13 @@ pub(super) fn process_function_type(
5553
.enumerate()
5654
{
5755
let overload = instantiate_function_for_call(builder, db, overload, call_expr);
58-
// 提前计算 return_docs 和 ret_detail 的差异, 免重复的 hover_doc_function_type 调用
59-
let (return_docs, ret_detail) = if i == 0 && !signature.return_overloads.is_empty()
60-
{
61-
let detail =
62-
build_signature_return_overload_rows(builder, db, signature, call_expr);
63-
(Vec::new(), Some(detail))
64-
} else {
65-
let docs = signature_return_docs(signature, i, overload.as_ref(), call_expr);
66-
(docs, None)
67-
};
56+
let return_docs = signature_return_docs(signature, i, overload.as_ref());
6857

6958
let ctx = FunctionRenderContext {
7059
func: overload.as_ref(),
7160
semantic_decl,
7261
owner_member: function_member,
7362
return_docs,
74-
ret_detail,
7563
};
7664
contents.push(render_function(builder, db, ctx)?);
7765
}
@@ -120,86 +108,21 @@ fn instantiate_function_for_call(
120108
.unwrap_or_else(|_| func.clone())
121109
}
122110

123-
fn build_signature_return_overload_rows(
124-
builder: &mut HoverBuilder,
125-
db: &DbIndex,
126-
signature: &LuaSignature,
127-
call_expr: Option<&LuaCallExpr>,
128-
) -> String {
129-
if let Some(call_expr) = call_expr {
130-
let return_overloads = instantiate_call_return_overloads(builder, db, call_expr, signature);
131-
build_function_return_overload_rows(builder, &return_overloads)
132-
} else {
133-
build_function_return_overload_rows(builder, &signature.return_overloads)
134-
}
135-
}
136-
137111
fn signature_return_docs(
138112
signature: &LuaSignature,
139113
index: usize,
140114
func: &LuaFunctionType,
141-
call_expr: Option<&LuaCallExpr>,
142115
) -> Vec<LuaDocReturnInfo> {
116+
let mut return_docs = convert_function_return_to_docs(func);
143117
if index == 0 && !signature.return_docs.is_empty() {
144-
if call_expr.is_none() {
145-
return signature.return_docs.clone();
118+
for (return_doc, declared_doc) in return_docs.iter_mut().zip(&signature.return_docs) {
119+
return_doc.name = declared_doc.name.clone();
120+
return_doc.description = declared_doc.description.clone();
121+
return_doc.attributes = declared_doc.attributes.clone();
146122
}
147-
148-
let mut return_docs = signature.return_docs.clone();
149-
for (return_doc, inferred_doc) in return_docs
150-
.iter_mut()
151-
.zip(convert_function_return_to_docs(func))
152-
{
153-
return_doc.type_ref = inferred_doc.type_ref;
154-
}
155-
return return_docs;
156123
}
157124

158-
convert_function_return_to_docs(func)
159-
}
160-
161-
pub(super) fn instantiate_call_return_overloads(
162-
builder: &HoverBuilder,
163-
db: &DbIndex,
164-
call_expr: &LuaCallExpr,
165-
signature: &LuaSignature,
166-
) -> Vec<LuaDocReturnOverloadInfo> {
167-
let mut cache = builder.semantic_model.get_cache().borrow_mut();
168-
169-
signature
170-
.return_overloads
171-
.iter()
172-
.map(|row| {
173-
let row_return_type = match row.type_refs.len() {
174-
0 => LuaType::Nil,
175-
1 => row.type_refs[0].clone(),
176-
_ => LuaType::Variadic(VariadicType::Multi(row.type_refs.clone()).into()),
177-
};
178-
let row_function = LuaFunctionType::new(
179-
signature.async_state,
180-
signature.is_colon_define,
181-
signature.is_vararg,
182-
signature.get_type_params(),
183-
row_return_type,
184-
Some(signature.get_function_generic_params()),
185-
);
186-
let type_refs = infer_call_generic(db, &mut cache, &row_function, call_expr.clone())
187-
.ok()
188-
.map(|func| match func.get_ret() {
189-
LuaType::Variadic(variadic) => match variadic.as_ref() {
190-
VariadicType::Multi(types) => types.clone(),
191-
VariadicType::Base(_) => vec![LuaType::Variadic(variadic.clone())],
192-
},
193-
typ => vec![typ.clone()],
194-
})
195-
.unwrap_or_else(|| row.type_refs.clone());
196-
197-
LuaDocReturnOverloadInfo {
198-
type_refs,
199-
description: row.description.clone(),
200-
}
201-
})
202-
.collect()
125+
return_docs
203126
}
204127

205128
/// 渲染单个函数签名的完整 hover 文本
@@ -213,7 +136,6 @@ pub(super) fn render_function(
213136
semantic_decl,
214137
owner_member,
215138
return_docs,
216-
ret_detail,
217139
} = ctx;
218140

219141
let async_label = match func.get_async_state() {
@@ -308,7 +230,7 @@ pub(super) fn render_function(
308230
.filter(|s| !s.is_empty())
309231
.collect::<Vec<_>>();
310232

311-
let ret_detail = ret_detail.unwrap_or_else(|| build_function_returns(builder, return_docs));
233+
let ret_detail = build_function_returns(builder, return_docs);
312234
Some(format_function_type(
313235
type_label,
314236
async_label,
@@ -456,33 +378,6 @@ fn build_function_returns(
456378
result
457379
}
458380

459-
pub(super) fn build_function_return_overload_rows(
460-
builder: &mut HoverBuilder,
461-
return_overloads: &[LuaDocReturnOverloadInfo],
462-
) -> String {
463-
let mut result = String::new();
464-
465-
for (row_idx, row) in return_overloads.iter().enumerate() {
466-
if row.type_refs.is_empty() {
467-
continue;
468-
}
469-
470-
if row_idx == 0 {
471-
result.push('\n');
472-
}
473-
result.push_str(" -> ");
474-
for (i, typ) in row.type_refs.iter().enumerate() {
475-
if i > 0 {
476-
result.push_str(", ");
477-
}
478-
result.push_str(&build_return_type_text(builder, typ, i));
479-
}
480-
result.push('\n');
481-
}
482-
483-
result
484-
}
485-
486381
fn build_return_type_text(builder: &mut HoverBuilder, typ: &LuaType, i: usize) -> String {
487382
let type_expansion_count = builder.get_type_expansion_count();
488383
// 在这个过程中可能会设置`type_expansion`

0 commit comments

Comments
 (0)