summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2020-10-31 15:44:41 +0800
committer最萌小汐 <sumneko@hotmail.com>2020-10-31 15:44:41 +0800
commit09e873bf25f1dbaccd97d4fe7f608dde1fe68028 (patch)
treec2b6d6c2c595826ec15bdd717ba7343b5176afdb
parent709d4d828978ecd787dbbf108b04e985c4afc0a4 (diff)
downloadlua-language-server-09e873bf25f1dbaccd97d4fe7f608dde1fe68028.zip
doc.param 和 doc.return 支持 ?
-rw-r--r--script-beta/core/hover/arg.lua18
-rw-r--r--script-beta/core/hover/return.lua5
-rw-r--r--script-beta/parser/luadoc.lua8
-rw-r--r--test-beta/hover/init.lua13
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'}