diff options
author | sumneko <sumneko@hotmail.com> | 2019-05-05 21:27:34 +0800 |
---|---|---|
committer | sumneko <sumneko@hotmail.com> | 2019-05-05 21:27:34 +0800 |
commit | de0adfc8ee4561d7eb9c31e19fa5774ff7d999dd (patch) | |
tree | 1ce2b253a781a01d519a2ff6abb24d4d378c75b9 /server | |
parent | c1902bfe0f61006bd9afae43c10ee6a25863e5f6 (diff) | |
download | lua-language-server-de0adfc8ee4561d7eb9c31e19fa5774ff7d999dd.zip |
emmyFunction
Diffstat (limited to 'server')
-rw-r--r-- | server/src/core/completion.lua | 8 | ||||
-rw-r--r-- | server/src/core/hover/hover.lua | 2 | ||||
-rw-r--r-- | server/src/core/signature.lua | 8 | ||||
-rw-r--r-- | server/src/emmy/funcType.lua | 8 | ||||
-rw-r--r-- | server/src/vm/emmy.lua | 27 | ||||
-rw-r--r-- | server/src/vm/function.lua | 12 | ||||
-rw-r--r-- | server/src/vm/value.lua | 1 | ||||
-rw-r--r-- | server/test/hover/init.lua | 18 | ||||
-rw-r--r-- | server/test/signature/init.lua | 37 |
9 files changed, 96 insertions, 25 deletions
diff --git a/server/src/core/completion.lua b/server/src/core/completion.lua index 0c3a0c71..59a3cb3f 100644 --- a/server/src/core/completion.lua +++ b/server/src/core/completion.lua @@ -1,6 +1,7 @@ local findSource = require 'core.find_source' local getFunctionHover = require 'core.hover.function' local getFunctionHoverAsLib = require 'core.hover.lib_function' +local getFunctionHoverAsEmmy = require 'core.hover.emmy_function' local sourceMgr = require 'vm.source' local config = require 'config' local matchKey = require 'core.matchKey' @@ -49,7 +50,12 @@ local function getDucumentation(name, value) if lib then hover = getFunctionHoverAsLib(name, lib) else - hover = getFunctionHover(name, value:getFunction()) + local emmy = value:getEmmy() + if emmy and emmy.type == 'emmy.functionType' then + hover = getFunctionHoverAsEmmy(name, emmy) + else + hover = getFunctionHover(name, value:getFunction()) + end end if not hover then return nil diff --git a/server/src/core/hover/hover.lua b/server/src/core/hover/hover.lua index 31699d07..c067899e 100644 --- a/server/src/core/hover/hover.lua +++ b/server/src/core/hover/hover.lua @@ -182,7 +182,7 @@ local function getValueHover(source, name, value, lib) text = ('%s %s: %s'):format(tp, name, unpackTable(value)) else if literal == nil then - if class then + if class and not OriginTypes[class] then text = ('%s %s: %s %s'):format(tp, name, valueType, unpackTable(value)) else text = ('%s %s: %s'):format(tp, name, valueType) diff --git a/server/src/core/signature.lua b/server/src/core/signature.lua index 5dcce85f..6c05be5c 100644 --- a/server/src/core/signature.lua +++ b/server/src/core/signature.lua @@ -1,5 +1,6 @@ local getFunctionHover = require 'core.hover.function' local getFunctionHoverAsLib = require 'core.hover.lib_function' +local getFunctionHoverAsEmmy = require 'core.hover.emmy_function' local findLib = require 'core.find_lib' local buildValueName = require 'core.hover.name' local findSource = require 'core.find_source' @@ -66,7 +67,12 @@ local function getHover(call, pos) if lib then hover = getFunctionHoverAsLib(name, lib, object, select) else - hover = getFunctionHover(name, value:getFunction(), object, select) + local emmy = value:getEmmy() + if emmy and emmy.type == 'emmy.functionType' then + hover = getFunctionHoverAsEmmy(name, emmy, object, select) + else + hover = getFunctionHover(name, value:getFunction(), object, select) + end end if hover and hover.argLabel then return hover diff --git a/server/src/emmy/funcType.lua b/server/src/emmy/funcType.lua index 405de594..2acf8d28 100644 --- a/server/src/emmy/funcType.lua +++ b/server/src/emmy/funcType.lua @@ -39,6 +39,14 @@ function mt:getReturn() return self._return end +function mt:bindFunction(func) + if func then + self._bindFunction = func + else + return self._bindFunction + end +end + return function (manager, source) local self = setmetatable({ source = source.id, diff --git a/server/src/vm/emmy.lua b/server/src/vm/emmy.lua index cdbbd3b7..7a82cfcd 100644 --- a/server/src/vm/emmy.lua +++ b/server/src/vm/emmy.lua @@ -1,3 +1,5 @@ +local functionMgr = require 'vm.function' +local library = require 'vm.library' local mt = require 'vm.manager' function mt:clearEmmy() @@ -267,17 +269,30 @@ function mt:doEmmyFunctionType(action) ---@type emmyMgr local emmyMgr = self.emmyMgr self:instantSource(action) - local func = emmyMgr:addFunctionType(action) + local funcObj = emmyMgr:addFunctionType(action) + ---@type function + local func = functionMgr.create(action) for i = 1, #action // 2 do local nameSource = action[i*2-1] local typeSource = action[i*2] local paramType = self:buildEmmyAnyType(typeSource) - func:addParam(nameSource[1], paramType) + 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 - local returnType = self:buildEmmyAnyType(action[#action]) - func:addReturn(returnType) - self._emmy = func - return func + local returnSource = action[#action] + 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) + end + funcObj:bindFunction(func) + self._emmy = funcObj + return funcObj end function mt:buildEmmyAnyType(source) diff --git a/server/src/vm/function.lua b/server/src/vm/function.lua index 2322fa70..e605e1c9 100644 --- a/server/src/vm/function.lua +++ b/server/src/vm/function.lua @@ -349,15 +349,19 @@ function mt:findEmmyParamByName(name) return nil end +function mt:addArg(name, source, value) + local loc = localMgr.create(name, source, value) + self:saveUpvalue(name, loc) + self.args[#self.args+1] = loc +end + function mt:createArg(vm, arg) vm:instantSource(arg) arg:set('arg', self) if arg.type == 'name' then + vm:instantSource(arg) local value = valueMgr.create('nil', arg) - local loc = localMgr.create(arg[1], arg, value) - self:saveUpvalue(arg[1], loc) - self.args[#self.args+1] = loc - + self:addArg(arg[1], arg, value) elseif arg.type == '...' then self._dots = createMulti() end diff --git a/server/src/vm/value.lua b/server/src/vm/value.lua index b43530d0..bf20468d 100644 --- a/server/src/vm/value.lua +++ b/server/src/vm/value.lua @@ -600,6 +600,7 @@ function mt:setEmmy(emmy) ---@type EmmyFunctionType local emmyFuncType = emmy emmyFuncType:setValue(self) + self:setFunction(emmyFuncType:bindFunction()) else return end diff --git a/server/test/hover/init.lua b/server/test/hover/init.lua index baed3912..f31dfa03 100644 --- a/server/test/hover/init.lua +++ b/server/test/hover/init.lua @@ -730,3 +730,21 @@ local <?f?> function f(x: number, y: number) -> boolean ]] + +TEST [[ +---@type fun(x: number, y: number):boolean +local f +f(<?a?>) +]] +[[ +global a: number +]] + +TEST [[ +---@type fun(x: number, y: number):boolean +local f +local <?r?> = f() +]] +[[ +local r: boolean +]] diff --git a/server/test/signature/init.lua b/server/test/signature/init.lua index 91ef75c8..eb6e609a 100644 --- a/server/test/signature/init.lua +++ b/server/test/signature/init.lua @@ -6,8 +6,8 @@ rawset(_G, 'TEST', true) function TEST(script) return function (expect) - local pos = script:find('@', 1, true) - local new_script = script:gsub('@', '') + local pos = script:find('$', 1, true) + local new_script = script:gsub('%$', '') local ast = parser:ast(new_script, 'lua', 'Lua 5.3') local vm = buildVM(ast) assert(vm) @@ -33,7 +33,7 @@ TEST [[ local function x(a, b) end -x(@ +x($ ]] { label = "function x(a: any, b: any)", @@ -44,7 +44,7 @@ TEST [[ local function x(a, b) end -x(@) +x($) ]] { label = "function x(a: any, b: any)", @@ -55,7 +55,7 @@ TEST [[ local function x(a, b) end -x(xxx@) +x(xxx$) ]] { label = "function x(a: any, b: any)", @@ -66,7 +66,7 @@ TEST [[ local function x(a, b) end -x(xxx, @) +x(xxx, $) ]] { label = "function x(a: any, b: any)", @@ -77,7 +77,7 @@ TEST [[ function mt:f(a) end -mt:f(@ +mt:f($ ]] { label = 'function mt:f(a: any)', @@ -85,7 +85,7 @@ mt:f(@ } TEST [[ -(''):sub(@ +(''):sub($ ]] { label = [[ @@ -96,7 +96,7 @@ function *string:sub(i: integer [, j: integer(-1)]) } TEST [[ -(''):sub(1)@ +(''):sub(1)$ ]] (nil) @@ -104,16 +104,29 @@ TEST [[ local function f(a, b, c) end -f(1, 'string@') +f(1, 'string$') ]] (nil) TEST [[ -pcall(function () @ end) +pcall(function () $ end) ]] (nil) TEST [[ -table.unpack {@} +table.unpack {$} ]] (nil) + +TEST [[ +---@type fun(x: number, y: number):boolean +local zzzz +zzzz($) +]] +{ + label = [[ +function zzzz(x: number, y: number) + -> boolean +]], + arg = {15, 23}, +} |