summaryrefslogtreecommitdiff
path: root/server-beta/src/core
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2019-11-04 19:56:00 +0800
committer最萌小汐 <sumneko@hotmail.com>2019-11-04 19:56:00 +0800
commit6376ac1a2d98280304221243b00a1bc8cac74dc3 (patch)
tree322c909f4946978a39dd30e99b7a52269eb20df1 /server-beta/src/core
parent521feda953a686ad876186366391340f945ca65b (diff)
downloadlua-language-server-6376ac1a2d98280304221243b00a1bc8cac74dc3.zip
诊断未定义全局变量
Diffstat (limited to 'server-beta/src/core')
-rw-r--r--server-beta/src/core/diagnostics/undefined-global.lua41
1 files changed, 35 insertions, 6 deletions
diff --git a/server-beta/src/core/diagnostics/undefined-global.lua b/server-beta/src/core/diagnostics/undefined-global.lua
index db7af133..888064dc 100644
--- a/server-beta/src/core/diagnostics/undefined-global.lua
+++ b/server-beta/src/core/diagnostics/undefined-global.lua
@@ -3,6 +3,8 @@ local guide = require 'parser.guide'
local searcher = require 'searcher'
local define = require 'proto.define'
local lang = require 'language'
+local library = require 'library'
+local config = require 'config'
return function (uri, callback)
local ast = files.getAst(uri)
@@ -10,26 +12,53 @@ return function (uri, callback)
return
end
+ -- 先遍历一次所有该文件中的全局变量
+ -- 如果变量有 set 行为,则做标记
+ -- 然后再遍历一次,对所有的行为打上标记
local hasSet = {}
searcher.eachGlobal(ast.ast, function (info)
- local key = info.key
- if hasSet[key] ~= nil then
+ if hasSet[info.source] ~= nil then
return
end
- hasSet[key] = false
+ local mark = false
searcher.eachRef(info.source, function (info)
if info.mode == 'set' then
- hasSet[key] = true
+ mark = true
end
end)
+ searcher.eachRef(info.source, function (info)
+ hasSet[info.source] = mark
+ end)
end)
+ -- 然后再遍历一次,检查所有标记为假的全局变量
searcher.eachGlobal(ast.ast, function (info)
local source = info.source
- if info.mode == 'get' and not hasSet[info.key] then
+ local key = info.key
+ local skey = key:match '^s|(.+)$'
+ if not skey then
+ return
+ end
+ if library.global[skey] then
+ return
+ end
+ if config.config.diagnostics.globals[skey] then
+ return
+ end
+ if info.mode == 'get' and not hasSet[source] then
+ local message
+ local otherVersion = library.other[key]
+ local customVersion = library.custom[key]
+ 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, '/')))
+ else
+ message = lang.script('DIAG_UNDEF_GLOBAL', info.key)
+ end
callback {
start = source.start,
finish = source.finish,
- message = lang.script('DIAG_UNDEF_GLOBAL', info.key),
+ message = message,
}
end
end)