Skip to content

Commit 0c7a22d

Browse files
committed
ignore varargs
if a function only has varargs and has `---@overload`, the varargs will be ignored resolve #1641
1 parent 1c79bbc commit 0c7a22d

File tree

9 files changed

+155
-9
lines changed

9 files changed

+155
-9
lines changed

changelog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ server will generate `doc.json` and `doc.md` in `LOGPATH`.
3636
* `redundant-return-value`
3737
* `return-type-mismatch`
3838
* `CHG` workspace-symbol: supports chain fields based on global variables and types. try `io.open` or `iolib.open`
39+
* `CHG` [#1641] if a function only has varargs and has `---@overload`, the varargs will be ignored
3940
* `FIX` [#1567]
4041
* `FIX` [#1593]
4142
* `FIX` [#1595]
@@ -59,6 +60,7 @@ server will generate `doc.json` and `doc.md` in `LOGPATH`.
5960
[#1608]: https://github.com/sumneko/lua-language-server/issues/1608
6061
[#1626]: https://github.com/sumneko/lua-language-server/issues/1626
6162
[#1637]: https://github.com/sumneko/lua-language-server/issues/1637
63+
[#1641]: https://github.com/sumneko/lua-language-server/issues/1641
6264
[#1642]: https://github.com/sumneko/lua-language-server/issues/1642
6365

6466
## 3.5.6

script/core/completion/completion.lua

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ local function checkLocal(state, word, position, results)
315315
return orders[a] < orders[b]
316316
end)
317317
for _, def in ipairs(defs) do
318-
if def.type == 'function'
318+
if (def.type == 'function' and not vm.isVarargFunctionWithOverloads(def))
319319
or def.type == 'doc.type.function' then
320320
local funcLabel = name .. getParams(def, false)
321321
buildFunction(results, source, def, false, {
@@ -490,7 +490,7 @@ end
490490
local function checkFieldThen(state, name, src, word, startPos, position, parent, oop, results)
491491
local value = vm.getObjectFunctionValue(src) or src
492492
local kind = define.CompletionItemKind.Field
493-
if value.type == 'function'
493+
if (value.type == 'function' and not vm.isVarargFunctionWithOverloads(value))
494494
or value.type == 'doc.type.function' then
495495
if oop then
496496
kind = define.CompletionItemKind.Method
@@ -572,9 +572,11 @@ local function checkFieldOfRefs(refs, state, word, startPos, position, parent, o
572572
local value = vm.getObjectFunctionValue(src) or src
573573
if value.type == 'function'
574574
or value.type == 'doc.type.function' then
575-
funcLabel = name .. getParams(value, oop)
576-
fields[funcLabel] = src
577-
count = count + 1
575+
if not vm.isVarargFunctionWithOverloads(value) then
576+
funcLabel = name .. getParams(value, oop)
577+
fields[funcLabel] = src
578+
count = count + 1
579+
end
578580
if value.type == 'function' and value.bindDocs then
579581
for _, doc in ipairs(value.bindDocs) do
580582
if doc.type == 'doc.overload' then

script/core/hover/init.lua

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,12 @@ local function getHover(source)
6060
if guide.isOOP(def) then
6161
oop = true
6262
end
63-
if def.type == 'function'
64-
or def.type == 'doc.type.function' then
63+
if def.type == 'function'
64+
and not vm.isVarargFunctionWithOverloads(def) then
65+
hasFunc = true
66+
addHover(def, true, oop)
67+
end
68+
if def.type == 'doc.type.function' then
6569
hasFunc = true
6670
addHover(def, true, oop)
6771
end

script/core/signature.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ local function makeSignatures(text, call, pos)
137137
node = node:getData 'originNode' or node
138138
local mark = {}
139139
for src in node:eachObject() do
140-
if src.type == 'function'
140+
if (src.type == 'function' and not vm.isVarargFunctionWithOverloads(src))
141141
or src.type == 'doc.type.function' then
142142
if not mark[src] then
143143
mark[src] = true

script/vm/doc.lua

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,13 @@ local function getDeprecated(value)
112112
end
113113
end
114114
end
115+
if value.type == 'function' then
116+
local doc = getDeprecated(value.parent)
117+
if doc then
118+
value._deprecated = doc
119+
return doc
120+
end
121+
end
115122
value._deprecated = false
116123
return nil
117124
end

script/vm/function.lua

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ function vm.getMatchedFunctions(func, args, mark)
218218
local funcs = {}
219219
local node = vm.compileNode(func)
220220
for n in node:eachObject() do
221-
if n.type == 'function'
221+
if (n.type == 'function' and not vm.isVarargFunctionWithOverloads(n))
222222
or n.type == 'doc.type.function' then
223223
funcs[#funcs+1] = n
224224
end
@@ -243,3 +243,26 @@ function vm.getMatchedFunctions(func, args, mark)
243243
return matched
244244
end
245245
end
246+
247+
---@param func table
248+
---@return boolean
249+
function vm.isVarargFunctionWithOverloads(func)
250+
if func.type ~= 'function' then
251+
return false
252+
end
253+
if not func.args then
254+
return false
255+
end
256+
if func.args[1].type ~= '...' then
257+
return false
258+
end
259+
if not func.bindDocs then
260+
return false
261+
end
262+
for _, doc in ipairs(func.bindDocs) do
263+
if doc.type == 'doc.overload' then
264+
return true
265+
end
266+
end
267+
return false
268+
end

test/completion/common.lua

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3847,3 +3847,60 @@ print(t2.A.<??>)
38473847
kind = define.CompletionItemKind.Field,
38483848
},
38493849
}
3850+
3851+
TEST [[
3852+
---@overload fun(x: number)
3853+
---@overload fun(x: number, y: number)
3854+
local function fff(...)
3855+
end
3856+
3857+
fff<??>
3858+
]]
3859+
{
3860+
{
3861+
label = 'fff(x)',
3862+
kind = define.CompletionItemKind.Function,
3863+
},
3864+
{
3865+
label = 'fff(x, y)',
3866+
kind = define.CompletionItemKind.Function,
3867+
},
3868+
}
3869+
3870+
TEST [[
3871+
---@overload fun(x: number)
3872+
---@overload fun(x: number, y: number)
3873+
function fff(...)
3874+
end
3875+
3876+
fff<??>
3877+
]]
3878+
{
3879+
{
3880+
label = 'fff(x)',
3881+
kind = define.CompletionItemKind.Function,
3882+
},
3883+
{
3884+
label = 'fff(x, y)',
3885+
kind = define.CompletionItemKind.Function,
3886+
},
3887+
}
3888+
3889+
TEST [[
3890+
---@overload fun(x: number)
3891+
---@overload fun(x: number, y: number)
3892+
function t.fff(...)
3893+
end
3894+
3895+
t.fff<??>
3896+
]]
3897+
{
3898+
{
3899+
label = 'fff(x)',
3900+
kind = define.CompletionItemKind.Function,
3901+
},
3902+
{
3903+
label = 'fff(x, y)',
3904+
kind = define.CompletionItemKind.Function,
3905+
},
3906+
}

test/crossfile/hover.lua

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1606,3 +1606,41 @@ someType:
16061606
| "#" -- description
16071607
```]]
16081608
}
1609+
1610+
TEST { { path = 'a.lua', content = [[
1611+
---@overload fun(x: number)
1612+
---@overload fun(x: number, y: number)
1613+
local function <?f?>(...)
1614+
end
1615+
]] },
1616+
hover = [[
1617+
```lua
1618+
function f(x: number)
1619+
```
1620+
1621+
---
1622+
1623+
```lua
1624+
function f(x: number, y: number)
1625+
```]]
1626+
}
1627+
1628+
TEST { { path = 'a.lua', content = [[
1629+
---@overload fun(x: number)
1630+
---@overload fun(x: number, y: number)
1631+
local function f(...)
1632+
end
1633+
1634+
<?f?>
1635+
]] },
1636+
hover = [[
1637+
```lua
1638+
function f(x: number)
1639+
```
1640+
1641+
---
1642+
1643+
```lua
1644+
function f(x: number, y: number)
1645+
```]]
1646+
}

test/signature/init.lua

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,3 +316,16 @@ X({}, <??>)
316316
{
317317
'function X(a: { x: number, y: number, z: number }, <!b: string!>)'
318318
}
319+
320+
TEST [[
321+
---@overload fun(x: number)
322+
---@overload fun(x: number, y: number)
323+
local function f(...)
324+
end
325+
326+
f(<??>)
327+
]]
328+
{
329+
'function f(<!x: number!>)',
330+
'function f(<!x: number!>, y: number)',
331+
}

0 commit comments

Comments
 (0)