Skip to content

Commit 73dff6a

Browse files
committed
signature: narrow by inputed literal
1 parent 764cc62 commit 73dff6a

File tree

3 files changed

+68
-2
lines changed

3 files changed

+68
-2
lines changed

changelog.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# changelog
22

3+
## 3.6.23
4+
* `CHG` signature: narrow by inputed literal
5+
36
## 3.6.22
47
`2023-6-14`
58
* `FIX` [#2038]

script/core/signature.lua

+37-2
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,39 @@ local function makeOneSignature(source, oop, index)
8989
}
9090
end
9191

92+
local function isEventNotMatch(call, src)
93+
if not call.args or not src.args then
94+
return false
95+
end
96+
local literal, index
97+
for i = 1, 2 do
98+
if not call.args[i] then
99+
break
100+
end
101+
literal = guide.getLiteral(call.args[i])
102+
if literal then
103+
index = i
104+
break
105+
end
106+
end
107+
if not literal then
108+
return false
109+
end
110+
local event = src.args[index]
111+
if not event or event.type ~= 'doc.type.arg' then
112+
return false
113+
end
114+
if not event.extends
115+
or #event.extends.types ~= 1 then
116+
return false
117+
end
118+
local eventLiteral = event.extends.types[1] and guide.getLiteral(event.extends.types[1])
119+
if eventLiteral == nil then
120+
return false
121+
end
122+
return eventLiteral ~= literal
123+
end
124+
92125
---@async
93126
local function makeSignatures(text, call, pos)
94127
local func = call.node
@@ -139,7 +172,8 @@ local function makeSignatures(text, call, pos)
139172
for src in node:eachObject() do
140173
if (src.type == 'function' and not vm.isVarargFunctionWithOverloads(src))
141174
or src.type == 'doc.type.function' then
142-
if not mark[src] then
175+
if not mark[src]
176+
and not isEventNotMatch(call, src) then
143177
mark[src] = true
144178
signs[#signs+1] = makeOneSignature(src, oop, index)
145179
end
@@ -149,7 +183,8 @@ local function makeSignatures(text, call, pos)
149183
if set.type == 'doc.class' then
150184
for _, overload in ipairs(set.calls) do
151185
local f = overload.overload
152-
if not mark[f] then
186+
if not mark[f]
187+
and not isEventNotMatch(call, src) then
153188
mark[f] = true
154189
signs[#signs+1] = makeOneSignature(f, oop, index)
155190
end

test/signature/init.lua

+28
Original file line numberDiff line numberDiff line change
@@ -344,3 +344,31 @@ f(1, 2<??>)
344344
{
345345
'function f(a: 😅, <!b: integer!>)',
346346
}
347+
348+
TEST [[
349+
---@class A
350+
---@field event fun(self: self, ev: "onChat", c: string)
351+
---@field event fun(self: self, ev: "onTimer", t: integer)
352+
353+
---@type A
354+
local t
355+
356+
t:event("onChat", <??>)
357+
]]
358+
{
359+
'(method) (ev: "onChat", <!c: string!>)',
360+
}
361+
362+
TEST [[
363+
---@class A
364+
---@field event fun(self: self, ev: "onChat", c: string)
365+
---@field event fun(self: self, ev: "onTimer", t: integer)
366+
367+
---@type A
368+
local t
369+
370+
t:event("onTimer", <??>)
371+
]]
372+
{
373+
'(method) (ev: "onTimer", <!t: integer!>)',
374+
}

0 commit comments

Comments
 (0)