summaryrefslogtreecommitdiff
path: root/server/src
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2019-06-29 15:27:38 +0800
committer最萌小汐 <sumneko@hotmail.com>2019-06-29 15:27:38 +0800
commitf0bfb047bfb81b6f11400939ad736b7267e08938 (patch)
tree2b4d706f82485164363987bc9740d0ebd92787f8 /server/src
parent4586d62c06751d99199044375393dc1595375491 (diff)
downloadlua-language-server-f0bfb047bfb81b6f11400939ad736b7267e08938.zip
emmyfunction 支持多返回值
Diffstat (limited to 'server/src')
-rw-r--r--server/src/core/hover/emmy_function.lua11
-rw-r--r--server/src/emmy/funcType.lua9
-rw-r--r--server/src/parser/ast.lua14
-rw-r--r--server/src/parser/grammar.lua11
-rw-r--r--server/src/vm/emmy.lua38
5 files changed, 53 insertions, 30 deletions
diff --git a/server/src/core/hover/emmy_function.lua b/server/src/core/hover/emmy_function.lua
index 313b5276..4dd89107 100644
--- a/server/src/core/hover/emmy_function.lua
+++ b/server/src/core/hover/emmy_function.lua
@@ -41,11 +41,14 @@ local function buildEmmyArgs(emmy, object, select)
end
local function buildEmmyReturns(emmy)
- local rtn = emmy:getReturn()
- if rtn then
- return '\n -> ' .. rtn:getType()
- else
+ local rtns = {}
+ emmy:eachReturn(function (rtn)
+ rtns[#rtns+1] = rtn:getType()
+ end)
+ if #rtns == 0 then
return '\n -> ' .. 'any'
+ else
+ return '\n -> ' .. table.concat(rtns, ', ')
end
end
diff --git a/server/src/emmy/funcType.lua b/server/src/emmy/funcType.lua
index f467f763..2c073fe2 100644
--- a/server/src/emmy/funcType.lua
+++ b/server/src/emmy/funcType.lua
@@ -30,7 +30,7 @@ function mt:addParam(name, type)
end
function mt:addReturn(type)
- self._return = type
+ self._returns[#self._returns+1] = type
end
function mt:eachParam(callback)
@@ -39,8 +39,10 @@ function mt:eachParam(callback)
end
end
-function mt:getReturn()
- return self._return
+function mt:eachReturn(callback)
+ for _, rtn in ipairs(self._returns) do
+ callback(rtn)
+ end
end
function mt:bindFunction(func)
@@ -55,6 +57,7 @@ return function (manager, source)
local self = setmetatable({
source = source.id,
_params = {},
+ _returns = {},
_manager = manager,
}, mt)
return self
diff --git a/server/src/parser/ast.lua b/server/src/parser/ast.lua
index b9cc9b47..f2486fd6 100644
--- a/server/src/parser/ast.lua
+++ b/server/src/parser/ast.lua
@@ -1213,16 +1213,22 @@ local Defs = {
[2] = valueType,
}
end,
- EmmyFunctionType = function (start, ...)
+ EmmyFunctionType = function (start, args, returns, finish)
local result = {
start = start,
+ finish = finish - 1,
type = 'emmyFunctionType',
- ...
+ args = args,
+ returns = returns,
}
- result.finish = result[#result] - 1
- result[#result] = nil
return result
end,
+ EmmyFunctionRtns = function (...)
+ return {...}
+ end,
+ EmmyFunctionArgs = function (...)
+ return {...}
+ end,
EmmyAlias = function (name, emmyName, ...)
return {
type = 'emmyAlias',
diff --git a/server/src/parser/grammar.lua b/server/src/parser/grammar.lua
index e0db8988..c20f5b32 100644
--- a/server/src/parser/grammar.lua
+++ b/server/src/parser/grammar.lua
@@ -593,10 +593,15 @@ EmmyArrayType <- (MustEmmyName '[]')
EmmyTableType <- ({} 'table' Cut '<' %s* EmmyType %s* ',' %s* EmmyType %s* '>' {})
-> EmmyTableType
-EmmyFunctionType<- ({} 'fun' Cut %s* EmmyFunctionArgs? %s* EmmyFunctionRtn? {})
+EmmyFunctionType<- ({} 'fun' Cut %s* EmmyFunctionArgs %s* EmmyFunctionRtns {})
-> EmmyFunctionType
-EmmyFunctionArgs<- '(' %s* EmmyFunctionArg %s* (',' %s* EmmyFunctionArg %s*)* ')'
-EmmyFunctionRtn <- ':' %s* EmmyType
+EmmyFunctionArgs<- ('(' %s* EmmyFunctionArg %s* (',' %s* EmmyFunctionArg %s*)* ')')
+ -> EmmyFunctionArgs
+ / '(' %nil ')'?
+ / %nil
+EmmyFunctionRtns<- (':' %s* EmmyType (%s* ',' %s* EmmyType)*)
+ -> EmmyFunctionRtns
+ / %nil
EmmyFunctionArg <- MustEmmyName %s* ':' %s* EmmyType
EmmySee <- {} MustEmmyName %s* '#' %s* MustEmmyName {}
diff --git a/server/src/vm/emmy.lua b/server/src/vm/emmy.lua
index 8625442b..5a15d3b0 100644
--- a/server/src/vm/emmy.lua
+++ b/server/src/vm/emmy.lua
@@ -299,23 +299,29 @@ function mt:buildEmmyFunctionType(source)
local funcObj = emmyMgr:addFunctionType(source)
---@type emmyFunction
local func = functionMgr.create(source)
- for i = 1, #source // 2 do
- local nameSource = source[i*2-1]
- local typeSource = source[i*2]
- local paramType = self:buildEmmyAnyType(typeSource)
- funcObj:addParam(nameSource[1], paramType)
- local value = self:createValue(paramType:getType(), typeSource)
- value:setEmmy(paramType)
- self:instantSource(nameSource)
- func:addArg(nameSource[1], nameSource, value)
+ local args = source.args
+ if args then
+ for i = 1, #args // 2 do
+ local nameSource = args[i*2-1]
+ local typeSource = args[i*2]
+ local paramType = self:buildEmmyAnyType(typeSource)
+ funcObj:addParam(nameSource[1], paramType)
+ local value = self:createValue(paramType:getType(), typeSource)
+ value:setEmmy(paramType)
+ self:instantSource(nameSource)
+ func:addArg(nameSource[1], nameSource, value)
+ end
end
- local returnSource = source[#source]
- if returnSource then
- local returnType = self:buildEmmyAnyType(returnSource)
- funcObj:addReturn(returnType)
- local value = self:createValue(returnType:getType(), returnSource)
- value:setEmmy(returnType)
- func:setReturn(1, value)
+ local returns = source.returns
+ if returns then
+ for i = 1, #returns do
+ local returnSource = returns[i]
+ local returnType = self:buildEmmyAnyType(returnSource)
+ funcObj:addReturn(returnType)
+ local value = self:createValue(returnType:getType(), returnSource)
+ value:setEmmy(returnType)
+ func:setReturn(i, value)
+ end
end
funcObj:bindFunction(func)
return funcObj