diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2019-11-10 20:14:29 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2019-11-10 20:14:29 +0800 |
commit | d12f4c82488685770dda1ab8d4f361e2662dd31f (patch) | |
tree | f42b1779d2c012a6b1cdcfc139c3cf48e03ab5ed /server-beta/src/core/diagnostics | |
parent | 5668f4ded84192efd4ebd9cd82e9619398f9bebe (diff) | |
download | lua-language-server-d12f4c82488685770dda1ab8d4f361e2662dd31f.zip |
优化多余参数诊断的性能
Diffstat (limited to 'server-beta/src/core/diagnostics')
-rw-r--r-- | server-beta/src/core/diagnostics/redundant-parameter.lua | 67 |
1 files changed, 34 insertions, 33 deletions
diff --git a/server-beta/src/core/diagnostics/redundant-parameter.lua b/server-beta/src/core/diagnostics/redundant-parameter.lua index 79c259d8..04012ef4 100644 --- a/server-beta/src/core/diagnostics/redundant-parameter.lua +++ b/server-beta/src/core/diagnostics/redundant-parameter.lua @@ -5,46 +5,46 @@ local lang = require 'language' local define = require 'proto.define' local library = require 'library' -local function packLibraryArgs(source) +local function countLibraryArgs(source) local func = searcher.getLibrary(source) if not func then return nil end - local result = {} + local result = 0 if not func.args then return result end - for _, lib in ipairs(func.args) do - result[#result+1] = lib + if func.args[#func.args].type == '...' then + return math.maxinteger end + result = result + #func.args return result end -local function packCallArgs(source) - local result = {} +local function countCallArgs(source) + local result = 0 if not source.args then - return result + return 0 end if source.node and source.node.type == 'getmethod' then - result[#result+1] = source.node.node - end - for _, arg in ipairs(source.args) do - result[#result+1] = arg + result = result + 1 end + result = result + #source.args return result end -local function packFuncArgs(source) - local result = {} +local function countFuncArgs(source) + local result = 0 if not source.args then return result end - if source.parent and source.parent.type == 'setmethod' then - result[#result+1] = source.parent.node + if source.args[#source.args].type == '...' then + return math.maxinteger end - for _, arg in ipairs(source.args) do - result[#result+1] = arg + if source.parent and source.parent.type == 'setmethod' then + result = result + 1 end + result = result + #source.args return result end @@ -55,8 +55,8 @@ return function (uri, callback) end guide.eachSourceType(ast.ast, 'call', function (source) - local callArgs = packCallArgs(source) - if not callArgs then + local callArgs = countCallArgs(source) + if callArgs == 0 then return end @@ -66,32 +66,33 @@ return function (uri, callback) if info.mode == 'value' then local src = info.source if src.type == 'function' then - local args = packFuncArgs(src) - if args and (not funcArgs or #funcArgs < #args) then + local args = countFuncArgs(src) + if not funcArgs or args > funcArgs then funcArgs = args end end end end) - funcArgs = funcArgs or packLibraryArgs(func) + funcArgs = funcArgs or countLibraryArgs(func) if not funcArgs then return end - local lastArg = funcArgs[#funcArgs] - if lastArg and lastArg.type == '...' then + local delta = callArgs - funcArgs + if delta <= 0 then return end - - for i = #funcArgs + 1, #callArgs do - local arg = callArgs[i] - callback { - start = arg.start, - finish = arg.finish, - tags = { define.DiagnosticTag.Unnecessary }, - message = lang.script('DIAG_OVER_MAX_ARGS', #funcArgs, #callArgs) - } + for i = #source.args - delta + 1, #source.args do + local arg = source.args[i] + if arg then + callback { + start = arg.start, + finish = arg.finish, + tags = { define.DiagnosticTag.Unnecessary }, + message = lang.script('DIAG_OVER_MAX_ARGS', funcArgs, callArgs) + } + end end end) end |