summaryrefslogtreecommitdiff
path: root/script
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2024-02-19 15:28:25 +0800
committerGitHub <noreply@github.com>2024-02-19 15:28:25 +0800
commit8fc5a88967e5f56296ed3f5a65fd7391905dbcbd (patch)
tree059b6960da2ffde5373b6adfb42159b099f38eeb /script
parent7848d0367a89a66207fc4b5e8d6593520bf40ffa (diff)
parent7503881344501248ac36d25d85dfc560809ced21 (diff)
downloadlua-language-server-8fc5a88967e5f56296ed3f5a65fd7391905dbcbd.zip
Merge pull request #2505 from lizho/master
support tuple type
Diffstat (limited to 'script')
-rw-r--r--script/parser/luadoc.lua74
-rw-r--r--script/vm/infer.lua20
2 files changed, 85 insertions, 9 deletions
diff --git a/script/parser/luadoc.lua b/script/parser/luadoc.lua
index c59bc5c1..aec8994b 100644
--- a/script/parser/luadoc.lua
+++ b/script/parser/luadoc.lua
@@ -369,6 +369,78 @@ local function parseTable(parent)
return typeUnit
end
+local function parseTuple(parent)
+ if not checkToken('symbol', '[', 1) then
+ return nil
+ end
+ nextToken()
+ local typeUnit = {
+ type = 'doc.type.table',
+ start = getStart(),
+ parent = parent,
+ fields = {},
+ isTuple = true,
+ }
+
+ local index = 1
+ while true do
+ if checkToken('symbol', ']', 1) then
+ nextToken()
+ break
+ end
+ local field = {
+ type = 'doc.type.field',
+ parent = typeUnit,
+ }
+
+ do
+ local needCloseParen
+ if checkToken('symbol', '(', 1) then
+ nextToken()
+ needCloseParen = true
+ end
+ field.name = {
+ type = 'doc.type',
+ start = getFinish(),
+ firstFinish = getFinish(),
+ finish = getFinish(),
+ parent = field,
+ }
+ field.name.types = {
+ [1] = {
+ type = 'doc.type.integer',
+ start = getFinish(),
+ finish = getFinish(),
+ parent = field.name,
+ [1] = index,
+ }
+ }
+ index = index + 1
+ field.extends = parseType(field)
+ if not field.extends then
+ break
+ end
+ field.optional = field.extends.optional
+ field.start = field.extends.start
+ field.finish = field.extends.finish
+ if needCloseParen then
+ nextSymbolOrError ')'
+ end
+ end
+
+ typeUnit.fields[#typeUnit.fields+1] = field
+ if checkToken('symbol', ',', 1)
+ or checkToken('symbol', ';', 1) then
+ nextToken()
+ else
+ nextSymbolOrError(']')
+ break
+ end
+ end
+ typeUnit.finish = getFinish()
+ return typeUnit
+end
+
local function parseSigns(parent)
if not checkToken('symbol', '<', 1) then
return nil
@@ -682,6 +754,7 @@ end
function parseTypeUnit(parent)
local result = parseFunction(parent)
or parseTable(parent)
+ or parseTuple(parent)
or parseString(parent)
or parseCode(parent)
or parseInteger(parent)
@@ -864,6 +937,7 @@ local docSwitch = util.switch()
while true do
local extend = parseName('doc.extends.name', result)
or parseTable(result)
+ or parseTuple(result)
if not extend then
pushWarning {
type = 'LUADOC_MISS_CLASS_EXTENDS_NAME',
diff --git a/script/vm/infer.lua b/script/vm/infer.lua
index f2673ed3..3f3d0e3a 100644
--- a/script/vm/infer.lua
+++ b/script/vm/infer.lua
@@ -157,22 +157,24 @@ local viewNodeSwitch;viewNodeSwitch = util.switch()
end
infer._hasClass = true
local buf = {}
- buf[#buf+1] = '{ '
+ buf[#buf+1] = source.isTuple and '[' or '{ '
for i, field in ipairs(source.fields) do
if i > 1 then
buf[#buf+1] = ', '
end
- local key = field.name
- if key.type == 'doc.type' then
- buf[#buf+1] = ('[%s]: '):format(vm.getInfer(key):view(uri))
- elseif type(key[1]) == 'string' then
- buf[#buf+1] = key[1] .. ': '
- else
- buf[#buf+1] = ('[%q]: '):format(key[1])
+ if not source.isTuple then
+ local key = field.name
+ if key.type == 'doc.type' then
+ buf[#buf+1] = ('[%s]: '):format(vm.getInfer(key):view(uri))
+ elseif type(key[1]) == 'string' then
+ buf[#buf+1] = key[1] .. ': '
+ else
+ buf[#buf+1] = ('[%q]: '):format(key[1])
+ end
end
buf[#buf+1] = vm.getInfer(field.extends):view(uri)
end
- buf[#buf+1] = ' }'
+ buf[#buf+1] = source.isTuple and ']' or ' }'
return table.concat(buf)
end)
: case 'doc.type.string'