Skip to content

Commit 00dd1ed

Browse files
committed
support enums with runtime values
resolved #1411
1 parent 34d32a4 commit 00dd1ed

File tree

4 files changed

+88
-47
lines changed

4 files changed

+88
-47
lines changed

script/core/completion/completion.lua

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1181,11 +1181,25 @@ local function insertDocEnum(state, pos, doc, enums)
11811181
if not key then
11821182
goto CONTINUE
11831183
end
1184-
if field.value.type == 'integer'
1185-
or field.value.type == 'string' then
1186-
if parentName then
1187-
enums[#enums+1] = {
1188-
label = parentName .. '.' .. key,
1184+
if parentName then
1185+
enums[#enums+1] = {
1186+
label = parentName .. '.' .. key,
1187+
kind = define.CompletionItemKind.EnumMember,
1188+
id = stack(field, function (newField) ---@async
1189+
return {
1190+
detail = buildDetail(newField),
1191+
description = buildDesc(newField),
1192+
}
1193+
end),
1194+
}
1195+
end
1196+
for nd in vm.compileNode(field.value):eachObject() do
1197+
if nd.type == 'boolean'
1198+
or nd.type == 'number'
1199+
or nd.type == 'integer'
1200+
or nd.type == 'string' then
1201+
valueEnums[#valueEnums+1] = {
1202+
label = util.viewLiteral(nd[1]),
11891203
kind = define.CompletionItemKind.EnumMember,
11901204
id = stack(field, function (newField) ---@async
11911205
return {
@@ -1195,16 +1209,6 @@ local function insertDocEnum(state, pos, doc, enums)
11951209
end),
11961210
}
11971211
end
1198-
valueEnums[#valueEnums+1] = {
1199-
label = util.viewLiteral(field.value[1]),
1200-
kind = define.CompletionItemKind.EnumMember,
1201-
id = stack(field, function (newField) ---@async
1202-
return {
1203-
detail = buildDetail(newField),
1204-
description = buildDesc(newField),
1205-
}
1206-
end),
1207-
}
12081212
end
12091213
::CONTINUE::
12101214
end

script/vm/global.lua

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -164,11 +164,9 @@ local function createGlobal(name, cate)
164164
}, mt)
165165
end
166166

167-
---@alias parser.enum string|integer
168-
169167
---@class parser.object
170168
---@field package _globalNode vm.global|false
171-
---@field package _enums? parser.enum[]
169+
---@field package _enums? parser.object[]
172170

173171
---@type table<string, vm.global>
174172
local allGlobals = {}
@@ -378,22 +376,7 @@ local compilerGlobalSwitch = util.switch()
378376
for _, field in ipairs(tbl) do
379377
if field.type == 'tablefield'
380378
or field.type == 'tableindex' then
381-
if not field.value then
382-
goto CONTINUE
383-
end
384-
local key = guide.getKeyName(field)
385-
if not key then
386-
goto CONTINUE
387-
end
388-
if field.value.type == 'integer'
389-
or field.value.type == 'string' then
390-
source._enums[#source._enums+1] = field.value[1]
391-
end
392-
if field.value.type == 'binary'
393-
or field.value.type == 'unary' then
394-
source._enums[#source._enums+1] = vm.getNumber(field.value)
395-
end
396-
::CONTINUE::
379+
source._enums[#source._enums+1] = field
397380
end
398381
end
399382
end)
@@ -546,7 +529,7 @@ function vm.getGlobalNode(source)
546529
end
547530

548531
---@param source parser.object
549-
---@return parser.enum[]?
532+
---@return parser.object[]?
550533
function vm.getEnums(source)
551534
return source._enums
552535
end

script/vm/type.lua

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,22 +53,43 @@ local function checkEnum(parentName, child, uri)
5353
if not parentClass then
5454
return nil
5555
end
56-
for _, set in ipairs(parentClass:getSets(uri)) do
57-
if set.type == 'doc.enum' then
58-
if not vm.getEnums(set) then
59-
return false
56+
local hasEnum
57+
if child.type == 'global' then
58+
---@cast child vm.global
59+
for _, set in ipairs(parentClass:getSets(uri)) do
60+
if set.type == 'doc.enum' then
61+
hasEnum = true
62+
local enums = vm.getEnums(set)
63+
if enums then
64+
for _, enum in ipairs(enums) do
65+
if vm.isSubType(uri, child, vm.compileNode(enum)) then
66+
return true
67+
end
68+
end
69+
end
6070
end
61-
if child.type ~= 'string'
62-
and child.type ~= 'doc.type.string'
63-
and child.type ~= 'integer'
64-
and child.type ~= 'number'
65-
and child.type ~= 'doc.type.integer' then
66-
return false
71+
end
72+
else
73+
---@cast child -vm.global
74+
for _, set in ipairs(parentClass:getSets(uri)) do
75+
if set.type == 'doc.enum' then
76+
hasEnum = true
77+
local myLiteral = vm.getInfer(child):viewLiterals()
78+
local enums = vm.getEnums(set)
79+
if enums then
80+
for _, enum in ipairs(enums) do
81+
if myLiteral == vm.getInfer(enum):viewLiterals() then
82+
return true
83+
end
84+
end
85+
end
6786
end
68-
return util.arrayHas(vm.getEnums(set), child[1])
6987
end
7088
end
7189

90+
if hasEnum then
91+
return false
92+
end
7293
return nil
7394
end
7495

@@ -269,7 +290,6 @@ function vm.isSubType(uri, child, parent, mark)
269290
return isEnum
270291
end
271292

272-
-- TODO: check duck
273293
if parentName == 'table' and not guide.isBasicType(childName) then
274294
return true
275295
end

test/completion/common.lua

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3781,6 +3781,40 @@ f(<??>)
37813781
},
37823782
}
37833783

3784+
TEST [[
3785+
local x = 1
3786+
local y = 2
3787+
3788+
---@enum Enum
3789+
local t = {
3790+
x = x,
3791+
y = y,
3792+
}
3793+
3794+
---@param p Enum
3795+
local function f(p) end
3796+
3797+
f(<??>)
3798+
]]
3799+
{
3800+
{
3801+
label = 't.x',
3802+
kind = define.CompletionItemKind.EnumMember,
3803+
},
3804+
{
3805+
label = 't.y',
3806+
kind = define.CompletionItemKind.EnumMember,
3807+
},
3808+
{
3809+
label = '1',
3810+
kind = define.CompletionItemKind.EnumMember,
3811+
},
3812+
{
3813+
label = '2',
3814+
kind = define.CompletionItemKind.EnumMember,
3815+
},
3816+
}
3817+
37843818
TEST [[
37853819
--
37863820
<??>

0 commit comments

Comments
 (0)