summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorC3pa <41503714+C3pa@users.noreply.github.com>2022-05-28 21:29:59 +0200
committerC3pa <41503714+C3pa@users.noreply.github.com>2022-05-28 21:29:59 +0200
commit96c969c432959fd7f21778153f9b3dd5c532f488 (patch)
tree52d11b4d77f8bf8099feaf259354e0229a8f5b15
parenta570167f3cd6a6ee05d08022b88689685202efff (diff)
downloadlua-language-server-96c969c432959fd7f21778153f9b3dd5c532f488.zip
A refactoring change
-rw-r--r--tools/love-api.lua33
-rw-r--r--tools/lovr-api.lua33
2 files changed, 34 insertions, 32 deletions
diff --git a/tools/love-api.lua b/tools/love-api.lua
index e217f5e5..c5f40288 100644
--- a/tools/love-api.lua
+++ b/tools/love-api.lua
@@ -50,17 +50,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
@@ -124,11 +127,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
@@ -160,10 +162,9 @@ local function buildFunction(func, node, 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
)
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
)