diff options
-rw-r--r-- | changelog.md | 1 | ||||
-rw-r--r-- | script/core/signature.lua | 26 | ||||
-rw-r--r-- | script/vm/compiler.lua | 1 | ||||
-rw-r--r-- | test/signature/init.lua | 134 |
4 files changed, 121 insertions, 41 deletions
diff --git a/changelog.md b/changelog.md index b5374d1c..77e11e03 100644 --- a/changelog.md +++ b/changelog.md @@ -45,6 +45,7 @@ --![](image.png) --> will convert to `--![](file:///xxxx/image.png)` local x ``` +* `CHG` signature: only show signatures matching the entered parameters * `FIX` [#880](https://github.com/sumneko/lua-language-server/issues/880) * `FIX` [#1284](https://github.com/sumneko/lua-language-server/issues/1284) * `FIX` [#1292](https://github.com/sumneko/lua-language-server/issues/1292) diff --git a/script/core/signature.lua b/script/core/signature.lua index 91b1156d..21e954bf 100644 --- a/script/core/signature.lua +++ b/script/core/signature.lua @@ -68,27 +68,30 @@ local function makeOneSignature(source, oop, index) } end -- 不定参数 - if index > i and i > 0 then + if index and index > i and i > 0 then local lastLabel = params[i].label local text = label:sub(lastLabel[1] + 1, lastLabel[2]) if text:sub(1, 3) == '...' then index = i end end + if #params < (index or 0) then + return nil + end return { label = label, params = params, - index = index, + index = index or 1, description = hoverDesc(source), } end ---@async local function makeSignatures(text, call, pos) - local node = call.node - local oop = node.type == 'method' - or node.type == 'getmethod' - or node.type == 'setmethod' + local func = call.node + local oop = func.type == 'method' + or func.type == 'getmethod' + or func.type == 'setmethod' local index if call.args then local args = {} @@ -124,13 +127,13 @@ local function makeSignatures(text, call, pos) index = #args end end - else - index = 1 end local signs = {} - local defs = vm.getDefs(node) + local node = vm.compileNode(func) + ---@type vm.node + node = node:getData 'originNode' or node local mark = {} - for _, src in ipairs(defs) do + for src in node:eachObject() do if src.type == 'function' or src.type == 'doc.type.function' then if not mark[src] then @@ -159,5 +162,8 @@ return function (uri, pos) if not signs or #signs == 0 then return nil end + table.sort(signs, function (a, b) + return #a.params < #b.params + end) return signs end diff --git a/script/vm/compiler.lua b/script/vm/compiler.lua index 1335ef7b..3c16a4d8 100644 --- a/script/vm/compiler.lua +++ b/script/vm/compiler.lua @@ -559,6 +559,7 @@ local function matchCall(source) if needRemove then local newNode = myNode:copy() newNode:removeNode(needRemove) + newNode:setData('originNode', myNode) vm.setNode(source, newNode, true) end end diff --git a/test/signature/init.lua b/test/signature/init.lua index 207fc0b7..9c05f7c5 100644 --- a/test/signature/init.lua +++ b/test/signature/init.lua @@ -8,18 +8,23 @@ rawset(_G, 'TEST', true) function TEST(script) return function (expect) local newScript, catched1 = catch(script, '?') - local newExpect, catched2 = catch(expect or '', '!') files.setText('', newScript) local hovers = core('', catched1['?'][1][1]) if hovers then - assert(expect) - local hover = hovers[#hovers] - - local arg = hover.params[hover.index].label - - assert(newExpect == hover.label) - assert(catched2['!'][1][1] == arg[1]) - assert(catched2['!'][1][2] == arg[2]) + assert(#hovers == #expect) + for i, hover in ipairs(hovers) do + local newExpect, catched2 = catch(expect[i], '!') + local arg = hover.params[hover.index] + + assert(newExpect == hover.label) + if arg then + assert(catched2['!'][1] ~= nil) + assert(catched2['!'][1][1] == arg.label[1]) + assert(catched2['!'][1][2] == arg.label[2]) + else + assert(#catched2['!'] == 0) + end + end else assert(expect == nil) end @@ -33,7 +38,7 @@ end x(<??> ]] -'function x(<!a: any!>, b: any)' +{'function x(<!a: any!>, b: any)'} TEST [[ local function x(a, b) @@ -41,7 +46,7 @@ end x(<??>) ]] -'function x(<!a: any!>, b: any)' +{'function x(<!a: any!>, b: any)'} TEST [[ local function x(a, b) @@ -49,7 +54,7 @@ end x(xxx<??>) ]] -'function x(<!a: any!>, b: any)' +{'function x(<!a: any!>, b: any)'} TEST [[ local function x(a, b) @@ -57,7 +62,7 @@ end x(xxx, <??>) ]] -'function x(a: any, <!b: any!>)' +{'function x(a: any, <!b: any!>)'} TEST [[ function mt:f(a) @@ -65,7 +70,7 @@ end mt:f(<??> ]] -'(method) mt:f(<!a: any!>)' +{'(method) mt:f(<!a: any!>)'} TEST [[ local function x(a, b) @@ -74,7 +79,7 @@ end x(<??> ]] -'function x(<!a: any!>, b: any)' +{'function x(<!a: any!>, b: any)'} TEST [[ local function x(a, ...) @@ -83,12 +88,12 @@ end x(1, 2, 3, <??> ]] -'function x(a: any, <!...any!>)' +{'function x(a: any, <!...any!>)'} TEST [[ (''):sub(<??> ]] -'(method) string:sub(<!i: integer!>, j?: integer)' +{'(method) string:sub(<!i: integer!>, j?: integer)'} TEST [[ (''):sub(1)<??> @@ -101,7 +106,7 @@ end f(1, 'string<??>') ]] -'function f(a: any, <!b: any!>, c: any)' +{'function f(a: any, <!b: any!>, c: any)'} TEST [[ pcall(function () <??> end) @@ -118,7 +123,7 @@ TEST [[ local zzzz zzzz(<??>) ]] -'function zzzz(<!x: number!>, y: number)' +{'function zzzz(<!x: number!>, y: number)'} TEST [[ ('abc'):format(f(<??>)) @@ -132,7 +137,7 @@ end Foo(<??>) ]] -'function Foo(<!param01: any!>, param02: any)' +{'function Foo(<!param01: any!>, param02: any)'} TEST [[ function f1(a, b) @@ -143,7 +148,7 @@ end f2(f1(),<??>) ]] -'function f2(c: any, <!d: any!>)' +{'function f2(c: any, <!d: any!>)'} TEST [[ local function f(a, b, c) @@ -151,13 +156,13 @@ end f({},<??>) ]] -'function f(a: any, <!b: any!>, c: any)' +{'function f(a: any, <!b: any!>, c: any)'} TEST [[ for _ in pairs(<??>) do end ]] -'function pairs(<!t: <T>!>)' +{'function pairs(<!t: <T>!>)'} TEST [[ function m:f() @@ -165,7 +170,7 @@ end m.f(<??>) ]] -'function m.f(<!self: any!>)' +{'function m.f(<!self: any!>)'} TEST [[ ---@alias nnn table<number, string> @@ -175,7 +180,7 @@ local function f(x, y, z) end f(<??>) ]] -'function f(<!x: table<number, string>!>, y: any, z: any)' +{'function f(<!x: table<number, string>!>, y: any, z: any)'} TEST [[ local function x(a, b) @@ -183,7 +188,7 @@ end x( aaaa <??>, 2) ]] -"function x(<!a: any!>, b: any)" +{"function x(<!a: any!>, b: any)"} TEST [[ local function x(a, b) @@ -191,7 +196,7 @@ end x(<??> aaaa , 2) ]] -'function x(<!a: any!>, b: any)' +{'function x(<!a: any!>, b: any)'} TEST [[ local function x(a, b) @@ -199,7 +204,7 @@ end x(aaaa ,<??> 2) ]] -'function x(a: any, <!b: any!>)' +{'function x(a: any, <!b: any!>)'} TEST [[ local function x(a, b) @@ -207,7 +212,7 @@ end x(aaaa , 2 <??>) ]] -'function x(a: any, <!b: any!>)' +{'function x(a: any, <!b: any!>)'} TEST [[ local fooC @@ -221,10 +226,77 @@ fooC(function (x, s) end,<??>) ]] -'function fooC(callback: fun(x: number, s: string):nil, <!par: number!>)' +{'function fooC(callback: fun(x: number, s: string):nil, <!par: number!>)'} TEST [[ (function (a, b) end)(<??>) ]] -'function (<!a: any!>, b: any)' +{'function (<!a: any!>, b: any)'} + +TEST [[ +function X() end + +---@param a number +function X(a) end + +---@param a number +---@param b number +function X(a, b) end + +X(<??>) +]] +{ +'function X()', +'function X(<!a: number!>)', +'function X(<!a: number!>, b: number)', +} + +TEST [[ +function X() end + +---@param a number +function X(a) end + +---@param a number +---@param b number +function X(a, b) end + +X(<?1?>) +]] +{ +'function X(<!a: number!>)', +'function X(<!a: number!>, b: number)', +} + +TEST [[ +function X() end + +---@param a number +function X(a) end + +---@param a number +---@param b number +function X(a, b) end + +X(1, <??>) +]] +{ +'function X(a: number, <!b: number!>)', +} + +TEST [[ +function X() end + +---@param a number +function X(a) end + +---@param a number +---@param b number +function X(a, b) end + +X(1, <?2?>) +]] +{ +'function X(a: number, <!b: number!>)', +} |