summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2021-11-08 20:17:04 +0800
committer最萌小汐 <sumneko@hotmail.com>2021-11-08 20:17:04 +0800
commit9585ac191f61d72dd198edec139b955276a53072 (patch)
tree8497952d1ea937cae6a8e4959a1e39223d86fc94
parent90763c00c9960a8776e046c45a9f87d348ebc3d1 (diff)
downloadlua-language-server-9585ac191f61d72dd198edec139b955276a53072.zip
resolve #708 resolve #767
-rw-r--r--.luarc.json1
-rw-r--r--changelog.md6
-rw-r--r--script/core/diagnostics/undefined-doc-param.lua3
-rw-r--r--script/core/hover/arg.lua17
-rw-r--r--script/core/infer.lua2
-rw-r--r--script/parser/luadoc.lua72
-rw-r--r--test/crossfile/hover.lua2
-rw-r--r--test/definition/luadoc.lua5
-rw-r--r--test/diagnostics/init.lua17
-rw-r--r--test/hover/init.lua41
10 files changed, 112 insertions, 54 deletions
diff --git a/.luarc.json b/.luarc.json
index 6ace9089..a8f8bbe2 100644
--- a/.luarc.json
+++ b/.luarc.json
@@ -1,6 +1,5 @@
{
"$schema": "https://raw.githubusercontent.com/sumneko/vscode-lua/master/setting/schema.json",
-
"color": {
"mode": "SemanticEnhanced"
},
diff --git a/changelog.md b/changelog.md
index e4c6d743..f2f72bb6 100644
--- a/changelog.md
+++ b/changelog.md
@@ -15,7 +15,11 @@
+ `discard-returns`: check whether the return value is discarded.
* `NEW` locale `pt-br`, thanks [Jeferson Ferreira](https://github.com/jefersonf)
* `NEW` supports [utf-8-offsets](https://clangd.llvm.org/extensions#utf-8-offsets)
-* `CHG` `LuaDoc` supports unicode
+* `CHG` `LuaDoc`:
+ + supports unicode
+ + supports `---@param ... number`, equivalent to `---@vararg number`
+ + supports `fun(...: string)`
+ + supports `fun(x, y, ...)`, equivalent to `fun(x: any, y: any, ...: any)`
* `CHG` no longer asks to trust plugin in VSCode, because VSCode already provides the workspace trust feature
* `CHG` skip huge files (>= 10 MB)
* `FIX` [#777](https://github.com/sumneko/lua-language-server/issues/777)
diff --git a/script/core/diagnostics/undefined-doc-param.lua b/script/core/diagnostics/undefined-doc-param.lua
index 6140b4f0..86bf3871 100644
--- a/script/core/diagnostics/undefined-doc-param.lua
+++ b/script/core/diagnostics/undefined-doc-param.lua
@@ -12,6 +12,9 @@ local function hasParamName(func, name)
if arg[1] == name then
return true
end
+ if arg.type == '...' and name == '...' then
+ return true
+ end
end
return false
end
diff --git a/script/core/hover/arg.lua b/script/core/hover/arg.lua
index 822be2b6..5963ad97 100644
--- a/script/core/hover/arg.lua
+++ b/script/core/hover/arg.lua
@@ -60,18 +60,11 @@ local function asDocFunction(source)
for i = 1, #source.args do
local arg = source.args[i]
local name = arg.name[1]
- if arg.extends then
- args[i] = ('%s%s: %s'):format(
- name,
- arg.optional and '?' or '',
- infer.searchAndViewInfers(arg.extends)
- )
- else
- args[i] = ('%s%s'):format(
- name,
- arg.optional and '?' or ''
- )
- end
+ args[i] = ('%s%s: %s'):format(
+ name,
+ arg.optional and '?' or '',
+ arg.extends and infer.searchAndViewInfers(arg.extends) or 'any'
+ )
end
return table.concat(args, ', ')
end
diff --git a/script/core/infer.lua b/script/core/infer.lua
index 6e667e65..36bf740d 100644
--- a/script/core/infer.lua
+++ b/script/core/infer.lua
@@ -385,7 +385,7 @@ function m.viewDocFunction(doc)
end
local args = {}
for i, arg in ipairs(doc.args) do
- args[i] = ('%s: %s'):format(arg.name[1], m.viewDocName(arg.extends))
+ args[i] = ('%s: %s'):format(arg.name[1], arg.extends and m.viewDocName(arg.extends) or 'any')
end
local label = ('fun(%s)'):format(table.concat(args, ', '))
if #doc.returns > 0 then
diff --git a/script/parser/luadoc.lua b/script/parser/luadoc.lua
index c332d4c0..d206e6d7 100644
--- a/script/parser/luadoc.lua
+++ b/script/parser/luadoc.lua
@@ -342,6 +342,21 @@ local function parseTypeUnitTable(parent, node)
return result
end
+local function parseDots(tp, parent)
+ if not checkToken('symbol', '...', 1) then
+ return
+ end
+ nextToken()
+ local dots = {
+ type = tp,
+ start = getStart(),
+ finish = getFinish(),
+ parent = parent,
+ [1] = '...',
+ }
+ return dots
+end
+
local function parseTypeUnitFunction()
local typeUnit = {
type = 'doc.type.function',
@@ -361,47 +376,29 @@ local function parseTypeUnitFunction()
type = 'doc.type.arg',
parent = typeUnit,
}
- if checkToken('symbol', '...', 1) then
- nextToken()
- local vararg = {
- type = 'doc.type.name',
- start = getStart(),
+ arg.name = parseName('doc.type.name', arg)
+ or parseDots('doc.type.name', arg)
+ if not arg.name then
+ pushError {
+ type = 'LUADOC_MISS_ARG_NAME',
+ start = getFinish(),
finish = getFinish(),
- parent = arg,
- [1] = '...',
}
- arg.name = vararg
- if not arg.start then
- arg.start = arg.name.start
- end
- arg.finish = getFinish()
- else
- arg.name = parseName('doc.type.name', arg)
- if not arg.name then
- pushError {
- type = 'LUADOC_MISS_ARG_NAME',
- start = getFinish(),
- finish = getFinish(),
- }
- break
- end
- if not arg.start then
- arg.start = arg.name.start
- end
- if checkToken('symbol', '?', 1) then
- nextToken()
- arg.optional = true
- end
- arg.finish = getFinish()
- if not nextSymbolOrError(':') then
- break
- end
+ break
+ end
+ if not arg.start then
+ arg.start = arg.name.start
+ end
+ if checkToken('symbol', '?', 1) then
+ nextToken()
+ arg.optional = true
+ end
+ arg.finish = getFinish()
+ if checkToken('symbol', ':', 1) then
+ nextToken()
arg.extends = parseType(arg)
- if not arg.extends then
- break
- end
- arg.finish = getFinish()
end
+ arg.finish = getFinish()
typeUnit.args[#typeUnit.args+1] = arg
if checkToken('symbol', ',', 1) then
nextToken()
@@ -762,6 +759,7 @@ local function parseParam()
type = 'doc.param',
}
result.param = parseName('doc.param.name', result)
+ or parseDots('doc.param.name', result)
if not result.param then
pushError {
type = 'LUADOC_MISS_PARAM_NAME',
diff --git a/test/crossfile/hover.lua b/test/crossfile/hover.lua
index 6357ab50..f23850a5 100644
--- a/test/crossfile/hover.lua
+++ b/test/crossfile/hover.lua
@@ -593,7 +593,7 @@ function f(arg1: integer, arg2: integer)
---
```lua
-function f()
+function f(arg3: any)
```]]}
diff --git a/test/definition/luadoc.lua b/test/definition/luadoc.lua
index 5f8c11ee..58c7c8fe 100644
--- a/test/definition/luadoc.lua
+++ b/test/definition/luadoc.lua
@@ -100,6 +100,11 @@ function f(<?...?>) end
]]
TEST [[
+---@param ... <!fun():void!>
+function f(<?...?>) end
+]]
+
+TEST [[
---@overload <!fun(y: boolean)!>
---@param x number
---@param y boolean
diff --git a/test/diagnostics/init.lua b/test/diagnostics/init.lua
index 4fcf38ab..fa67d229 100644
--- a/test/diagnostics/init.lua
+++ b/test/diagnostics/init.lua
@@ -1489,3 +1489,20 @@ local _ = type(function () ---@async
return nil
end)
]]
+
+TEST [[
+---@param ... number
+local function f(...)
+ return ...
+end
+
+return f
+]]
+
+TEST [[
+---@type fun(...: string)
+]]
+
+TEST [[
+---@type fun(xxx, yyy, ...): boolean
+]]
diff --git a/test/hover/init.lua b/test/hover/init.lua
index d2a6e084..d5c47ddf 100644
--- a/test/hover/init.lua
+++ b/test/hover/init.lua
@@ -1118,6 +1118,45 @@ local t: Class[]
]]
TEST [[
+---@class Class
+
+---@param ... Class
+local function f(...)
+ local _, <?x?> = ...
+end
+f(1, 2, 3)
+]]
+[[
+local x: Class
+]]
+
+TEST [[
+---@class Class
+
+---@param ... Class
+local function f(...)
+ local t = {...}
+ local <?v?> = t[1]
+end
+]]
+[[
+local v: Class
+]]
+
+TEST [[
+---@class Class
+
+---@param ... Class
+local function f(...)
+ local <?t?> = {...}
+end
+f(1, 2, 3)
+]]
+[[
+local t: Class[]
+]]
+
+TEST [[
---@type string[]
local <?x?>
]]
@@ -1454,7 +1493,7 @@ TEST [[
local function f(<?callback?>) end
]]
[[
-local callback: fun(x: integer, ...: nil)
+local callback: fun(x: integer, ...: any)
]]
TEST [[