diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2019-03-13 10:17:10 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2019-03-13 10:17:10 +0800 |
commit | 52d631e2e5879c218adb0a969d337fc85732b593 (patch) | |
tree | 1700a3169d04ade1ff9f6a3bbd6ea0fc44c2f0f8 /server | |
parent | fe7b7bd71785844dd34aea9e2a1da9f8b9e6620a (diff) | |
download | lua-language-server-52d631e2e5879c218adb0a969d337fc85732b593.zip |
换个实现
Diffstat (limited to 'server')
-rw-r--r-- | server/src/vm/source.lua | 7 | ||||
-rw-r--r-- | server/src/vm/value.lua | 48 | ||||
-rw-r--r-- | server/src/vm/vm.lua | 5 |
3 files changed, 22 insertions, 38 deletions
diff --git a/server/src/vm/source.lua b/server/src/vm/source.lua index 9528a67f..83f9569b 100644 --- a/server/src/vm/source.lua +++ b/server/src/vm/source.lua @@ -66,13 +66,6 @@ function mt:setVM() self.vm = vm end -function mt:subscribe(value) - if self.uri == value.uri then - return - end - self.vm._subscribted[value] = true -end - function mt:isRemoved() return self.vm:isRemoved() end diff --git a/server/src/vm/value.lua b/server/src/vm/value.lua index d747f6ce..ee173c38 100644 --- a/server/src/vm/value.lua +++ b/server/src/vm/value.lua @@ -9,6 +9,9 @@ local function getDefaultSource() } end +local weakMt = { __mode = 'k' } +local Sort = 0 + local mt = {} mt.__index = mt mt.type = 'value' @@ -22,7 +25,7 @@ local function create (tp, source, literal) source = source or getDefaultSource(), _type = {}, _literal = literal, - _info = {}, + _info = setmetatable({}, weakMt), }, mt) if type(tp) == 'table' then for i = 1, #tp do @@ -219,13 +222,8 @@ function mt:mergeValue(value) self._child[k] = v end end - for _, info in ipairs(value._info) do - if not self._info[info.source] then - self._info[#self._info+1] = info - self._info[info.source] = true - - info.source:subscribe(self) - end + for source, info in pairs(value._info) do + self._info[source] = info end if value._meta then self._meta = value._meta @@ -248,23 +246,32 @@ function mt:addInfo(tp, source, ...) if self._info[source] then return end - if not source or not source.subscribe then + if not source then + return + end + if source.uri ~= self.uri then return end + Sort = Sort + 1 local info = { type = tp, source = source, + _sort = Sort, ... } - self._info[#self._info+1] = info - self._info[info.source] = true - - source:subscribe(self) + self._info[source] = info end function mt:eachInfo(callback) - for _, info in ipairs(self._info) do - local res = callback(info) + local list = {} + for _, info in pairs(self._info) do + list[#list+1] = info + end + table.sort(list, function (a, b) + return a._sort < b._sort + end) + for i = 1, #list do + local res = callback(list[i]) if res ~= nil then return res end @@ -272,17 +279,6 @@ function mt:eachInfo(callback) return nil end -function mt:cleanInfo() - for i = #self._info, 1, -1 do - local info = self._info[i] - local source = info.source - if source:isRemoved() then - self._info[source] = nil - table.remove(self._info, i) - end - end -end - function mt:setFunction(func) self._func = func end diff --git a/server/src/vm/vm.lua b/server/src/vm/vm.lua index 88a272de..aeaffc13 100644 --- a/server/src/vm/vm.lua +++ b/server/src/vm/vm.lua @@ -1143,10 +1143,6 @@ end function mt:remove() self._removed = true - for value in pairs(self._subscribted) do - self._subscribted[value] = nil - value:cleanInfo() - end end local function compile(ast, lsp, uri) @@ -1157,7 +1153,6 @@ local function compile(ast, lsp, uri) env = nil, lsp = lsp, uri = uri or '', - _subscribted = {}, }, mt) -- 创建初始环境 |