Skip to content

Commit b99c855

Browse files
committed
update completion
1 parent e796804 commit b99c855

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

crates/emmylua_ls/src/handlers/completion/providers/function_provider.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ pub fn dispatch_type(
111111
LuaType::ConstTplRef(tpl) => {
112112
add_const_tpl_ref_completion(builder, &tpl.get_tpl_id(), infer_guard);
113113
}
114+
LuaType::TplRef(tpl) => {
115+
add_tpl_ref_completion(builder, &tpl.get_tpl_id(), infer_guard);
116+
}
114117
LuaType::Call(special_call) => {
115118
add_special_call_completion(builder, &special_call);
116119
}
@@ -185,7 +188,11 @@ fn add_union_member_completion(
185188
union_typ: &LuaUnionType,
186189
infer_guard: &InferGuardRef,
187190
) -> Option<()> {
188-
for union_sub_typ in union_typ.into_vec() {
191+
// 如果存在 strtplref, 那么将其移动到最后面
192+
let mut union_types = union_typ.into_vec();
193+
union_types.sort_by_key(|typ| matches!(typ, LuaType::StrTplRef(_)));
194+
195+
for union_sub_typ in union_types {
189196
let name = match union_sub_typ {
190197
LuaType::DocStringConst(s) => to_enum_label(builder, s.as_str()),
191198
LuaType::DocIntegerConst(i) => i.to_string(),
@@ -841,3 +848,13 @@ pub fn get_function_remove_nil(db: &DbIndex, typ: &LuaType) -> Option<LuaType> {
841848
_ => None,
842849
}
843850
}
851+
852+
fn add_tpl_ref_completion(
853+
builder: &mut CompletionBuilder,
854+
tpl_id: &GenericTplId,
855+
infer_guard: &InferGuardRef,
856+
) -> Option<()> {
857+
let extend_type = get_tpl_ref_extend_type(builder, tpl_id)?;
858+
dispatch_type(builder, extend_type, infer_guard);
859+
Some(())
860+
}

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2118,4 +2118,44 @@ mod tests {
21182118
));
21192119
Ok(())
21202120
}
2121+
2122+
#[gtest]
2123+
fn test_generic_extends_completion() -> Result<()> {
2124+
let mut ws = ProviderVirtualWorkspace::new();
2125+
ws.def_file(
2126+
"std.lua",
2127+
r#"
2128+
---@alias std.type
2129+
---| "nil"
2130+
---| "number"
2131+
"#,
2132+
);
2133+
ws.def(
2134+
r#"
2135+
---@generic TP: std.type | table
2136+
---@param tp `TP`|TP
2137+
function is_type(tp)
2138+
end
2139+
"#,
2140+
);
2141+
check!(ws.check_completion_with_kind(
2142+
r#"
2143+
is_type(<??>)
2144+
"#,
2145+
vec![
2146+
VirtualCompletionItem {
2147+
label: "\"nil\"".to_string(),
2148+
kind: CompletionItemKind::ENUM_MEMBER,
2149+
..Default::default()
2150+
},
2151+
VirtualCompletionItem {
2152+
label: "\"number\"".to_string(),
2153+
kind: CompletionItemKind::ENUM_MEMBER,
2154+
..Default::default()
2155+
},
2156+
],
2157+
CompletionTriggerKind::TRIGGER_CHARACTER,
2158+
));
2159+
Ok(())
2160+
}
21212161
}

0 commit comments

Comments
 (0)