diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2020-07-21 22:16:18 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2020-07-21 22:16:18 +0800 |
commit | 050b7262a4019a1ecafac0acdc5bd9a1b0ca0bf4 (patch) | |
tree | 508723afe0c2d322582cf8116b21e3625e9f681d | |
parent | de8ca882f4b50b667c6f4c647d26937a869f8546 (diff) | |
download | lua-language-server-050b7262a4019a1ecafac0acdc5bd9a1b0ca0bf4.zip |
支持单对象跨文件
-rw-r--r-- | script-beta/core/definition.lua | 2 | ||||
-rw-r--r-- | script-beta/parser/guide.lua | 13 | ||||
-rw-r--r-- | script-beta/vm/eachDef.lua | 73 | ||||
-rw-r--r-- | script-beta/vm/getCall.lua | 5 | ||||
-rw-r--r-- | script-beta/vm/getRequire.lua | 0 | ||||
-rw-r--r-- | script-beta/vm/init.lua | 1 |
6 files changed, 71 insertions, 23 deletions
diff --git a/script-beta/core/definition.lua b/script-beta/core/definition.lua index fa13c010..6a895256 100644 --- a/script-beta/core/definition.lua +++ b/script-beta/core/definition.lua @@ -89,7 +89,7 @@ return function (uri, offset) vm.eachDef(source, function (src) results[#results+1] = { target = src, - uri = files.getOriginUri(uri), + uri = guide.getRoot(src).uri, source = source, } end) diff --git a/script-beta/parser/guide.lua b/script-beta/parser/guide.lua index 22e49c90..5eff3e1d 100644 --- a/script-beta/parser/guide.lua +++ b/script-beta/parser/guide.lua @@ -936,6 +936,19 @@ function m.getCallAndArgIndex(callarg) return call, index end +-- 根据函数调用的返回值,获取:调用的函数,参数列表,自己是第几个返回值 +function m.getCallValue(source) + local value = source.value + if not value or value.type ~= 'select' then + return + end + local call = value.vararg + if call.type ~= 'call' then + return + end + return call.node, call.args, value.index +end + function m.getNextRef(ref) local nextRef = ref.next if nextRef then diff --git a/script-beta/vm/eachDef.lua b/script-beta/vm/eachDef.lua index d887854d..92982660 100644 --- a/script-beta/vm/eachDef.lua +++ b/script-beta/vm/eachDef.lua @@ -1,26 +1,56 @@ local vm = require 'vm.vm' -local req = require 'vm.getRequire' local guide = require 'parser.guide' +local ws = require 'workspace' +local files = require 'files' local m = {} -function m.searchDefAcrossRequire(results) - for _, source in ipairs(results) do +function m.mergeResults(a, b) + for _, r in ipairs(b) do + if not a[r] then + a[r] = true + a[#a+1] = r + end + end + return a +end +function m.searchFileReturn(results, ast, source) + local returns = ast.returns + for _, ret in ipairs(returns) do + if ret[1] then + m.eachDef(ret[1], results) + end end end -function m.mergeResults(a, b, mark) - for _, r in ipairs(b) do - if not mark[r] then - mark[r] = true - a[#a+1] = r +function m.require(results, source, args) + local reqName = args[1] and args[1][1] + if not reqName then + return + end + local uris = ws.findUrisByRequirePath(reqName, true) + for _, uri in ipairs(uris) do + local ast = files.getAst(uri) + if ast then + m.searchFileReturn(results, ast.ast, source) end end - return a end -function m.searchLibrary(source, results, mark) +function m.searchDefAcrossRequire(results) + for _, source in ipairs(results) do + local func, args, index = guide.getCallValue(source) + if func and index == 1 then + local lib = vm.getLibrary(func) + if lib and lib.name == 'require' then + m.require(results, source, args) + end + end + end +end + +function m.searchLibrary(source, results) if not source then return end @@ -28,24 +58,23 @@ function m.searchLibrary(source, results, mark) if not lib then return end - if mark[lib] then - return - end - mark[lib] = true - results[#results+1] = lib + m.mergeResults(results, { lib }) end -function m.eachDef(source, results, mark) +function m.eachDef(source, results) results = results or {} - mark = mark or {} - if mark[source] then + local lock = vm.lock('eachDef', source) + if not lock then return results end - m.mergeResults(results, guide.requestDefinition(source), mark) - m.searchLibrary(source, results, mark) - m.searchLibrary(guide.getObjectValue(source), results, mark) - m.searchDefAcrossRequire(results, mark) + local myResults = guide.requestDefinition(source) + m.searchDefAcrossRequire(myResults) + m.mergeResults(results, myResults) + m.searchLibrary(source, results) + m.searchLibrary(guide.getObjectValue(source), results) + + lock() return results end diff --git a/script-beta/vm/getCall.lua b/script-beta/vm/getCall.lua new file mode 100644 index 00000000..f769633e --- /dev/null +++ b/script-beta/vm/getCall.lua @@ -0,0 +1,5 @@ +local vm = require 'vm.vm' + +function vm.getCallRecv(source) + +end diff --git a/script-beta/vm/getRequire.lua b/script-beta/vm/getRequire.lua deleted file mode 100644 index e69de29b..00000000 --- a/script-beta/vm/getRequire.lua +++ /dev/null diff --git a/script-beta/vm/init.lua b/script-beta/vm/init.lua index 8cc7ae92..df6b87fa 100644 --- a/script-beta/vm/init.lua +++ b/script-beta/vm/init.lua @@ -9,4 +9,5 @@ require 'vm.getMeta' require 'vm.eachField' require 'vm.eachDef' require 'vm.eachRef' +require 'vm.getCall' return vm |