From 979676be219b83c7295409824ee8280a3335cc5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Thu, 6 Dec 2018 18:07:10 +0800 Subject: =?UTF-8?q?=E6=9E=9A=E4=B8=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/src/matcher/hover.lua | 89 +++++++++++++++++++++++++++++++++++--------- 1 file changed, 71 insertions(+), 18 deletions(-) (limited to 'server/src/matcher/hover.lua') diff --git a/server/src/matcher/hover.lua b/server/src/matcher/hover.lua index 2b78216a..79a47ede 100644 --- a/server/src/matcher/hover.lua +++ b/server/src/matcher/hover.lua @@ -6,21 +6,30 @@ local function buildArgs(lib) return '' end local strs = {} - for i, rtn in ipairs(lib.args) do - if rtn.optional then - strs[#strs+1] = '[' + for i, arg in ipairs(lib.args) do + if arg.optional then + if i > 1 then + strs[#strs+1] = ' [' + else + strs[#strs+1] = '[' + end end if i > 1 then strs[#strs+1] = ', ' end - strs[#strs+1] = ('%s:%s'):format( - rtn.name or ('arg' .. tostring(i)), - (rtn.type or 'any') - ) - if rtn.default then - strs[#strs+1] = ('(%q)'):format(rtn.default) + if arg.name then + strs[#strs+1] = ('%s:'):format(arg.name) end - if rtn.optional then + strs[#strs+1] = arg.type or 'any' + if arg.default then + strs[#strs+1] = ('(%q)'):format(arg.default) + end + if arg.optional == 'self' then + strs[#strs+1] = ']' + end + end + for _, arg in ipairs(lib.args) do + if arg.optional == 'after' then strs[#strs+1] = ']' end end @@ -34,29 +43,73 @@ local function buildReturns(lib) local strs = {} for i, rtn in ipairs(lib.returns) do if rtn.optional then - strs[#strs+1] = '[' + if i > 1 then + strs[#strs+1] = ' [' + else + strs[#strs+1] = '[' + end end if i > 1 then strs[#strs+1] = ', ' end - strs[#strs+1] = ('%s:%s'):format( - rtn.name or ('res' .. tostring(i)), - (rtn.type or 'any') - ) + if rtn.name then + strs[#strs+1] = ('%s:'):format(rtn.name) + end + strs[#strs+1] = rtn.type or 'any' if rtn.default then strs[#strs+1] = ('(%q)'):format(rtn.default) end - if rtn.optional then + if rtn.optional == 'self' then + strs[#strs+1] = ']' + end + end + for _, rtn in ipairs(lib.returns) do + if rtn.optional == 'after' then strs[#strs+1] = ']' end end - return '\n-> ' .. table.concat(strs) + return '\n -> ' .. table.concat(strs) +end + +local function buildEnum(lib) + if not lib.enums then + return '' + end + local container = table.container() + for _, enum in ipairs(lib.enums) do + if not enum.name or not enum.enum then + goto NEXT_ENUM + end + if not container[enum.name] then + container[enum.name] = {} + if lib.args then + for _, arg in ipairs(lib.args) do + if arg.name == enum.name then + container[enum.name].type = arg.type + break + end + end + end + end + table.insert(container[enum.name], enum) + ::NEXT_ENUM:: + end + local strs = {} + for name, enums in pairs(container) do + strs[#strs+1] = ('\n%s:%s'):format(name, enums.type or '') + for _, enum in ipairs(enums) do + strs[#strs+1] = '\n | ' + strs[#strs+1] = ('%q: %s'):format(enum.enum, enum.description or '') + end + end + return table.concat(strs) end local function buildFunctionHover(lib, name) local title = ('function %s(%s)%s'):format(name, buildArgs(lib), buildReturns(lib)) + local enum = buildEnum(lib) local tip = lib.description or '' - return ('```lua\n%s\n```\n%s'):format(title, tip) + return ('```lua\n%s%s\n```\n%s'):format(title, enum, tip) end return function (results, pos) -- cgit v1.2.3