Skip to content

Event emitter support fix #1011

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 26 additions & 23 deletions script/core/completion/completion.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1380,34 +1380,37 @@ local function getCallEnumsAndFuncs(source, index, oop, call)
return
end
local results = {}
if currentIndex == 1 then
for _, doc in ipairs(class.fields) do
if doc.field ~= source
and doc.field[1] == source[1] then
local eventName = noder.getFieldEventName(doc)
if eventName then
results[#results+1] = {
label = ('%q'):format(eventName),
description = doc.comment,
kind = define.CompletionItemKind.EnumMember,
}
local valueBeforeIndex = index > 1 and call.args[index - 1][1]

for _, doc in ipairs(class.fields) do
if doc.field ~= source
and doc.field[1] == source[1] then
local indexType = currentIndex
if not oop then
local args = noder.getFieldArgs(doc)
-- offset if doc's first arg is `self`
if args and args[1] and args[1].name[1] == 'self' then
indexType = indexType - 1
end
end
end
elseif currentIndex == 2 then
local myEventName = call.args[index - 1][1]
for _, doc in ipairs(class.fields) do
if doc.field ~= source
and doc.field[1] == source[1] then
local eventName = noder.getFieldEventName(doc)
if eventName and eventName == myEventName then
local docFunc = doc.extends.types[1].args[2].extends.types[1]
local eventName = noder.getFieldEventName(doc)
if eventName then
if indexType == 1 then
results[#results+1] = {
label = infer.viewDocFunction(docFunc),
label = ('%q'):format(eventName),
description = doc.comment,
kind = define.CompletionItemKind.Function,
insertText = buildInsertDocFunction(docFunc),
kind = define.CompletionItemKind.EnumMember,
}
elseif indexType == 2 then
if eventName == valueBeforeIndex then
local docFunc = doc.extends.types[1].args[index].extends.types[1]
results[#results+1] = {
label = infer.viewDocFunction(docFunc),
description = doc.comment,
kind = define.CompletionItemKind.Function,
insertText = buildInsertDocFunction(docFunc),
}
end
end
end
end
Expand Down
28 changes: 18 additions & 10 deletions script/core/noder.lua
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,7 @@ local function getMethodNode(source)
end
end

local function getFieldEventName(field)
if field._eventName then
return field._eventName or nil
end
field._eventName = false
local function getFieldArgs(field)
local fieldType = field.extends
if not fieldType then
return nil
Expand All @@ -153,19 +149,29 @@ local function getFieldEventName(field)
if not docFunc or docFunc.type ~= 'doc.type.function' then
return nil
end
local firstArg = docFunc.args and docFunc.args[1]
return docFunc.args
end

local function getFieldEventName(field)
if field._eventName then
return field._eventName or nil
end
field._eventName = false

local args = getFieldArgs(field)
local firstArg = args and args[1]
if not firstArg then
return nil
end
local secondArg
if firstArg.name[1] == 'self' then
firstArg = docFunc.args[2]
firstArg = args[2]
if not firstArg then
return nil
end
secondArg = docFunc.args[3]
secondArg = args[3]
else
secondArg = docFunc.args[2]
secondArg = args[2]
end
if not secondArg then
return
Expand Down Expand Up @@ -881,7 +887,8 @@ local function compileCallParam(noders, call, sourceID)
local eventNodeID
for firstIndex, callArg in ipairs(call.args) do
firstIndex = firstIndex - fixIndex
if firstIndex == 1 and callArg.type == 'string' then
if (firstIndex == 1 or firstIndex == 2)
and callArg.type == 'string' then
if callArg[1] then
eventNodeID = sformat('%s%s%s'
, nodeID
Expand Down Expand Up @@ -1685,6 +1692,7 @@ function m.eachID(noders)
return next, noders.source
end

m.getFieldArgs = getFieldArgs
m.getFieldEventName = getFieldEventName

---获取对象的noders
Expand Down
38 changes: 35 additions & 3 deletions test/completion/common.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2640,9 +2640,9 @@ class2:<??>

TEST [[
--- @class Emit
--- @field on fun(eventName: string, cb: function)
--- @field on fun(eventName: '"died"', cb: fun(i: integer))
--- @field on fun(eventName: '"won"', cb: fun(s: string))
--- @field on fun(self: Emit, eventName: string, cb: function)
--- @field on fun(self: Emit, eventName: '"died"', cb: fun(i: integer))
--- @field on fun(self: Emit, eventName: '"won"', cb: fun(s: string))
local emit = {}

emit:on('<??>')
Expand All @@ -2656,6 +2656,22 @@ TEST [[
--- @field on fun(eventName: '"won"', cb: fun(s: string))
local emit = {}

emit.on('died', <??>)
]]
{
[1] = {
label = 'fun(i: integer)',
kind = define.CompletionItemKind.Function,
}
}

TEST [[
--- @class Emit
--- @field on fun(self: Emit, eventName: string, cb: function)
--- @field on fun(self: Emit, eventName: '"died"', cb: fun(i: integer))
--- @field on fun(self: Emit, eventName: '"won"', cb: fun(s: string))
local emit = {}

emit:on('won', <??>)
]]
{
Expand All @@ -2665,6 +2681,22 @@ emit:on('won', <??>)
}
}

TEST [[
--- @class Emit
--- @field on fun(self: Emit, eventName: string, cb: function)
--- @field on fun(self: Emit, eventName: '"died"', cb: fun(i: integer))
--- @field on fun(self: Emit, eventName: '"won"', cb: fun(s: string))
local emit = {}

emit.on(emit, 'won', <??>)
]]
{
[1] = {
label = 'fun(s: string)',
kind = define.CompletionItemKind.Function,
}
}

TEST [[
local function f()
local inferCache
Expand Down
11 changes: 11 additions & 0 deletions test/type_inference/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -941,6 +941,17 @@ emit:on("died", function (<?i?>)
end)
]]

TEST 'integer' [[
--- @class Emit
--- @field on fun(self: Emit, eventName: string, cb: function)
--- @field on fun(self: Emit, eventName: '"died"', cb: fun(i: integer))
--- @field on fun(self: Emit, eventName: '"won"', cb: fun(s: string))
local emit = {}

emit.on(self, "died", function (<?i?>)
end)
]]

TEST '👍' [[
---@class 👍
local <?x?>
Expand Down