diff options
-rw-r--r-- | server/src/core/hover.lua | 33 | ||||
-rw-r--r-- | server/src/core/hover_function.lua | 35 | ||||
-rw-r--r-- | server/test/signature/init.lua | 15 |
3 files changed, 68 insertions, 15 deletions
diff --git a/server/src/core/hover.lua b/server/src/core/hover.lua index 9a2500f0..b40c6fa4 100644 --- a/server/src/core/hover.lua +++ b/server/src/core/hover.lua @@ -29,7 +29,6 @@ local function buildLibArgs(lib, oo, select) start = 1 end local strs = {} - local argLabel for i = start, #lib.args do local arg = lib.args[i] if arg.optional then @@ -44,6 +43,9 @@ local function buildLibArgs(lib, oo, select) end local argStr = {} + if i == select then + argStr[#argStr+1] = '@ARG' + end if arg.name then argStr[#argStr+1] = ('%s: '):format(arg.name) end @@ -55,6 +57,9 @@ local function buildLibArgs(lib, oo, select) if arg.default then argStr[#argStr+1] = ('(%q)'):format(arg.default) end + if i == select then + argStr[#argStr+1] = '@ARG' + end for _, str in ipairs(argStr) do strs[#strs+1] = str @@ -62,16 +67,29 @@ local function buildLibArgs(lib, oo, select) if arg.optional == 'self' then strs[#strs+1] = ']' end - if i == select then - argLabel = table.concat(argStr) - end end for _, arg in ipairs(lib.args) do if arg.optional == 'after' then strs[#strs+1] = ']' end end - return table.concat(strs), argLabel + local text = table.concat(strs) + local argLabel = {} + for i = 1, 2 do + local pos = text:find('@ARG', 1, true) + if pos then + if i == 1 then + argLabel[i] = pos + else + argLabel[i] = pos - 1 + end + text = text:sub(1, pos-1) .. text:sub(pos+4) + end + end + if #argLabel == 0 then + argLabel = nil + end + return text, argLabel end local function buildLibReturns(lib) @@ -177,7 +195,12 @@ local function getFunctionHoverAsLib(name, lib, oo, select) local returns = buildLibReturns(lib) local enum = buildEnum(lib) local tip = lib.description + local headLen = #('function %s('):format(name) local title = ('function %s(%s)%s'):format(name, args, returns) + if argLabel then + argLabel[1] = argLabel[1] + headLen + argLabel[2] = argLabel[2] + headLen + end return { label = title, description = tip, diff --git a/server/src/core/hover_function.lua b/server/src/core/hover_function.lua index 4573bcc2..42a5f766 100644 --- a/server/src/core/hover_function.lua +++ b/server/src/core/hover_function.lua @@ -12,7 +12,6 @@ local function buildValueArgs(func, oo, select) end end local strs = {} - local argLabel local start = 1 if oo then start = 2 @@ -27,21 +26,46 @@ local function buildValueArgs(func, oo, select) max = math.max(#names, #values) end for i = start, max do + if i > start then + strs[#strs+1] = ', ' + end local name = names[i] local value = values[i] or 'any' + if i == select then + strs[#strs+1] = '@ARG' + end if name then strs[#strs+1] = name .. ': ' .. value else strs[#strs+1] = value end if i == select then - argLabel = strs[#strs] + strs[#strs+1] = '@ARG' end end if func.hasDots then + if max > 0 then + strs[#strs+1] = ', ' + end strs[#strs+1] = '...' end - return table.concat(strs, ', '), argLabel + local text = table.concat(strs) + local argLabel = {} + for i = 1, 2 do + local pos = text:find('@ARG', 1, true) + if pos then + if i == 1 then + argLabel[i] = pos + else + argLabel[i] = pos - 1 + end + text = text:sub(1, pos-1) .. text:sub(pos+4) + end + end + if #argLabel == 0 then + argLabel = nil + end + return text, argLabel end local function buildValueReturns(func) @@ -63,7 +87,12 @@ end return function (name, func, oo, select) local args, argLabel = buildValueArgs(func, oo, select) local returns = buildValueReturns(func) + local headLen = #('function %s('):format(name) local title = ('function %s(%s)%s'):format(name, args, returns) + if argLabel then + argLabel[1] = argLabel[1] + headLen + argLabel[2] = argLabel[2] + headLen + end return { label = title, argLabel = argLabel, diff --git a/server/test/signature/init.lua b/server/test/signature/init.lua index 0f16af91..9096c826 100644 --- a/server/test/signature/init.lua +++ b/server/test/signature/init.lua @@ -19,7 +19,8 @@ function TEST(script) local arg = hover.argLabel assert(expect.label == label) - assert(expect.arg == arg) + assert(expect.arg[1] == arg[1]) + assert(expect.arg[2] == arg[2]) else assert(expect == nil) end @@ -34,7 +35,7 @@ x(@ ]] { label = "function x(a: any, b: any)", - arg = 'a: any' + arg = {12, 17}, } TEST [[ @@ -45,7 +46,7 @@ x(@) ]] { label = "function x(a: any, b: any)", - arg = 'a: any' + arg = {12, 17}, } TEST [[ @@ -56,7 +57,7 @@ x(xxx@) ]] { label = "function x(a: any, b: any)", - arg = 'a: any' + arg = {12, 17}, } TEST [[ @@ -67,7 +68,7 @@ x(xxx, @) ]] { label = "function x(a: any, b: any)", - arg = 'b: any' + arg = {20, 25}, } TEST [[ @@ -78,7 +79,7 @@ mt:f(@ ]] { label = 'function mt:f(a: any)', - arg = 'a: any' + arg = {15, 20}, } TEST [[ @@ -89,7 +90,7 @@ TEST [[ function *string:sub(i: integer [, j: integer(-1)]) -> string ]], - arg = 'i: integer' + arg = {22, 31}, } TEST [[ |