Skip to content

Commit db2771b

Browse files
authored
Merge pull request #1011 from Cassolette/patch-evtemitter-fix
Event emitter support fix
2 parents 01f8679 + 0173906 commit db2771b

File tree

4 files changed

+90
-36
lines changed

4 files changed

+90
-36
lines changed

script/core/completion/completion.lua

+26-23
Original file line numberDiff line numberDiff line change
@@ -1380,34 +1380,37 @@ local function getCallEnumsAndFuncs(source, index, oop, call)
13801380
return
13811381
end
13821382
local results = {}
1383-
if currentIndex == 1 then
1384-
for _, doc in ipairs(class.fields) do
1385-
if doc.field ~= source
1386-
and doc.field[1] == source[1] then
1387-
local eventName = noder.getFieldEventName(doc)
1388-
if eventName then
1389-
results[#results+1] = {
1390-
label = ('%q'):format(eventName),
1391-
description = doc.comment,
1392-
kind = define.CompletionItemKind.EnumMember,
1393-
}
1383+
local valueBeforeIndex = index > 1 and call.args[index - 1][1]
1384+
1385+
for _, doc in ipairs(class.fields) do
1386+
if doc.field ~= source
1387+
and doc.field[1] == source[1] then
1388+
local indexType = currentIndex
1389+
if not oop then
1390+
local args = noder.getFieldArgs(doc)
1391+
-- offset if doc's first arg is `self`
1392+
if args and args[1] and args[1].name[1] == 'self' then
1393+
indexType = indexType - 1
13941394
end
13951395
end
1396-
end
1397-
elseif currentIndex == 2 then
1398-
local myEventName = call.args[index - 1][1]
1399-
for _, doc in ipairs(class.fields) do
1400-
if doc.field ~= source
1401-
and doc.field[1] == source[1] then
1402-
local eventName = noder.getFieldEventName(doc)
1403-
if eventName and eventName == myEventName then
1404-
local docFunc = doc.extends.types[1].args[2].extends.types[1]
1396+
local eventName = noder.getFieldEventName(doc)
1397+
if eventName then
1398+
if indexType == 1 then
14051399
results[#results+1] = {
1406-
label = infer.viewDocFunction(docFunc),
1400+
label = ('%q'):format(eventName),
14071401
description = doc.comment,
1408-
kind = define.CompletionItemKind.Function,
1409-
insertText = buildInsertDocFunction(docFunc),
1402+
kind = define.CompletionItemKind.EnumMember,
14101403
}
1404+
elseif indexType == 2 then
1405+
if eventName == valueBeforeIndex then
1406+
local docFunc = doc.extends.types[1].args[index].extends.types[1]
1407+
results[#results+1] = {
1408+
label = infer.viewDocFunction(docFunc),
1409+
description = doc.comment,
1410+
kind = define.CompletionItemKind.Function,
1411+
insertText = buildInsertDocFunction(docFunc),
1412+
}
1413+
end
14111414
end
14121415
end
14131416
end

script/core/noder.lua

+18-10
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,7 @@ local function getMethodNode(source)
140140
end
141141
end
142142

