diff options
author | C3pa <41503714+C3pa@users.noreply.github.com> | 2022-05-28 21:29:59 +0200 |
---|---|---|
committer | C3pa <41503714+C3pa@users.noreply.github.com> | 2022-05-28 21:29:59 +0200 |
commit | 96c969c432959fd7f21778153f9b3dd5c532f488 (patch) | |
tree | 52d11b4d77f8bf8099feaf259354e0229a8f5b15 /tools/lovr-api.lua | |
parent | a570167f3cd6a6ee05d08022b88689685202efff (diff) | |
download | lua-language-server-96c969c432959fd7f21778153f9b3dd5c532f488.zip |
A refactoring change
Diffstat (limited to 'tools/lovr-api.lua')
-rw-r--r-- | tools/lovr-api.lua | 33 |
1 files changed, 17 insertions, 16 deletions
diff --git a/tools/lovr-api.lua b/tools/lovr-api.lua index dfa2bff8..4875707d 100644 --- a/tools/lovr-api.lua +++ b/tools/lovr-api.lua @@ -51,17 +51,20 @@ local function formatIndex(key) return ('[%q]'):format(key) end -local function isTableOptional(tbl) - if not tbl then - return false - end - local optional = true - for _, field in ipairs(tbl) do - if field.default == nil then - optional = nil - end - end - return optional +local function getOptional(param) + if param.type == 'table' then + if not param.table then + return '' + end + for _, field in ipairs(param.table) do + if field.default == nil then + return '' + end + end + return '?' + else + return (param.default ~= nil) and '?' or '' + end end local buildType @@ -128,11 +131,10 @@ local function buildDocFunc(variant, overload) if param.name == '...' then params[#params+1] = '...' else - local optional = (param.type == 'table' and isTableOptional(param.table) or (param.default ~= nil)) and '?' or '' if param.name:find '^[\'"]' then - params[#params+1] = ('%s%s: %s|%q'):format(param.name:sub(2, -2), optional, getTypeName(param.type), param.name) + params[#params+1] = ('%s%s: %s|%q'):format(param.name:sub(2, -2), getOptional(param), getTypeName(param.type), param.name) else - params[#params+1] = ('%s%s: %s'):format(param.name, optional, getTypeName(param.type)) + params[#params+1] = ('%s%s: %s'):format(param.name, getOptional(param), getTypeName(param.type)) end end end @@ -164,10 +166,9 @@ local function buildFunction(func, typeName) for _, param in ipairs(func.variants[1].arguments or {}) do for paramName in param.name:gmatch '[%a_][%w_]*' do params[#params+1] = paramName - local optional = param.type == 'table' and isTableOptional(param.table) or (param.default ~= nil) text[#text+1] = ('---@param %s%s %s # %s'):format( paramName, - optional and '?' or '', + getOptional(param), buildType(param), param.description ) |