Skip to content

Commit 00f73b2

Browse files
committed
optimize inlay hint
1 parent e3cd3bd commit 00f73b2

File tree

3 files changed

+20
-100
lines changed

3 files changed

+20
-100
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@
2525

2626
`NEW` Support description without '#' on multi union
2727

28+
`NEW` Add standard library translation
29+
30+
`NEW` Optimize inlay hint for parameter, if the parameter name is the same as the variable name, the parameter name will not be displayed
31+
2832
# 0.5.1
2933

3034
`FIX` Fix issue `emmylua_ls` might not exit in unix.

crates/emmylua_code_analysis/src/config/configs/runtime.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,4 @@ mod tests {
9494
let runtime: EmmyrcRuntime = serde_json::from_str(json2).unwrap();
9595
assert_eq!(runtime.version, EmmyrcLuaVersion::Lua51);
9696
}
97-
}
97+
}

crates/emmylua_ls/src/handlers/inlay_hint/build_inlay_hint.rs

Lines changed: 15 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -99,33 +99,17 @@ fn build_call_expr_param_hint(
9999
return Some(());
100100
}
101101

102-
let prefix_expr = call_expr.get_prefix_expr()?;
103-
let semantic_info =
104-
semantic_model.get_semantic_info(NodeOrToken::Node(prefix_expr.syntax().clone()))?;
105-
102+
let func = semantic_model.infer_call_expr_func(call_expr.clone(), None)?;
106103
let call_args_list = call_expr.get_args_list()?;
107104
let colon_call = call_expr.is_colon_call();
108-
match semantic_info.typ {
109-
LuaType::DocFunction(f) => {
110-
build_call_args_for_func_type(
111-
semantic_model,
112-
result,
113-
call_args_list.get_args().collect(),
114-
colon_call,
115-
&f,
116-
);
117-
}
118-
LuaType::Signature(signature_id) => {
119-
build_call_args_for_signature(
120-
semantic_model,
121-
result,
122-
call_args_list.get_args().collect(),
123-
colon_call,
124-
signature_id,
125-
);
126-
}
127-
_ => {}
128-
}
105+
build_call_args_for_func_type(
106+
semantic_model,
107+
result,
108+
call_args_list.get_args().collect(),
109+
colon_call,
110+
&func,
111+
);
112+
129113
Some(())
130114
}
131115

@@ -239,83 +223,15 @@ fn build_call_args_for_func_type(
239223
}
240224

241225
let arg = &call_args[idx];
242-
let range = arg.get_range();
243-
let document = semantic_model.get_document();
244-
let lsp_range = document.to_lsp_range(range)?;
245-
let hint = InlayHint {
246-
kind: Some(InlayHintKind::PARAMETER),
247-
label: InlayHintLabel::String(format!("{}:", name)),
248-
position: lsp_range.start,
249-
text_edits: None,
250-
tooltip: None,
251-
padding_left: None,
252-
padding_right: Some(true),
253-
data: None,
254-
};
255-
result.push(hint);
256-
}
257-
258-
Some(())
259-
}
260-
261-
fn build_call_args_for_signature(
262-
semantic_model: &SemanticModel,
263-
result: &mut Vec<InlayHint>,
264-
call_args: Vec<LuaExpr>,
265-
colon_call: bool,
266-
signature_id: LuaSignatureId,
267-
) -> Option<()> {
268-
let signature = semantic_model
269-
.get_db()
270-
.get_signature_index()
271-
.get(&signature_id)?;
272-
let call_args_len = call_args.len();
273-
let mut params = signature
274-
.get_type_params()
275-
.iter()
276-
.map(|(name, _)| name.clone())
277-
.collect::<Vec<_>>();
278-
279-
let colon_define = signature.is_colon_define;
280-
match (colon_call, colon_define) {
281-
(false, true) => {
282-
params.insert(0, "self".to_string());
283-
}
284-
(true, false) => {
285-
if params.len() > 0 {
286-
params.remove(0);
287-
}
288-
}
289-
_ => {}
290-
}
291-
292-
for (idx, name) in params.iter().enumerate() {
293-
if idx >= call_args_len {
294-
break;
295-
}
296-
297-
if name == "..." {
298-
for i in idx..call_args_len {
299-
let arg = &call_args[i];
300-
let range = arg.get_range();
301-
let document = semantic_model.get_document();
302-
let lsp_range = document.to_lsp_range(range)?;
303-
let hint = InlayHint {
304-
kind: Some(InlayHintKind::PARAMETER),
305-
label: InlayHintLabel::String(format!("var{}:", i - idx)),
306-
position: lsp_range.start,
307-
text_edits: None,
308-
tooltip: None,
309-
padding_left: None,
310-
padding_right: Some(true),
311-
data: None,
312-
};
313-
result.push(hint);
226+
if let LuaExpr::NameExpr(name_expr) = arg {
227+
if let Some(param_name) = name_expr.get_name_text() {
228+
// optimize like rust analyzer
229+
if &param_name == name {
230+
continue;
231+
}
314232
}
315-
break;
316233
}
317234

318-
let arg = &call_args[idx];
319235
let range = arg.get_range();
320236
let document = semantic_model.get_document();
321237
let lsp_range = document.to_lsp_range(range)?;

0 commit comments

Comments
 (0)