diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2019-09-25 11:48:50 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2019-09-25 11:48:50 +0800 |
commit | 12e07305cf74f7e98adbfb482b01aceb0c72fd61 (patch) | |
tree | e8b619baee09a550cd5070253b09ea25182538a2 /server-beta/src/core | |
parent | f501deff48a30e937afdcb528aba942648c4888b (diff) | |
download | lua-language-server-12e07305cf74f7e98adbfb482b01aceb0c72fd61.zip |
整理代码
Diffstat (limited to 'server-beta/src/core')
-rw-r--r-- | server-beta/src/core/definition.lua | 42 | ||||
-rw-r--r-- | server-beta/src/core/engineer.lua | 60 |
2 files changed, 78 insertions, 24 deletions
diff --git a/server-beta/src/core/definition.lua b/server-beta/src/core/definition.lua index fd01072e..de041ef2 100644 --- a/server-beta/src/core/definition.lua +++ b/server-beta/src/core/definition.lua @@ -1,4 +1,5 @@ -local guide = require 'parser.guide' +local guide = require 'parser.guide' +local engineer = require 'core.engineer' local m = {} @@ -17,26 +18,17 @@ function m.search(state, ast, source) f(state, ast, source) end -function m.asgetlocal(state, ast, source) - local loc = ast.root[source.loc] - m.search(state, ast, loc) -end - -function m.assetlocal(state, ast, source) - local loc = ast.root[source.loc] - m.search(state, ast, loc) - state.callback(source, ast.uri) -end - function m.aslocal(state, ast, source) - state.callback(source, ast.uri) - if source.ref then - for _, ref in ipairs(source.ref) do - m.search(state, ast, ast.root[ref]) + engineer(ast):eachLocalRef(source, function (src) + if src.type == 'local' or src.type == 'setlocal' then + state.callback(src) end - end + end) end +m.asgetlocal = m.aslocal +m.assetlocal = m.aslocal + function m.globals(state, ast, source) local name = source[1] guide.eachGloabl(ast.root, function (src, gname) @@ -60,16 +52,18 @@ end return function (ast, text, offset) local results = {} local state = { + ast = ast, cache = {}, - callback = function (target, uri) - results[#results+1] = { - uri = uri or ast.uri, - source = source, - target = target, - } - end } + function state.callback(target, uri) + results[#results+1] = { + uri = uri or ast.uri, + source = state.source, + target = target, + } + end guide.eachSource(ast.root, offset, function (source) + state.source = source m.search(state, ast, source) end) if #results == 0 then diff --git a/server-beta/src/core/engineer.lua b/server-beta/src/core/engineer.lua new file mode 100644 index 00000000..e4630670 --- /dev/null +++ b/server-beta/src/core/engineer.lua @@ -0,0 +1,60 @@ +local guide = require 'parser.guide' + +---@class engineer +local mt = {} +mt.__index = mt +mt.type = 'engineer' + +--- 遍历全局变量 +function mt:eachGloabl(root, callback) + guide.eachSourceOf(root, {'setglobal', 'getglobal', 'setfield', 'getfield'}, function (src) + if src.type == 'setglobal' or src.type == 'getglobal' then + callback(src, src[1]) + elseif src.type == 'setfield' or src.type == 'getfield' then + local node = root[src.node] + if self.isGlobal(root, node) then + callback(src, src.field[1]) + end + end + end) +end + +--- 判断全局变量 +function mt:isGlobal(root, obj) + if obj.type == 'getglobal' then + if obj[1] == '_G' or obj[1] == '_ENV' then + return true + end + end + return false +end + +--- 遍历局部变量引用 +function mt:eachLocalRef(obj, callback) + if not obj then + return + end + local src + if obj.type == 'local' then + src = obj + elseif obj.type == 'getlocal' or obj.type == 'setlocal' then + src = self.ast.root[obj.loc] + else + return + end + callback(src) + if src.ref then + for i = 1, #src.ref do + local ref = src.ref[i] + callback(self.ast.root[ref]) + end + end +end + +return function (ast) + local self = setmetatable({ + step = 0, + ast = ast, + }, mt) + return self +end |