diff options
-rw-r--r-- | changelog.md | 2 | ||||
-rw-r--r-- | script/core/completion/completion.lua | 12 | ||||
-rw-r--r-- | script/core/hover/init.lua | 8 | ||||
-rw-r--r-- | script/core/signature.lua | 2 | ||||
-rw-r--r-- | script/vm/doc.lua | 7 | ||||
-rw-r--r-- | script/vm/function.lua | 25 | ||||
-rw-r--r-- | test/completion/common.lua | 57 | ||||
-rw-r--r-- | test/crossfile/hover.lua | 38 | ||||
-rw-r--r-- | test/signature/init.lua | 13 |
9 files changed, 155 insertions, 9 deletions
diff --git a/changelog.md b/changelog.md index 4057bce8..001fa032 100644 --- a/changelog.md +++ b/changelog.md @@ -36,6 +36,7 @@ server will generate `doc.json` and `doc.md` in `LOGPATH`. * `redundant-return-value` * `return-type-mismatch` * `CHG` workspace-symbol: supports chain fields based on global variables and types. try `io.open` or `iolib.open` +* `CHG` [#1641] if a function only has varargs and has `---@overload`, the varargs will be ignored * `FIX` [#1567] * `FIX` [#1593] * `FIX` [#1595] @@ -59,6 +60,7 @@ server will generate `doc.json` and `doc.md` in `LOGPATH`. [#1608]: https://github.com/sumneko/lua-language-server/issues/1608 [#1626]: https://github.com/sumneko/lua-language-server/issues/1626 [#1637]: https://github.com/sumneko/lua-language-server/issues/1637 +[#1641]: https://github.com/sumneko/lua-language-server/issues/1641 [#1642]: https://github.com/sumneko/lua-language-server/issues/1642 ## 3.5.6 diff --git a/script/core/completion/completion.lua b/script/core/completion/completion.lua index 39f95ddf..a8909d06 100644 --- a/script/core/completion/completion.lua +++ b/script/core/completion/completion.lua @@ -315,7 +315,7 @@ local function checkLocal(state, word, position, results) return orders[a] < orders[b] end) for _, def in ipairs(defs) do - if def.type == 'function' + if (def.type == 'function' and not vm.isVarargFunctionWithOverloads(def)) or def.type == 'doc.type.function' then local funcLabel = name .. getParams(def, false) buildFunction(results, source, def, false, { @@ -490,7 +490,7 @@ end local function checkFieldThen(state, name, src, word, startPos, position, parent, oop, results) local value = vm.getObjectFunctionValue(src) or src local kind = define.CompletionItemKind.Field - if value.type == 'function' + if (value.type == 'function' and not vm.isVarargFunctionWithOverloads(value)) or value.type == 'doc.type.function' then if oop then kind = define.CompletionItemKind.Method @@ -572,9 +572,11 @@ local function checkFieldOfRefs(refs, state, word, startPos, position, parent, o local value = vm.getObjectFunctionValue(src) or src if value.type == 'function' or value.type == 'doc.type.function' then - funcLabel = name .. getParams(value, oop) - fields[funcLabel] = src - count = count + 1 + if not vm.isVarargFunctionWithOverloads(value) then + funcLabel = name .. getParams(value, oop) + fields[funcLabel] = src + count = count + 1 + end if value.type == 'function' and value.bindDocs then for _, doc in ipairs(value.bindDocs) do if doc.type == 'doc.overload' then diff --git a/script/core/hover/init.lua b/script/core/hover/init.lua index 5a65cbce..61f09455 100644 --- a/script/core/hover/init.lua +++ b/script/core/hover/init.lua @@ -60,8 +60,12 @@ local function getHover(source) if guide.isOOP(def) then oop = true end - if def.type == 'function' - or def.type == 'doc.type.function' then + if def.type == 'function' + and not vm.isVarargFunctionWithOverloads(def) then + hasFunc = true + addHover(def, true, oop) + end + if def.type == 'doc.type.function' then hasFunc = true addHover(def, true, oop) end diff --git a/script/core/signature.lua b/script/core/signature.lua index c411d787..de38cb80 100644 --- a/script/core/signature.lua +++ b/script/core/signature.lua @@ -137,7 +137,7 @@ local function makeSignatures(text, call, pos) node = node:getData 'originNode' or node local mark = {} for src in node:eachObject() do - if src.type == 'function' + if (src.type == 'function' and not vm.isVarargFunctionWithOverloads(src)) or src.type == 'doc.type.function' then if not mark[src] then mark[src] = true diff --git a/script/vm/doc.lua b/script/vm/doc.lua index 293cf5c3..b292bc3c 100644 --- a/script/vm/doc.lua +++ b/script/vm/doc.lua @@ -112,6 +112,13 @@ local function getDeprecated(value) end end end + if value.type == 'function' then + local doc = getDeprecated(value.parent) + if doc then + value._deprecated = doc + return doc + end + end value._deprecated = false return nil end diff --git a/script/vm/function.lua b/script/vm/function.lua index 7cde6298..8b996721 100644 --- a/script/vm/function.lua +++ b/script/vm/function.lua @@ -218,7 +218,7 @@ function vm.getMatchedFunctions(func, args, mark) local funcs = {} local node = vm.compileNode(func) for n in node:eachObject() do - if n.type == 'function' + if (n.type == 'function' and not vm.isVarargFunctionWithOverloads(n)) or n.type == 'doc.type.function' then funcs[#funcs+1] = n end @@ -243,3 +243,26 @@ function vm.getMatchedFunctions(func, args, mark) return matched end end + +---@param func table +---@return boolean +function vm.isVarargFunctionWithOverloads(func) + if func.type ~= 'function' then + return false + end + if not func.args then + return false + end + if func.args[1].type ~= '...' then + return false + end + if not func.bindDocs then + return false + end + for _, doc in ipairs(func.bindDocs) do + if doc.type == 'doc.overload' then + return true + end + end + return false +end diff --git a/test/completion/common.lua b/test/completion/common.lua index 38104d41..cc164d3a 100644 --- a/test/completion/common.lua +++ b/test/completion/common.lua @@ -3847,3 +3847,60 @@ print(t2.A.<??>) kind = define.CompletionItemKind.Field, }, } + +TEST [[ +---@overload fun(x: number) +---@overload fun(x: number, y: number) +local function fff(...) +end + +fff<??> +]] +{ + { + label = 'fff(x)', + kind = define.CompletionItemKind.Function, + }, + { + label = 'fff(x, y)', + kind = define.CompletionItemKind.Function, + }, +} + +TEST [[ +---@overload fun(x: number) +---@overload fun(x: number, y: number) +function fff(...) +end + +fff<??> +]] +{ + { + label = 'fff(x)', + kind = define.CompletionItemKind.Function, + }, + { + label = 'fff(x, y)', + kind = define.CompletionItemKind.Function, + }, +} + +TEST [[ +---@overload fun(x: number) +---@overload fun(x: number, y: number) +function t.fff(...) +end + +t.fff<??> +]] +{ + { + label = 'fff(x)', + kind = define.CompletionItemKind.Function, + }, + { + label = 'fff(x, y)', + kind = define.CompletionItemKind.Function, + }, +} diff --git a/test/crossfile/hover.lua b/test/crossfile/hover.lua index c3d7078c..6d1f3df6 100644 --- a/test/crossfile/hover.lua +++ b/test/crossfile/hover.lua @@ -1606,3 +1606,41 @@ someType: | "#" -- description ```]] } + +TEST { { path = 'a.lua', content = [[ +---@overload fun(x: number) +---@overload fun(x: number, y: number) +local function <?f?>(...) +end +]] }, +hover = [[ +```lua +function f(x: number) +``` + +--- + +```lua +function f(x: number, y: number) +```]] +} + +TEST { { path = 'a.lua', content = [[ +---@overload fun(x: number) +---@overload fun(x: number, y: number) +local function f(...) +end + +<?f?> +]] }, +hover = [[ +```lua +function f(x: number) +``` + +--- + +```lua +function f(x: number, y: number) +```]] +} diff --git a/test/signature/init.lua b/test/signature/init.lua index b78fdaa5..e92b89e8 100644 --- a/test/signature/init.lua +++ b/test/signature/init.lua @@ -316,3 +316,16 @@ X({}, <??>) { 'function X(a: { x: number, y: number, z: number }, <!b: string!>)' } + +TEST [[ +---@overload fun(x: number) +---@overload fun(x: number, y: number) +local function f(...) +end + +f(<??>) +]] +{ +'function f(<!x: number!>)', +'function f(<!x: number!>, y: number)', +} |