diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2018-12-06 17:06:23 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2018-12-06 17:06:23 +0800 |
commit | 607d9f9aa196cf75a68084c4d505602d2e491c57 (patch) | |
tree | 0da2d7fed98e5148032e3aa6beb5053be5b53459 /server/src/matcher/hover.lua | |
parent | 00ad163d5c8049769a173040f20a28ba46e59262 (diff) | |
download | lua-language-server-607d9f9aa196cf75a68084c4d505602d2e491c57.zip |
格式化参数和返回值
Diffstat (limited to 'server/src/matcher/hover.lua')
-rw-r--r-- | server/src/matcher/hover.lua | 60 |
1 files changed, 57 insertions, 3 deletions
diff --git a/server/src/matcher/hover.lua b/server/src/matcher/hover.lua index 1c4da28e..09d2f168 100644 --- a/server/src/matcher/hover.lua +++ b/server/src/matcher/hover.lua @@ -1,6 +1,62 @@ local findResult = require 'matcher.find_result' local findLib = require 'matcher.find_lib' +local function buildArgs(lib) + if not lib.args then + return '' + end + local strs = {} + for i, rtn in ipairs(lib.args) do + if rtn.optional then + strs[#strs+1] = '[' + end + if i > 1 then + strs[#strs+1] = ', ' + end + strs[#strs+1] = rtn.name or ('arg' .. tostring(i)) + strs[#strs+1] = ':' + strs[#strs+1] = rtn.type or 'any' + if rtn.default then + strs[#strs+1] = (':%q'):format(rtn.default) + end + if rtn.optional then + strs[#strs+1] = ']' + end + end + return table.concat(strs) +end + +local function buildReturns(lib) + if not lib.returns then + return '' + end + local strs = {} + for i, rtn in ipairs(lib.returns) do + if rtn.optional then + strs[#strs+1] = '[' + end + if i > 1 then + strs[#strs+1] = ', ' + end + strs[#strs+1] = rtn.name or ('res' .. tostring(i)) + strs[#strs+1] = ':' + strs[#strs+1] = rtn.type or 'any' + if rtn.default then + strs[#strs+1] = (':%q'):format(rtn.default) + end + if rtn.optional then + strs[#strs+1] = ']' + end + end + return '\n-> ' .. table.concat(strs) +end + +local function buildFunctionHover(lib, name) + local title = ('```lua\n%s(%s)%s\n```'):format(name, buildArgs(lib), buildReturns(lib)) + local tip = lib.description or '' + return ('%s\n\n%s'):format(title, tip) +end + return function (results, pos) local result = findResult(results, pos) if not result then @@ -17,9 +73,7 @@ return function (results, pos) end if lib.type == 'function' then - local title = name - local tip = lib.description or '' - return ('### %s\n\n* %s'):format(title, tip) + return buildFunctionHover(lib, name) elseif lib.type == 'table' then local tip = lib.description or '' return ('%s'):format(tip) |