summaryrefslogtreecommitdiff
path: root/server/src/vm/label.lua
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2019-03-13 18:03:24 +0800
committer最萌小汐 <sumneko@hotmail.com>2019-03-13 18:03:24 +0800
commit2614c2f6e6a53e4142fe57a184fd7bead11a5f4f (patch)
tree17bd6669b19ac260bed90a9dfea241e4117074ff /server/src/vm/label.lua
parent628a272b7babbeab995cf582ff4337b897ac09e6 (diff)
downloadlua-language-server-2614c2f6e6a53e4142fe57a184fd7bead11a5f4f.zip
解除所有的source循环引用
Diffstat (limited to 'server/src/vm/label.lua')
-rw-r--r--server/src/vm/label.lua78
1 files changed, 64 insertions, 14 deletions
diff --git a/server/src/vm/label.lua b/server/src/vm/label.lua
index b82e98e8..cf24ce5d 100644
--- a/server/src/vm/label.lua
+++ b/server/src/vm/label.lua
@@ -1,32 +1,70 @@
-local function getDefaultSource()
- return {
- start = 0,
- finish = 0,
- uri = '',
- }
-end
+local sourceMgr = require 'vm.source'
+
+local Sort = 0
local mt = {}
mt.__index = mt
mt.type = 'label'
+mt._infoCount = 0
+mt._infoLimit = 10
function mt:getName()
return self.name
end
function mt:addInfo(tp, source)
- if source and not source.start then
- error('Miss start: ' .. table.dump(source))
+ if not source then
+ error('No source')
+ end
+ local id = source.id
+ if not id then
+ error('Not instanted source')
end
- self[#self+1] = {
+ if self._info[id] then
+ return
+ end
+ Sort = Sort + 1
+ local info = {
type = tp,
- source = source or getDefaultSource(),
+ source = id,
+ _sort = Sort,
}
+
+ self._info[id] = info
+ self._infoCount = self._infoCount + 1
+
+ if self._infoCount > self._infoLimit then
+ for srcId in pairs(self._info) do
+ local src = sourceMgr.list[srcId]
+ if not src then
+ self._info[srcId] = nil
+ self._infoCount = self._infoCount - 1
+ end
+ end
+ self._infoLimit = self._infoCount * 2
+ if self._infoLimit < 10 then
+ self._infoLimit = 10
+ end
+ end
end
function mt:eachInfo(callback)
- for _, info in ipairs(self) do
- local res = callback(info)
+ local list = {}
+ for srcId, info in pairs(self._info) do
+ local src = sourceMgr.list[srcId]
+ if src then
+ list[#list+1] = info
+ else
+ self._info[srcId] = nil
+ self._infoCount = self._infoCount - 1
+ end
+ end
+ table.sort(list, function (a, b)
+ return a._sort < b._sort
+ end)
+ for i = 1, #list do
+ local info = list[i]
+ local res = callback(info, sourceMgr.list[info.source])
if res ~= nil then
return res
end
@@ -34,10 +72,22 @@ function mt:eachInfo(callback)
return nil
end
+function mt:getSource()
+ return sourceMgr.list[self.source]
+end
+
return function (name, source)
+ if not source then
+ error('No source')
+ end
+ local id = source.id
+ if not id then
+ error('Not instanted source')
+ end
local self = setmetatable({
name = name,
- source = source,
+ source = id,
+ _info = {},
}, mt)
return self
end