diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2019-10-10 16:03:28 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2019-10-10 16:03:28 +0800 |
commit | c80ebc80ec68909cb890cccba05ba5fded59b645 (patch) | |
tree | 3687a0438d81b2aebb013557798b773076e922fa /server-beta/src | |
parent | 63f8fe059b3b429baf0e10969a259beecaddbaad (diff) | |
download | lua-language-server-c80ebc80ec68909cb890cccba05ba5fded59b645.zip |
支持_G
Diffstat (limited to 'server-beta/src')
-rw-r--r-- | server-beta/src/core/_G.lua | 34 | ||||
-rw-r--r-- | server-beta/src/core/engineer.lua | 92 | ||||
-rw-r--r-- | server-beta/src/core/getglobal.lua | 31 | ||||
-rw-r--r-- | server-beta/src/core/local.lua | 15 | ||||
-rw-r--r-- | server-beta/src/parser/guide.lua | 7 |
5 files changed, 78 insertions, 101 deletions
diff --git a/server-beta/src/core/_G.lua b/server-beta/src/core/_G.lua deleted file mode 100644 index 3a28c7ac..00000000 --- a/server-beta/src/core/_G.lua +++ /dev/null @@ -1,34 +0,0 @@ -local m = {} - -function m:def(source, callback) - local parent = source.parent - if parent.type == 'setfield' - or parent.type == 'setindex' - or parent.type == 'setmethod' then - callback(parent, 'set') - elseif parent.type == 'getfield' - or parent.type == 'getindex' then - self:search(parent, 'special', 'def', callback) - elseif parent.type == 'callargs' then - self:search(parent.parent, 'special', 'def', callback) - end -end - -function m:ref(source, callback) - local parent = source.parent - if parent.type == 'setfield' - or parent.type == 'setindex' - or parent.type == 'setmethod' then - callback(parent, 'set') - elseif parent.type == 'getfield' - or parent.type == 'getindex' - or parent.type == 'getmethod' then - callback(parent, 'get') - elseif parent.type == 'getfield' then - self:search(parent, 'special', 'ref', callback) - elseif parent.type == 'callargs' then - self:search(parent.parent, 'special', 'ref', callback) - end -end - -return m diff --git a/server-beta/src/core/engineer.lua b/server-beta/src/core/engineer.lua index 05976c76..24a83151 100644 --- a/server-beta/src/core/engineer.lua +++ b/server-beta/src/core/engineer.lua @@ -2,7 +2,6 @@ local guide = require 'parser.guide' local require = require local setmetatable = setmetatable -local ipairs = ipairs _ENV = nil ---@class engineer @@ -13,7 +12,6 @@ mt.type = 'engineer' mt['local'] = require 'core.local' mt['getlocal'] = require 'core.getlocal' mt['setlocal'] = mt['getlocal'] -mt['_G'] = require 'core._G' mt['getglobal'] = require 'core.getglobal' mt['setglobal'] = mt['getglobal'] mt['field'] = require 'core.field' @@ -23,43 +21,33 @@ mt['boolean'] = require 'core.boolean' mt['string'] = require 'core.string' mt['table'] = require 'core.table' ---mt['special'] = function (self, source, mode, callback) --- local name = self:getSpecial(source) --- if not name then --- return --- end --- if mode == 'def' then --- if name == 's|_G' then --- self:search(source, '_G', mode, callback) --- elseif name == 's|rawset' then --- callback(source.parent, 'set') --- end --- end ---end ---mt['call'] = function (self, source, mode, callback) --- local name = self:getSpecial(source) --- if not name then --- return --- end --- local parent = source.parent --- if name == 's|setmetatable' then --- if parent.index ~= 1 then --- return --- end --- self:eachField(source.args[2], 's|__index', 'def', function (src) --- self:eachField(src, nil, 'ref', callback) --- end) --- return --- end ---end - -function mt:getSpecial(source) +function mt:getSpecialName(source) local node = source.node if node.tag ~= '_ENV' then return nil end local name = guide.getKeyName(source) - return name + if name:sub(1, 2) ~= 's|' then + return nil + end + return name:sub(3) +end + +function mt:eachSpecial(callback) + local env = guide.getENV(self.ast) + if not env then + return + end + local refs = env.ref + if not refs then + return + end + for i = 1, #refs do + local ref = refs[i] + if ref.type == 'getglobal' then + callback(ref[1], ref) + end + end end function mt:eachField(source, key, callback) @@ -101,13 +89,39 @@ function mt:eachDef(source, callback) f(self, source, callback) end +function mt:childDef(source, callback) + local tp = source.type + if tp == 'setfield' then + callback(source.field, 'set') + elseif tp == 'setmethod' then + callback(source.field, 'set') + elseif tp == 'setindex' then + callback(source.index, 'set') + end +end + +function mt:childRef(source, callback) + local tp = source.type + if tp == 'setfield' then + callback(source.field, 'set') + elseif tp == 'setmethod' then + callback(source.field, 'set') + elseif tp == 'setindex' then + callback(source.index, 'set') + elseif tp == 'getfield' then + callback(source.field, 'get') + elseif tp == 'getmethod' then + callback(source.method, 'get') + elseif tp == 'getindex' then + callback(source.index, 'get') + end +end + return function (ast) local self = setmetatable({ - step = 0, - ast = ast.ast, + step = 0, + ast = ast.ast, + cache = {}, }, mt) - if not ast.vm then - ast.vm = {} - end return self end diff --git a/server-beta/src/core/getglobal.lua b/server-beta/src/core/getglobal.lua index 74c05e29..5cac5bdd 100644 --- a/server-beta/src/core/getglobal.lua +++ b/server-beta/src/core/getglobal.lua @@ -10,6 +10,14 @@ function m:def(source, callback) callback(src, mode) end end) + self:eachSpecial(function (name, src) + if name == '_G' then + local parent = src.parent + if guide.getKeyName(parent) == key then + self:childDef(parent, callback) + end + end + end) end function m:ref(source, callback) @@ -20,6 +28,14 @@ function m:ref(source, callback) callback(src, mode) end end) + self:eachSpecial(function (name, src) + if name == '_G' then + local parent = src.parent + if guide.getKeyName(parent) == key then + self:childRef(parent, callback) + end + end + end) end function m:field(source, key, callback) @@ -28,20 +44,7 @@ function m:field(source, key, callback) if mode == 'get' then local parent = src.parent if key == guide.getKeyName(parent) then - local tp = parent.type - if tp == 'getfield' then - callback(parent.field, 'get') - elseif tp == 'getmethod' then - callback(parent.method, 'get') - elseif tp == 'getindex' then - callback(parent.index, 'get') - elseif tp == 'setfield' then - callback(parent.field, 'set') - elseif tp == 'setmethod' then - callback(parent.method, 'set') - elseif tp == 'setindex' then - callback(parent.index, 'set') - end + self:childRef(parent) end end end) diff --git a/server-beta/src/core/local.lua b/server-beta/src/core/local.lua index 040da822..b9f12259 100644 --- a/server-beta/src/core/local.lua +++ b/server-beta/src/core/local.lua @@ -46,20 +46,7 @@ function m:field(source, key, callback) if ref.type == 'getlocal' then local parent = ref.parent if key == guide.getKeyName(parent) then - local tp = parent.type - if tp == 'setfield' then - callback(parent.field, 'set') - elseif tp == 'setmethod' then - callback(parent.method, 'set') - elseif tp == 'setindex' then - callback(parent.index, 'set') - elseif tp == 'getfield' then - callback(parent.field, 'get') - elseif tp == 'getmethod' then - callback(parent.method, 'get') - elseif tp == 'getindex' then - callback(parent.index, 'get') - end + self:childRef(parent, callback) end elseif ref.type == 'setlocal' then self:eachField(ref.value, key, callback) diff --git a/server-beta/src/parser/guide.lua b/server-beta/src/parser/guide.lua index 2120fe48..9530f9ce 100644 --- a/server-beta/src/parser/guide.lua +++ b/server-beta/src/parser/guide.lua @@ -405,4 +405,11 @@ function m.getKeyName(obj) return nil end +function m.getENV(ast) + if ast.type ~= 'main' then + return nil + end + return ast.locals[1] +end + return m |