diff options
-rw-r--r-- | server/src/core/completion.lua | 4 | ||||
-rw-r--r-- | server/src/core/hover/function.lua | 3 | ||||
-rw-r--r-- | server/src/core/hover/lib_function.lua | 3 | ||||
-rw-r--r-- | server/src/core/signature.lua | 92 | ||||
-rw-r--r-- | server/src/vm/vm.lua | 2 | ||||
-rw-r--r-- | server/test/signature/init.lua | 3 |
6 files changed, 54 insertions, 53 deletions
diff --git a/server/src/core/completion.lua b/server/src/core/completion.lua index 621fe2cf..92a93552 100644 --- a/server/src/core/completion.lua +++ b/server/src/core/completion.lua @@ -352,9 +352,9 @@ local function searchCallArg(vm, source, word, callback, pos) return end - local select = 1 + local select = #args + 1 for i, arg in ipairs(args) do - if arg.start <= pos and arg.finish >= pos then + if arg.start <= pos and arg.finish >= pos - 1 then select = i break end diff --git a/server/src/core/hover/function.lua b/server/src/core/hover/function.lua index 6be90b06..f8f7655e 100644 --- a/server/src/core/hover/function.lua +++ b/server/src/core/hover/function.lua @@ -21,9 +21,6 @@ local function buildValueArgs(func, object, select) local start = 1 if object then start = 2 - if select then - select = select + 1 - end end local max if func.source then diff --git a/server/src/core/hover/lib_function.lua b/server/src/core/hover/lib_function.lua index cb67c7f9..c3caea7a 100644 --- a/server/src/core/hover/lib_function.lua +++ b/server/src/core/hover/lib_function.lua @@ -6,9 +6,6 @@ local function buildLibArgs(lib, object, select) local start if object then start = 2 - if select then - select = select + 1 - end else start = 1 end diff --git a/server/src/core/signature.lua b/server/src/core/signature.lua index dd5e91bd..7d217593 100644 --- a/server/src/core/signature.lua +++ b/server/src/core/signature.lua @@ -1,54 +1,63 @@ -local hover = require 'core.hover' +local getFunctionHover = require 'core.hover.function' +local getFunctionHoverAsLib = require 'core.hover.lib_function' +local findLib = require 'core.find_lib' +local buildValueName = require 'core.hover.name' -local function isContainPos(obj, pos) - if obj.start <= pos and obj.finish >= pos then - return true +local function findCall(vm, pos) + local results = {} + for _, src in ipairs(vm.sources) do + if src.type == 'call' + and src.start <= pos + and src.finish >= pos + then + results[#results+1] = src + end end - return false -end - -local function isContainArgPos(obj, pos) - if obj.start <= pos and obj.finish+1 >= pos then - return true + if #results == 0 then + return nil end - return false + -- 可能处于 'func1(func2(' 的嵌套中,将最近的call放到最前面 + table.sort(results, function (a, b) + return a.start > b.start + end) + return results end -local function findArgCount(args, pos) +local function getSelect(args, pos) for i, arg in ipairs(args) do - if isContainArgPos(arg, pos) then - return i, arg + if arg.start <= pos and arg.finish >= pos - 1 then + return i end end - return #args + 1, nil + return #args + 1 end --- 找出范围包含pos的call -local function findCall(vm, pos) - local results = {} - for _, call in ipairs(vm.results.calls) do - if isContainPos(call.args, pos) then - local n, arg = findArgCount(call.args, pos) - if arg and arg.type == 'string' then - return nil - end - local var = call.lastObj.bind - if var then - results[#results+1] = { - func = call.func, - var = var, - source = call.lastObj, - select = n, - args = call.args, - } - end +local function getFunctionSource(call) + local simple = call:get 'simple' + for i, source in ipairs(simple) do + if source == call then + return simple[i-1] end end - -- 可能处于 'func1(func2(' 的嵌套中,因此距离越远的函数层级越低 - table.sort(results, function (a, b) - return a.args.start < b.args.start - end) - return results + return nil +end + +local function getHover(call, pos) + local func, args = call:bindCall() + local select = getSelect(args, pos) + local source = getFunctionSource(call) + local object = source:get 'object' + local lib, fullkey = findLib(source) + local name = fullkey or buildValueName(source) + local hover + if lib then + hover = getFunctionHoverAsLib(name, lib, object, select) + else + hover = getFunctionHover(name, func:getFunction(), object, select) + end + if hover and hover.argLabel then + return hover + end end return function (vm, pos) @@ -59,10 +68,7 @@ return function (vm, pos) local hovers = {} for _, call in ipairs(calls) do - local hvr = hover(call.var, call.source, nil, call.select) - if hvr and hvr.argLabel then - hovers[#hovers+1] = hvr - end + hovers[#hovers+1] = getHover(call, pos) end if #hovers == 0 then diff --git a/server/src/vm/vm.lua b/server/src/vm/vm.lua index 41015923..009de868 100644 --- a/server/src/vm/vm.lua +++ b/server/src/vm/vm.lua @@ -467,7 +467,7 @@ function mt:getSimple(simple, max) local func = value if object then table.insert(values, 1, object) - table.insert(args, 1, simple[i-1]) + table.insert(args, 1, simple[i-3]) end object = nil source:bindCall(func, args) diff --git a/server/test/signature/init.lua b/server/test/signature/init.lua index 9096c826..acca3260 100644 --- a/server/test/signature/init.lua +++ b/server/test/signature/init.lua @@ -1,5 +1,6 @@ local parser = require 'parser' local core = require 'core' +local buildVM = require 'vm' rawset(_G, 'TEST', true) @@ -8,7 +9,7 @@ function TEST(script) local pos = script:find('@', 1, true) local new_script = script:gsub('@', '') local ast = parser:ast(new_script) - local vm = core.vm(ast) + local vm = buildVM(ast) assert(vm) local hovers = core.signature(vm, pos) if hovers then |