diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2018-12-06 18:07:10 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2018-12-06 18:07:10 +0800 |
commit | 979676be219b83c7295409824ee8280a3335cc5a (patch) | |
tree | e4928df884ddd961671d62876e84d2bec7c9ab19 /server/src/matcher | |
parent | d119c1cefff8a6a2812ac10375297f033a8cf5f0 (diff) | |
download | lua-language-server-979676be219b83c7295409824ee8280a3335cc5a.zip |
枚举
Diffstat (limited to 'server/src/matcher')
-rw-r--r-- | server/src/matcher/find_lib.lua | 4 | ||||
-rw-r--r-- | server/src/matcher/hover.lua | 89 |
2 files changed, 73 insertions, 20 deletions
diff --git a/server/src/matcher/find_lib.lua b/server/src/matcher/find_lib.lua index a7b15bbb..ca5577d1 100644 --- a/server/src/matcher/find_lib.lua +++ b/server/src/matcher/find_lib.lua @@ -13,7 +13,7 @@ local function getLibs() if Libs then return Libs end - Libs = {} + Libs = table.container() for path in io.scan(ROOT / 'libs') do local buf = io.load(path) if buf then @@ -22,7 +22,7 @@ local function getLibs() end local language = require 'language' - local locale = {} + local locale = table.container() for path in io.scan(ROOT / 'locale' / language / 'libs') do local buf = io.load(path) if buf then 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) |