diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2019-03-26 16:11:27 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2019-03-26 16:11:27 +0800 |
commit | 637253ee878d80add56c09ea5ecf3369e99cc40e (patch) | |
tree | 1a4d05d152ed1e2981eb9b17da30087817619cdd /server | |
parent | 813291da49e57e62fd4118f4a3f8a3cf977c264c (diff) | |
download | lua-language-server-637253ee878d80add56c09ea5ecf3369e99cc40e.zip |
重新实现穿透require
Diffstat (limited to 'server')
-rw-r--r-- | server/src/core/global.lua | 6 | ||||
-rw-r--r-- | server/src/method/textDocument/documentSymbol.lua | 1 | ||||
-rw-r--r-- | server/src/method/textDocument/references.lua | 1 | ||||
-rw-r--r-- | server/src/service.lua | 2 | ||||
-rw-r--r-- | server/src/vm/chain.lua | 49 | ||||
-rw-r--r-- | server/src/vm/function.lua | 14 | ||||
-rw-r--r-- | server/src/vm/vm.lua | 46 |
7 files changed, 74 insertions, 45 deletions
diff --git a/server/src/core/global.lua b/server/src/core/global.lua index 007f0fd9..5e466fc8 100644 --- a/server/src/core/global.lua +++ b/server/src/core/global.lua @@ -1,12 +1,6 @@ local mt = {} mt.__index = mt -function mt:compileVM(uri) - if not self.set[uri] and not self.get[uri] then - return - end -end - function mt:markSet(uri) if not uri then return diff --git a/server/src/method/textDocument/documentSymbol.lua b/server/src/method/textDocument/documentSymbol.lua index f9823385..201137c8 100644 --- a/server/src/method/textDocument/documentSymbol.lua +++ b/server/src/method/textDocument/documentSymbol.lua @@ -48,6 +48,7 @@ return function (lsp, params) end t:remove() + timerCache[uri] = nil local symbols = core.documentSymbol(vm) if not symbols then diff --git a/server/src/method/textDocument/references.lua b/server/src/method/textDocument/references.lua index c5a3ebf3..f21625c8 100644 --- a/server/src/method/textDocument/references.lua +++ b/server/src/method/textDocument/references.lua @@ -23,6 +23,7 @@ return function (lsp, params) return end t:remove() + LastTask = nil vm, lines = lsp:getVM(uri) local positions = core.references(vm, position, declarat) diff --git a/server/src/service.lua b/server/src/service.lua index abac804c..dcf8b2e2 100644 --- a/server/src/service.lua +++ b/server/src/service.lua @@ -11,6 +11,7 @@ local buildVM = require 'vm' local sourceMgr = require 'vm.source' local localMgr = require 'vm.local' local valueMgr = require 'vm.value' +local chainMgr = require 'vm.chain' local ErrorCodes = { -- Defined by JSON RPC @@ -708,5 +709,6 @@ return function () _version = 0, }, mt) session.global = core.global(session) + session.chain = chainMgr() return session end diff --git a/server/src/vm/chain.lua b/server/src/vm/chain.lua new file mode 100644 index 00000000..23b38a75 --- /dev/null +++ b/server/src/vm/chain.lua @@ -0,0 +1,49 @@ +local valueMgr = require 'vm.value' +local sourceMgr = require 'vm.source' + +local mt = {} +mt.__index = mt +mt.type = 'chain' + +mt.min = 100 +mt.max = 100 +mt.count = 0 + +function mt:clearCache() + if self.count <= self.max then + return + end + local n = 0 + for uri, value in pairs(self.cache) do + local ok = value:eachInfo(function () + return true + end) + if ok then + n = n + 1 + else + self.cache[uri] = nil + end + end + self.count = n + self.max = self.count * 2 + if self.max < self.min then + self.max = self.min + end +end + +function mt:get(uri) + if not self.cache[uri] then + self.count = self.count + 1 + self:clearCache() + self.cache[uri] = valueMgr.create('any', sourceMgr.dummy()) + self.cache[uri]:markGlobal() + self.cache[uri].uri = uri + end + return self.cache[uri] +end + +return function () + return setmetatable({ + cache = {}, + }, mt) +end diff --git a/server/src/vm/function.lua b/server/src/vm/function.lua index 2ca12e2b..5b7d8682 100644 --- a/server/src/vm/function.lua +++ b/server/src/vm/function.lua @@ -101,6 +101,20 @@ function mt:setReturn(index, value) end end +function mt:mergeReturn(index, value) + self:set('hasReturn', true) + if not self.returns then + self.returns = createMulti() + end + if value then + if self.returns[index] then + self.returns[index]:mergeValue(value) + else + self.returns[index] = value + end + end +end + function mt:getReturn(index) if self.maxReturns and index and self.maxReturns < index then return nil diff --git a/server/src/vm/vm.lua b/server/src/vm/vm.lua index c519ad72..940e822f 100644 --- a/server/src/vm/vm.lua +++ b/server/src/vm/vm.lua @@ -153,31 +153,6 @@ function mt:callSetMetaTable(func, values, source) values[1]:setMetaTable(values[2]) end -function mt:getRequire(strValue, destVM) - -- 取出对方的主函数 - local main = destVM.main - -- 获取主函数返回值 - local mainValue = main:getFunction():getReturn(1) - if mainValue then - mainValue:markGlobal() - else - mainValue = self:createValue('any', self:getDefaultSource()) - mainValue.uri = destVM.uri - end - - return mainValue -end - -function mt:getLoadFile(strValue, destVM) - -- 取出对方的主函数 - local main = destVM.main - -- loadfile 的返回值就是对方的主函数 - local mainValue = main - mainValue:markGlobal() - - return mainValue -end - function mt:tryRequireOne(strValue, mode) if not self.lsp or not self.lsp.workspace then return nil @@ -203,18 +178,8 @@ function mt:tryRequireOne(strValue, mode) end strSource:set('target uri', uri) - -- 如果取不到VM(不编译),则做个标记,之后再取一次 - local destVM = self.lsp:getVM(uri) self.lsp:compileChain(self:getUri(), uri) - if destVM then - if mode == 'require' then - return self:getRequire(strValue, destVM) - elseif mode == 'loadfile' then - return self:getLoadFile(strValue, destVM) - elseif mode == 'dofile' then - return self:getRequire(strValue, destVM) - end - end + return self.lsp.chain:get(uri) end return nil end @@ -236,7 +201,7 @@ function mt:callRequire(func, values) else local requireValue = self:tryRequireOne(values[1], 'require') if not requireValue then - requireValue = self:createValue('boolean', self:getDefaultSource()) + requireValue = self:createValue('any', self:getDefaultSource()) requireValue:set('cross file', true) end func:setReturn(1, requireValue) @@ -330,7 +295,7 @@ function mt:call(value, values, source) self:runFunction(func) end else - func:setReturn(1, self:createValue('any', source)) + func:mergeReturn(1, self:createValue('any', source)) end end @@ -734,7 +699,7 @@ function mt:doReturn(action) local func = self:getCurrentFunction() values:eachValue(function (n, value) value.uri = self:getUri() - func:setReturn(n, value) + func:mergeReturn(n, value) local source = action[n] or value:getSource() if not source or source.start == 0 then source = self:getDefaultSource() @@ -1147,6 +1112,9 @@ function mt:createEnvironment(ast) -- 整个文件是一个函数 self.main = self:createFunction(ast) self:setCurrentFunction(self.main:getFunction()) + if self.lsp then + self.main:getFunction():mergeReturn(1, self.lsp.chain:get(self.uri)) + end -- 全局变量`_G` local global = buildGlobal(self.lsp) -- 隐藏的上值`_ENV` |