summaryrefslogtreecommitdiff
path: root/script-beta/core/diagnostics/undefined-global.lua
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2019-11-23 00:05:30 +0800
committer最萌小汐 <sumneko@hotmail.com>2019-11-23 00:05:30 +0800
commit6da2b175e20ed3c03b0dfcfc9046de1e0e5d4444 (patch)
treefdc22d78150fd1c5edc46732c8b151ccfefb519f /script-beta/core/diagnostics/undefined-global.lua
parentd0ff66c9abe9d6abbca12fd811e0c3cb69c1033a (diff)
downloadlua-language-server-6da2b175e20ed3c03b0dfcfc9046de1e0e5d4444.zip
正路目录
Diffstat (limited to 'script-beta/core/diagnostics/undefined-global.lua')
-rw-r--r--script-beta/core/diagnostics/undefined-global.lua63
1 files changed, 63 insertions, 0 deletions
diff --git a/script-beta/core/diagnostics/undefined-global.lua b/script-beta/core/diagnostics/undefined-global.lua
new file mode 100644
index 00000000..ed81ced3
--- /dev/null
+++ b/script-beta/core/diagnostics/undefined-global.lua
@@ -0,0 +1,63 @@
+local files = require 'files'
+local vm = require 'vm'
+local lang = require 'language'
+local library = require 'library'
+local config = require 'config'
+
+return function (uri, callback)
+ local ast = files.getAst(uri)
+ if not ast then
+ return
+ end
+
+ local globalCache = {}
+
+ -- 遍历全局变量,检查所有没有 mode['set'] 的全局变量
+ local globals = vm.getGlobals(ast.ast)
+ for key, infos in pairs(globals) do
+ if infos.mode['set'] == true then
+ goto CONTINUE
+ end
+ if globalCache[key] then
+ goto CONTINUE
+ end
+ local skey = key and key:match '^s|(.+)$'
+ if not skey then
+ goto CONTINUE
+ end
+ if library.global[skey] then
+ goto CONTINUE
+ end
+ if config.config.diagnostics.globals[skey] then
+ goto CONTINUE
+ end
+ if globalCache[key] == nil then
+ local uris = files.findGlobals(key)
+ for i = 1, #uris do
+ local destAst = files.getAst(uris[i])
+ local destGlobals = vm.getGlobals(destAst.ast)
+ if destGlobals[key] and destGlobals[key].mode['set'] then
+ globalCache[key] = true
+ goto CONTINUE
+ end
+ end
+ end
+ globalCache[key] = false
+ local message = lang.script('DIAG_UNDEF_GLOBAL', skey)
+ local otherVersion = library.other[skey]
+ local customVersion = library.custom[skey]
+ if otherVersion then
+ message = ('%s(%s)'):format(message, lang.script('DIAG_DEFINED_VERSION', table.concat(otherVersion, '/'), config.config.runtime.version))
+ elseif customVersion then
+ message = ('%s(%s)'):format(message, lang.script('DIAG_DEFINED_CUSTOM', table.concat(customVersion, '/')))
+ end
+ for _, info in ipairs(infos) do
+ callback {
+ start = info.source.start,
+ finish = info.source.finish,
+ message = message,
+ }
+ end
+ ::CONTINUE::
+ end
+end