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 | |
parent | f501deff48a30e937afdcb528aba942648c4888b (diff) | |
download | lua-language-server-12e07305cf74f7e98adbfb482b01aceb0c72fd61.zip |
整理代码
-rw-r--r-- | server-beta/src/core/definition.lua | 42 | ||||
-rw-r--r-- | server-beta/src/core/engineer.lua | 60 | ||||
-rw-r--r-- | server-beta/src/parser/guide.lua | 24 | ||||
-rw-r--r-- | server-beta/src/workspace/workspace.lua | 1 | ||||
-rw-r--r-- | server-beta/test/definition/init.lua | 2 |
5 files changed, 80 insertions, 49 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 diff --git a/server-beta/src/parser/guide.lua b/server-beta/src/parser/guide.lua index e6be631d..bd7f9150 100644 --- a/server-beta/src/parser/guide.lua +++ b/server-beta/src/parser/guide.lua @@ -215,30 +215,6 @@ function m.eachSourceOf(root, types, callback) end end ---- 遍历全局变量 -function m.eachGloabl(root, callback) - m.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 m.isGlobal(root, node) then - callback(src, src.field[1]) - end - end - end) -end - ---- 判断全局变量 -function m.isGlobal(root, obj) - if obj.type == 'getglobal' then - if obj[1] == '_G' or obj[1] == '_ENV' then - return true - end - end - return false -end - --- 获取偏移对应的坐标(row从0开始,col为光标位置) ---@param lines table ---@return integer {name = 'row'} diff --git a/server-beta/src/workspace/workspace.lua b/server-beta/src/workspace/workspace.lua index b22bcfb8..efe2f1cf 100644 --- a/server-beta/src/workspace/workspace.lua +++ b/server-beta/src/workspace/workspace.lua @@ -5,6 +5,7 @@ local files = require 'files' local config = require 'config' local glob = require 'glob' local platform = require 'bee.platform' +local task = require 'task' local m = {} m.type = 'workspace' diff --git a/server-beta/test/definition/init.lua b/server-beta/test/definition/init.lua index b9e9ec2a..2654e7b2 100644 --- a/server-beta/test/definition/init.lua +++ b/server-beta/test/definition/init.lua @@ -54,8 +54,8 @@ function TEST(script) end end -require 'definition.set' require 'definition.local' +require 'definition.set' require 'definition.arg' require 'definition.function' require 'definition.table' |