diff options
Diffstat (limited to 'server/src')
-rw-r--r-- | server/src/constant/DiagnosticDefaultSeverity.lua | 1 | ||||
-rw-r--r-- | server/src/core/diagnostics.lua | 56 |
2 files changed, 50 insertions, 7 deletions
diff --git a/server/src/constant/DiagnosticDefaultSeverity.lua b/server/src/constant/DiagnosticDefaultSeverity.lua index 33df37d8..3cd67528 100644 --- a/server/src/constant/DiagnosticDefaultSeverity.lua +++ b/server/src/constant/DiagnosticDefaultSeverity.lua @@ -1,5 +1,6 @@ return { ['unused-local'] = 'Hint', + ['unused-function'] = 'Hint', ['undefined-global'] = 'Warning', ['unused-label'] = 'Hint', ['unused-vararg'] = 'Hint', diff --git a/server/src/core/diagnostics.lua b/server/src/core/diagnostics.lua index 26327ea2..509fe48c 100644 --- a/server/src/core/diagnostics.lua +++ b/server/src/core/diagnostics.lua @@ -9,6 +9,13 @@ local DiagnosticTag = require 'constant.DiagnosticTag' local mt = {} mt.__index = mt +local function isContainPos(obj, start, finish) + if obj.start <= start and obj.finish >= finish then + return true + end + return false +end + function mt:searchUnusedLocals(callback) self.vm:eachSource(function (source) local loc = source:bindLocal() @@ -39,6 +46,41 @@ function mt:searchUnusedLocals(callback) end) end +function mt:searchUnusedFunctions(callback) + self.vm:eachSource(function (source) + local loc = source:bindLocal() + if not loc then + return + end + if loc:get 'emmy arg' then + return + end + if source:action() ~= 'local' then + return + end + if loc:get 'hide' then + return + end + local used = loc:eachInfo(function (info) + if info.type == 'get' then + return true + end + end) + if used then + return + end + loc:eachInfo(function (info, src) + if info.type == 'set' or info.type == 'local' then + local v = src:bindValue() + local func = v and v:getFunction() + if func then + callback(func:getSource().start, func:getSource().finish) + end + end + end) + end) +end + function mt:searchUndefinedGlobal(callback) local definedGlobal = {} for name in pairs(config.config.diagnostics.globals) do @@ -112,13 +154,6 @@ function mt:searchUnusedVararg(callback) end) end -local function isContainPos(obj, start, finish) - if obj.start <= start and obj.finish >= finish then - return true - end - return false -end - local function isInString(vm, start, finish) return vm:eachSource(function (source) if source.type == 'string' and isContainPos(source, start, finish) then @@ -798,6 +833,13 @@ return function (vm, lines, uri) tags = {DiagnosticTag.Unnecessary}, } end) + -- 未使用的函数 + session:doDiagnostics(session.searchUnusedFunctions, 'unused-function', function () + return { + message = lang.script.DIAG_UNUSED_FUNCTION, + tags = {DiagnosticTag.Unnecessary}, + } + end) -- 读取未定义全局变量 session:doDiagnostics(session.searchUndefinedGlobal, 'undefined-global', function (key) local message = lang.script('DIAG_UNDEF_GLOBAL', key) |