summaryrefslogtreecommitdiff
path: root/script/core/diagnostics/init.lua
diff options
context:
space:
mode:
Diffstat (limited to 'script/core/diagnostics/init.lua')
-rw-r--r--script/core/diagnostics/init.lua56
1 files changed, 56 insertions, 0 deletions
diff --git a/script/core/diagnostics/init.lua b/script/core/diagnostics/init.lua
new file mode 100644
index 00000000..a6b61e12
--- /dev/null
+++ b/script/core/diagnostics/init.lua
@@ -0,0 +1,56 @@
+local files = require 'files'
+local define = require 'proto.define'
+local config = require 'config'
+local await = require 'await'
+
+-- 把耗时最长的诊断放到最后面
+local diagLevel = {
+ ['redundant-parameter'] = 100,
+}
+
+local diagList = {}
+for k in pairs(define.DiagnosticDefaultSeverity) do
+ diagList[#diagList+1] = k
+end
+table.sort(diagList, function (a, b)
+ return (diagLevel[a] or 0) < (diagLevel[b] or 0)
+end)
+
+local function check(uri, name, results)
+ if config.config.diagnostics.disable[name] then
+ return
+ end
+ local level = config.config.diagnostics.severity[name]
+ or define.DiagnosticDefaultSeverity[name]
+ if level == 'Hint' and not files.isOpen(uri) then
+ return
+ end
+ local severity = define.DiagnosticSeverity[level]
+ local clock = os.clock()
+ require('core.diagnostics.' .. name)(uri, function (result)
+ result.level = severity or result.level
+ result.code = name
+ results[#results+1] = result
+ end, name)
+ local passed = os.clock() - clock
+ if passed >= 0.5 then
+ log.warn(('Diagnostics [%s] @ [%s] takes [%.3f] sec!'):format(name, uri, passed))
+ end
+end
+
+return function (uri, response)
+ local vm = require 'vm'
+ local ast = files.getAst(uri)
+ if not ast then
+ return nil
+ end
+
+ local isOpen = files.isOpen(uri)
+
+ for _, name in ipairs(diagList) do
+ await.delay()
+ local results = {}
+ check(uri, name, results)
+ response(results)
+ end
+end