diff options
Diffstat (limited to 'server-beta/src')
-rw-r--r-- | server-beta/src/core/diagnostics/undefined-global.lua | 15 | ||||
-rw-r--r-- | server-beta/src/files.lua | 6 | ||||
-rw-r--r-- | server-beta/src/searcher/eachGlobal.lua | 60 | ||||
-rw-r--r-- | server-beta/src/searcher/eachRef.lua | 16 | ||||
-rw-r--r-- | server-beta/src/searcher/getGlobals.lua | 43 | ||||
-rw-r--r-- | server-beta/src/searcher/init.lua | 2 | ||||
-rw-r--r-- | server-beta/src/searcher/isGlobal.lua | 5 | ||||
-rw-r--r-- | server-beta/src/searcher/searcher.lua | 4 |
8 files changed, 65 insertions, 86 deletions
diff --git a/server-beta/src/core/diagnostics/undefined-global.lua b/server-beta/src/core/diagnostics/undefined-global.lua index b3ef3044..1a0af9d9 100644 --- a/server-beta/src/core/diagnostics/undefined-global.lua +++ b/server-beta/src/core/diagnostics/undefined-global.lua @@ -11,20 +11,20 @@ return function (uri, callback) end -- 遍历全局变量,检查所有没有 mode['set'] 的全局变量 - searcher.eachGlobal(ast.ast, function (infos) + local globals = searcher.getGlobals(ast.ast) + for key, infos in pairs(globals) do if infos.mode['set'] == true then - return + goto CONTINUE end - local key = infos.key local skey = key and key:match '^s|(.+)$' if not skey then - return + goto CONTINUE end if library.global[skey] then - return + goto CONTINUE end if config.config.diagnostics.globals[skey] then - return + goto CONTINUE end local message local otherVersion = library.other[skey] @@ -43,5 +43,6 @@ return function (uri, callback) message = message, } end - end) + ::CONTINUE:: + end end diff --git a/server-beta/src/files.lua b/server-beta/src/files.lua index 3c0862c2..8a362841 100644 --- a/server-beta/src/files.lua +++ b/server-beta/src/files.lua @@ -206,10 +206,10 @@ function m.getGlobals(uri) return nil end file.globals = {} - searcher.eachGlobal(ast.ast, function (info) - local name = info.key + local globals = searcher.getGlobals(ast.ast) + for name in pairs(globals) do file.globals[name] = true - end) + end return file.globals end diff --git a/server-beta/src/searcher/eachGlobal.lua b/server-beta/src/searcher/eachGlobal.lua deleted file mode 100644 index 27ee0f21..00000000 --- a/server-beta/src/searcher/eachGlobal.lua +++ /dev/null @@ -1,60 +0,0 @@ -local guide = require 'parser.guide' -local searcher = require 'searcher.searcher' - -local function eachGlobal(source, callback) - local root = guide.getRoot(source) - local env = root.locals[1] - local result = {} - local mark = {} - searcher.eachField(env, function (info) - local src = info.source - if mark[src] then - return - end - mark[src] = true - local name = info.key - if not result[name] then - result[name] = { - key = name, - mode = {}, - } - end - result[name][#result[name]+1] = info - result[name].mode[info.mode] = true - searcher.cache.isGlobal[src] = true - end) - for _, info in pairs(result) do - callback(info) - end -end - -function searcher.eachGlobal(source, callback) - source = guide.getRoot(source) - local cache = searcher.cache.eachGlobal[source] - if cache then - for i = 1, #cache do - local res = callback(cache[i]) - if res ~= nil then - return res - end - end - return - end - local unlock = searcher.lock('eachGlobal', source) - if not unlock then - return - end - cache = {} - searcher.cache.eachGlobal[source] = cache - local mark = {} - eachGlobal(source, function (info) - cache[#cache+1] = info - end) - unlock() - for i = 1, #cache do - local res = callback(cache[i]) - if res ~= nil then - return res - end - end -end diff --git a/server-beta/src/searcher/eachRef.lua b/server-beta/src/searcher/eachRef.lua index 0cfed014..dca1c636 100644 --- a/server-beta/src/searcher/eachRef.lua +++ b/server-beta/src/searcher/eachRef.lua @@ -238,17 +238,15 @@ local function ofGlobal(source, callback) local globals = files.getGlobals(uri) local ast = files.getAst(uri) if ast and globals and globals[key] then - searcher.eachGlobal(ast.ast, function (infos) - if key == infos.key then - for _, info in ipairs(infos) do - callback(info) - if info.value then - ofValue(info.value, callback) - end + globals = searcher.getGlobals(ast.ast) + if globals[key] then + for _, info in ipairs(globals[key]) do + callback(info) + if info.value then + ofValue(info.value, callback) end - return true end - end) + end end end else diff --git a/server-beta/src/searcher/getGlobals.lua b/server-beta/src/searcher/getGlobals.lua new file mode 100644 index 00000000..56c489bd --- /dev/null +++ b/server-beta/src/searcher/getGlobals.lua @@ -0,0 +1,43 @@ +local guide = require 'parser.guide' +local searcher = require 'searcher.searcher' + +local function getGlobals(source) + local root = guide.getRoot(source) + local env = guide.getENV(root) + local cache = {} + local mark = {} + searcher.eachField(env, function (info) + local src = info.source + if mark[src] then + return + end + mark[src] = true + local name = info.key + if not cache[name] then + cache[name] = { + key = name, + mode = {}, + } + end + cache[name][#cache[name]+1] = info + cache[name].mode[info.mode] = true + searcher.cache.isGlobal[src] = true + end) + return cache +end + +function searcher.getGlobals(source) + source = guide.getRoot(source) + local cache = searcher.cache.getGlobals[source] + if cache then + return cache + end + local unlock = searcher.lock('getGlobals', source) + if not unlock then + return nil + end + cache = getGlobals(source) + searcher.cache.getGlobals[source] = cache + unlock() + return cache +end diff --git a/server-beta/src/searcher/init.lua b/server-beta/src/searcher/init.lua index 95d21427..cd59fa4a 100644 --- a/server-beta/src/searcher/init.lua +++ b/server-beta/src/searcher/init.lua @@ -1,7 +1,7 @@ local searcher = require 'searcher.searcher' require 'searcher.eachField' require 'searcher.eachRef' -require 'searcher.eachGlobal' +require 'searcher.getGlobals' require 'searcher.isGlobal' require 'searcher.getLibrary' return searcher diff --git a/server-beta/src/searcher/isGlobal.lua b/server-beta/src/searcher/isGlobal.lua index e4093b58..8c59dadb 100644 --- a/server-beta/src/searcher/isGlobal.lua +++ b/server-beta/src/searcher/isGlobal.lua @@ -2,9 +2,6 @@ local searcher = require 'searcher.searcher' local guide = require 'parser.guide' function searcher.isGlobal(source) - source = guide.getRoot(source) - if not searcher.cache.eachGlobal[source] then - searcher.eachGlobal(source, function () end) - end + local globals = searcher.getGlobals(source) return searcher.cache.isGlobal[source] == true end diff --git a/server-beta/src/searcher/searcher.lua b/server-beta/src/searcher/searcher.lua index a5b2d677..144a0a1e 100644 --- a/server-beta/src/searcher/searcher.lua +++ b/server-beta/src/searcher/searcher.lua @@ -107,7 +107,7 @@ function m.refreshCache() m.cache = { eachRef = {}, eachField = {}, - eachGlobal = {}, + getGlobals = {}, isGlobal = {}, specialName = {}, getLibrary = {}, @@ -116,7 +116,7 @@ function m.refreshCache() m.locked = { eachRef = {}, eachField = {}, - eachGlobal = {}, + getGlobals = {}, getLibrary = {}, } end |