diff options
-rw-r--r-- | script-beta/core/hover/arg.lua | 18 | ||||
-rw-r--r-- | script-beta/core/hover/return.lua | 5 | ||||
-rw-r--r-- | script-beta/parser/luadoc.lua | 8 | ||||
-rw-r--r-- | test-beta/hover/init.lua | 13 |
4 files changed, 42 insertions, 2 deletions
diff --git a/script-beta/core/hover/arg.lua b/script-beta/core/hover/arg.lua index ef5eef57..2b7d47d3 100644 --- a/script-beta/core/hover/arg.lua +++ b/script-beta/core/hover/arg.lua @@ -56,6 +56,18 @@ local function asLibrary(source, oop) return table.concat(args) end +local function optionalArg(arg) + if not arg.bindDocs then + return false + end + local name = arg[1] + for _, doc in ipairs(arg.bindDocs) do + if doc.type == 'doc.param' and doc.param[1] == name then + return doc.optional + end + end +end + local function asFunction(source, oop) if not source.args then return '' @@ -65,7 +77,11 @@ local function asFunction(source, oop) local arg = source.args[i] local name = arg.name or guide.getName(arg) if name then - args[i] = ('%s: %s'):format(name, vm.getInferType(arg)) + args[i] = ('%s: %s%s'):format( + name, + vm.getInferType(arg), + optionalArg(arg) and '?' or '' + ) else args[i] = ('%s'):format(vm.getInferType(arg)) end diff --git a/script-beta/core/hover/return.lua b/script-beta/core/hover/return.lua index a0b4363d..589b86b4 100644 --- a/script-beta/core/hover/return.lua +++ b/script-beta/core/hover/return.lua @@ -108,7 +108,10 @@ local function asFunction(source) end if #types > 0 or rtn[1] then local tp = mergeTypes(types) or 'any' - line[#line+1] = tp + line[#line+1] = ('%s%s'):format( + tp, + rtn[1].optional and '?' or '' + ) else break end diff --git a/script-beta/parser/luadoc.lua b/script-beta/parser/luadoc.lua index ca4ee25d..933ce1be 100644 --- a/script-beta/parser/luadoc.lua +++ b/script-beta/parser/luadoc.lua @@ -453,6 +453,10 @@ local function parseParam() end result.start = getStart() result.extends = parseType(result) + if checkToken('symbol', '?', 1) then + nextToken() + result.optional = true + end if not result.extends then pushError { type = 'LUADOC_MISS_PARAM_EXTENDS', @@ -478,6 +482,10 @@ local function parseReturn() if not result.start then result.start = docType.start end + if checkToken('symbol', '?', 1) then + nextToken() + docType.optional = true + end result.returns[#result.returns+1] = docType if not checkToken('symbol', ',', 1) then break diff --git a/test-beta/hover/init.lua b/test-beta/hover/init.lua index 75750f78..e1c11f04 100644 --- a/test-beta/hover/init.lua +++ b/test-beta/hover/init.lua @@ -1222,6 +1222,19 @@ function f(x: boolean?) -> boolean? ]] +TEST [[ +---@param x number? +---@param y boolean? +---@return table?, string? +local function <?f?>(x, y) +end +]] +[[ +function f(x: number?, y: boolean?) + -> table? + 2. string? +]] + do return end TEST [[ ---@param x number {optional = 'after'} |