143-
local function getFieldEventName(field)
144-
if field._eventName then
145-
return field._eventName or nil
146-
end
147-
field._eventName = false
143+
local function getFieldArgs(field)
148144
local fieldType = field.extends
149145
if not fieldType then
150146
return nil
@@ -153,19 +149,29 @@ local function getFieldEventName(field)
153149
if not docFunc or docFunc.type ~= 'doc.type.function' then
154150
return nil
155151
end
156-
local firstArg = docFunc.args and docFunc.args[1]
152+
return docFunc.args
153+
end
154+
155+
local function getFieldEventName(field)
156+
if field._eventName then
157+
return field._eventName or nil
158+
end
159+
field._eventName = false
160+
161+
local args = getFieldArgs(field)
162+
local firstArg = args and args[1]
157163
if not firstArg then
158164
return nil
159165
end
160166
local secondArg
161167
if firstArg.name[1] == 'self' then
162-
firstArg = docFunc.args[2]
168+
firstArg = args[2]
163169
if not firstArg then
164170
return nil
165171
end
166-
secondArg = docFunc.args[3]
172+
secondArg = args[3]
167173
else
168-
secondArg = docFunc.args[2]
174+
secondArg = args[2]
169175
end
170176
if not secondArg then
171177
return
@@ -881,7 +887,8 @@ local function compileCallParam(noders, call, sourceID)
881887
local eventNodeID
882888
for firstIndex, callArg in ipairs(call.args) do
883889
firstIndex = firstIndex - fixIndex
884-
if firstIndex == 1 and callArg.type == 'string' then
890+
if (firstIndex == 1 or firstIndex == 2)
891+
and callArg.type == 'string' then
885892
if callArg[1] then
886893
eventNodeID = sformat('%s%s%s'
887894
, nodeID
@@ -1685,6 +1692,7 @@ function m.eachID(noders)
16851692
return next, noders.source
16861693
end
16871694

1695+
m.getFieldArgs = getFieldArgs
16881696
m.getFieldEventName = getFieldEventName
16891697

16901698
---获取对象的noders

test/completion/common.lua

+35-3
Original file line numberDiff line numberDiff line change
@@ -2640,9 +2640,9 @@ class2:<??>
26402640

26412641
TEST [[
26422642
--- @class Emit
2643-
--- @field on fun(eventName: string, cb: function)
2644-
--- @field on fun(eventName: '"died"', cb: fun(i: integer))
2645-
--- @field on fun(eventName: '"won"', cb: fun(s: string))
2643+
--- @field on fun(self: Emit, eventName: string, cb: function)
2644+
--- @field on fun(self: Emit, eventName: '"died"', cb: fun(i: integer))
2645+
--- @field on fun(self: Emit, eventName: '"won"', cb: fun(s: string))
26462646
local emit = {}
26472647
26482648
emit:on('<??>')
@@ -2656,6 +2656,22 @@ TEST [[
26562656
--- @field on fun(eventName: '"won"', cb: fun(s: string))
26572657
local emit = {}
26582658
2659+
emit.on('died', <??>)
2660+
]]
2661+
{
2662+
[1] = {
2663+
label = 'fun(i: integer)',
2664+
kind = define.CompletionItemKind.Function,
2665+
}
2666+
}
2667+
2668+
TEST [[
2669+
--- @class Emit
2670+
--- @field on fun(self: Emit, eventName: string, cb: function)
2671+
--- @field on fun(self: Emit, eventName: '"died"', cb: fun(i: integer))
2672+
--- @field on fun(self: Emit, eventName: '"won"', cb: fun(s: string))
2673+
local emit = {}
2674+
26592675
emit:on('won', <??>)
26602676
]]
26612677
{
@@ -2665,6 +2681,22 @@ emit:on('won', <??>)
26652681
}
26662682
}
26672683

2684+
TEST [[
2685+
--- @class Emit
2686+
--- @field on fun(self: Emit, eventName: string, cb: function)
2687+
--- @field on fun(self: Emit, eventName: '"died"', cb: fun(i: integer))
2688+
--- @field on fun(self: Emit, eventName: '"won"', cb: fun(s: string))
2689+
local emit = {}
2690+
2691+
emit.on(emit, 'won', <??>)
2692+
]]
2693+
{
2694+
[1] = {
2695+
label = 'fun(s: string)',
2696+
kind = define.CompletionItemKind.Function,
2697+
}
2698+
}
2699+
26682700
TEST [[
26692701
local function f()
26702702
local inferCache

test/type_inference/init.lua

+11
Original file line numberDiff line numberDiff line change
@@ -941,6 +941,17 @@ emit:on("died", function (<?i?>)
941941
end)
942942
]]
943943

944+
TEST 'integer' [[
945+
--- @class Emit
946+
--- @field on fun(self: Emit, eventName: string, cb: function)
947+
--- @field on fun(self: Emit, eventName: '"died"', cb: fun(i: integer))
948+
--- @field on fun(self: Emit, eventName: '"won"', cb: fun(s: string))
949+
local emit = {}
950+
951+
emit.on(self, "died", function (<?i?>)
952+
end)
953+
]]
954+
944955
TEST '👍' [[
945956
---@class 👍
946957
local <?x?>

0 commit comments

Comments
 (0)