diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2020-07-22 18:59:36 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2020-07-22 18:59:36 +0800 |
commit | 8a3e5ec178a45a68e468a504f06452308ba2f22c (patch) | |
tree | 423436acff108a22872e3e8acbca9314c20e8007 | |
parent | 9212e6a1b16ca4eac39667ceafd12b63cfef0bbb (diff) | |
download | lua-language-server-8a3e5ec178a45a68e468a504f06452308ba2f22c.zip |
用 interface 解决require问题
-rw-r--r-- | script-beta/vm/eachDef.lua | 24 | ||||
-rw-r--r-- | script-beta/vm/guideInterface.lua | 47 | ||||
-rw-r--r-- | script-beta/vm/init.lua | 1 | ||||
-rw-r--r-- | script-beta/vm/vm.lua | 11 |
4 files changed, 66 insertions, 17 deletions
diff --git a/script-beta/vm/eachDef.lua b/script-beta/vm/eachDef.lua index 65878ebf..104b77ba 100644 --- a/script-beta/vm/eachDef.lua +++ b/script-beta/vm/eachDef.lua @@ -5,17 +5,7 @@ local files = require 'files' local m = {} -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) +function m.searchFileReturn(results, ast) local returns = ast.returns for _, ret in ipairs(returns) do if ret[1] then @@ -24,7 +14,7 @@ function m.searchFileReturn(results, ast, source) end end -function m.require(results, source, args) +function m.require(results, args) local reqName = args[1] and args[1][1] if not reqName then return @@ -33,7 +23,7 @@ function m.require(results, source, args) for _, uri in ipairs(uris) do local ast = files.getAst(uri) if ast then - m.searchFileReturn(results, ast.ast, source) + m.searchFileReturn(results, ast.ast) end end end @@ -44,7 +34,7 @@ function m.searchDefAcrossRequire(results) if func and index == 1 then local lib = vm.getLibrary(func) if lib and lib.name == 'require' then - m.require(results, source, args) + m.require(results, args) end end end @@ -58,7 +48,7 @@ function m.searchLibrary(source, results) if not lib then return end - m.mergeResults(results, { lib }) + vm.mergeResults(results, { lib }) end function m.eachDef(source, results) @@ -68,9 +58,9 @@ function m.eachDef(source, results) return results end - local myResults = guide.requestDefinition(source) + local myResults = guide.requestDefinition(source, vm.interface) m.searchDefAcrossRequire(myResults) - m.mergeResults(results, myResults) + vm.mergeResults(results, myResults) m.searchLibrary(source, results) m.searchLibrary(guide.getObjectValue(source), results) diff --git a/script-beta/vm/guideInterface.lua b/script-beta/vm/guideInterface.lua new file mode 100644 index 00000000..db576032 --- /dev/null +++ b/script-beta/vm/guideInterface.lua @@ -0,0 +1,47 @@ +local vm = require 'vm.vm' +local files = require 'files' +local ws = require 'workspace' + +local m = {} + +function m.searchFileReturn(results, ast) + local returns = ast.returns + for _, ret in ipairs(returns) do + if ret[1] then + if ret[1].type == 'table' then + vm.mergeResults(results, { ret[1] }) + else + local newRes = vm.getDefs(ret[1]) + vm.mergeResults(results, newRes) + end + end + end +end + +function m.require(args) + local reqName = args[1] and args[1][1] + if not reqName then + return nil + end + local results = {} + 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) + end + end + return results +end + +vm.interface = {} + +function vm.interface.call(func, args, index) + local lib = vm.getLibrary(func) + if not lib then + return nil + end + if lib.name == 'require' and index == 1 then + return m.require(args) + end +end diff --git a/script-beta/vm/init.lua b/script-beta/vm/init.lua index df6b87fa..00aaff2e 100644 --- a/script-beta/vm/init.lua +++ b/script-beta/vm/init.lua @@ -10,4 +10,5 @@ require 'vm.eachField' require 'vm.eachDef' require 'vm.eachRef' require 'vm.getCall' +require 'vm.guideInterface' return vm diff --git a/script-beta/vm/vm.lua b/script-beta/vm/vm.lua index 5028a67c..26f1f190 100644 --- a/script-beta/vm/vm.lua +++ b/script-beta/vm/vm.lua @@ -6,6 +6,7 @@ local assert = assert local require = require local type = type local running = coroutine.running +local ipairs = ipairs _ENV = nil @@ -139,6 +140,16 @@ function m.getKeyName(source) return guide.getKeyName(source) end +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 + m.cacheTracker = setmetatable({}, { __mode = 'kv' }) --- 刷新缓存 |