From 8faf1954a49f79a7f7c99a930643266fb4a5ad24 Mon Sep 17 00:00:00 2001
From: LJ Sonic <lamr@free.fr>
Date: Mon, 25 Mar 2024 16:04:44 +0100
Subject: [PATCH] Add diagnostics.ignoredConstantPrefixes setting

---
 locale/en-us/setting.lua                     |  2 ++
 locale/pt-br/setting.lua                     |  2 ++
 locale/zh-cn/setting.lua                     |  2 ++
 locale/zh-tw/setting.lua                     |  2 ++
 script/config/template.lua                   |  9 +++++++++
 script/core/diagnostics/undefined-global.lua | 13 +++++++++++++
 6 files changed, 30 insertions(+)

diff --git a/locale/en-us/setting.lua b/locale/en-us/setting.lua
index 18840e94f..6fd7abdc5 100644
--- a/locale/en-us/setting.lua
+++ b/locale/en-us/setting.lua
@@ -48,6 +48,8 @@ config.diagnostics.disable        =
 "Disabled diagnostic (Use code in hover brackets)."
 config.diagnostics.globals        =
 "Defined global variables."
+config.diagnostics.ignoredConstantPrefixes =
+"Global variables that start with one of these prefixes will be ignored when checking for undefined global variables."
 config.diagnostics.severity       =
 [[
 Modify the diagnostic severity.
diff --git a/locale/pt-br/setting.lua b/locale/pt-br/setting.lua
index 759048725..d67464b37 100644
--- a/locale/pt-br/setting.lua
+++ b/locale/pt-br/setting.lua
@@ -48,6 +48,8 @@ config.diagnostics.disable        = -- TODO: need translate!
 "Disabled diagnostic (Use code in hover brackets)."
 config.diagnostics.globals        = -- TODO: need translate!
 "Defined global variables."
+config.diagnostics.ignoredConstantPrefixes = -- TODO: need translate!
+"Global variables that start with one of these prefixes will be ignored when checking for undefined global variables."
 config.diagnostics.severity       = -- TODO: need translate!
 [[
 Modify the diagnostic severity.
diff --git a/locale/zh-cn/setting.lua b/locale/zh-cn/setting.lua
index d488ac3f1..89de87661 100644
--- a/locale/zh-cn/setting.lua
+++ b/locale/zh-cn/setting.lua
@@ -48,6 +48,8 @@ config.diagnostics.disable        =
 "禁用的诊断(使用浮框括号内的代码)。"
 config.diagnostics.globals        =
 "已定义的全局变量。"
+config.diagnostics.ignoredConstantPrefixes = -- TODO: need translate!
+"Global variables that start with one of these prefixes will be ignored when checking for undefined global variables."
 config.diagnostics.severity       =
 [[
 修改诊断等级。
diff --git a/locale/zh-tw/setting.lua b/locale/zh-tw/setting.lua
index 8c74668da..f964afa62 100644
--- a/locale/zh-tw/setting.lua
+++ b/locale/zh-tw/setting.lua
@@ -48,6 +48,8 @@ config.diagnostics.disable        =
 "停用的診斷(使用浮框括號內的程式碼)。"
 config.diagnostics.globals        =
 "已定義的全域變數。"
+config.diagnostics.ignoredConstantPrefixes = -- TODO: need translate!
+"Global variables that start with one of these prefixes will be ignored when checking for undefined global variables."
 config.diagnostics.severity       =
 [[
 修改診斷等級。
diff --git a/script/config/template.lua b/script/config/template.lua
index 3d2a8d35d..059dcab96 100644
--- a/script/config/template.lua
+++ b/script/config/template.lua
@@ -242,6 +242,15 @@ local template = {
                                             >> util.deepCopy(define.BuiltIn),
     ['Lua.diagnostics.enable']              = Type.Boolean >> true,
     ['Lua.diagnostics.globals']             = Type.Array(Type.String),
+    ['Lua.diagnostics.ignoredConstantPrefixes'] = Type.Array(Type.String) >> {
+                                                'MT_',
+                                                'S_',
+                                                'sfx_',
+                                                'SKINCOLOR_',
+                                                'SPR_',
+                                                'SPR2_',
+                                                'TOL_',
+                                            },
     ['Lua.diagnostics.disable']             = Type.Array(Type.String << util.getTableKeys(diag.getDiagAndErrNameMap(), true)),
     ['Lua.diagnostics.severity']            = Type.Hash(
                                                 Type.String << util.getTableKeys(define.DiagnosticDefaultNeededFileStatus, true),
diff --git a/script/core/diagnostics/undefined-global.lua b/script/core/diagnostics/undefined-global.lua
index 179c92043..4fb8117ba 100644
--- a/script/core/diagnostics/undefined-global.lua
+++ b/script/core/diagnostics/undefined-global.lua
@@ -13,6 +13,15 @@ local requireLike = {
     ['load']    = true,
 }
 
+local function isNameIgnored(name, ignoredPrefixes)
+    for i = 1, #ignoredPrefixes do
+        if name:find(ignoredPrefixes[i], nil, false) == 1 then
+            return true
+        end
+    end
+    return false
+end
+
 ---@async
 return function (uri, callback)
     local state = files.getState(uri)
@@ -22,6 +31,7 @@ return function (uri, callback)
 
     local dglobals = util.arrayToHash(config.get(uri, 'Lua.diagnostics.globals'))
     local rspecial = config.get(uri, 'Lua.runtime.special')
+    local ignoredPrefixes = config.get(uri, 'Lua.diagnostics.ignoredConstantPrefixes')
     local cache    = {}
 
     -- 遍历全局变量,检查所有没有 set 模式的全局变量
@@ -36,6 +46,9 @@ return function (uri, callback)
         if rspecial[key] then
             return
         end
+        if isNameIgnored(key, ignoredPrefixes) then
+            return
+        end
         local node = src.node
         if node.tag ~= '_ENV' then
             return
-- 
GitLab