diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2018-12-21 16:50:01 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2018-12-21 16:50:01 +0800 |
commit | 3c5ff2671305041d03cde8dafb05d4aed0cc0a65 (patch) | |
tree | 0f058a92500c7abef99fab007dc30d80107795b6 /server/src/matcher | |
parent | bc0ab7b1fa924076a549cdcaaf3e90a6ef67600f (diff) | |
download | lua-language-server-3c5ff2671305041d03cde8dafb05d4aed0cc0a65.zip |
找当前参数
Diffstat (limited to 'server/src/matcher')
-rw-r--r-- | server/src/matcher/init.lua | 1 | ||||
-rw-r--r-- | server/src/matcher/signature.lua | 66 |
2 files changed, 67 insertions, 0 deletions
diff --git a/server/src/matcher/init.lua b/server/src/matcher/init.lua index 27adbbea..29239370 100644 --- a/server/src/matcher/init.lua +++ b/server/src/matcher/init.lua @@ -8,6 +8,7 @@ local api = { findResult = require 'matcher.find_result', findLib = require 'matcher.find_lib', completion = require 'matcher.completion', + signature = require 'matcher.signature', vm = require 'matcher.vm', } diff --git a/server/src/matcher/signature.lua b/server/src/matcher/signature.lua new file mode 100644 index 00000000..97b8fc00 --- /dev/null +++ b/server/src/matcher/signature.lua @@ -0,0 +1,66 @@ +local hover = require 'matcher.hover' + +local function isContainPos(obj, pos) + if obj.start <= pos and obj.finish + 1 >= pos then + return true + end + return false +end + +local function findArgCount(args, pos) + for i, arg in ipairs(args) do + if isContainPos(arg, pos) then + return i + end + end + return #args + 1 +end + +-- 找出范围包含pos的,且有dirty标记的call +local function findDirtyCall(vm, pos) + local results = {} + for _, call in ipairs(vm.results.calls) do + if call.args.dirty and isContainPos(call.args, pos) then + local n = findArgCount(call.args, pos) + results[#results+1] = { + func = call.func, + var = vm.results.sources[call.lastObj], + source = call.lastObj, + select = n, + args = call.args, + } + end + end + -- 可能处于 'func1(func2(' 的嵌套中,因此距离越远的函数层级越低 + table.sort(results, function (a, b) + return a.args.start < b.args.start + end) + return results +end + +local function parseCall(call) + local results = hover(call.var, call.source, nil, call.select) + return results[1], results[2] +end + +return function (vm, pos) + local calls = findDirtyCall(vm, pos) + if #calls == 0 then + return nil + end + + local results = {} + for i, call in ipairs(calls) do + local label, description = parseCall(call) + results[i] = { + label = label, + description = description, + arg = { + label = 'a', + description = '参数说明', + } + } + end + + return results +end |