diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2021-01-04 19:29:36 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2021-01-04 19:29:36 +0800 |
commit | 216f3d54901c6759b99850882a549210f0270745 (patch) | |
tree | 68db79cfcc46134277c9719efc0dd882630e713b /script | |
parent | e39b4a734ec3b4e769a584601f522ba4e5a84a99 (diff) | |
download | lua-language-server-216f3d54901c6759b99850882a549210f0270745.zip |
new dianostic: `count-down-loop`
Diffstat (limited to 'script')
-rw-r--r-- | script/core/diagnostics/count-down-loop.lua | 37 | ||||
-rw-r--r-- | script/proto/define.lua | 14 |
2 files changed, 51 insertions, 0 deletions
diff --git a/script/core/diagnostics/count-down-loop.lua b/script/core/diagnostics/count-down-loop.lua new file mode 100644 index 00000000..b64391fd --- /dev/null +++ b/script/core/diagnostics/count-down-loop.lua @@ -0,0 +1,37 @@ +local files = require "files" +local guide = require "parser.guide" +local lang = require 'language' + +return function (uri, callback) + local state = files.getAst(uri) + local text = files.getText(uri) + if not state or not text then + return + end + + guide.eachSourceType(state.ast, 'loop', function (source) + if not source.loc or not source.loc.value then + return + end + local maxNumer = source.max and source.max.type == 'number' and tonumber(source.max[1]) + if maxNumer ~= 1 then + return + end + if not source.step then + callback { + start = source.loc.value.start, + finish = source.max.finish, + message = lang.script('DIAG_COUNT_DOWN_LOOP', ('%s, %s'):format(text:sub(source.loc.value.start, source.max.finish), '-1')) + } + else + local stepNumber = source.step.type == 'number' and tonumber(source.step[1]) + if stepNumber and stepNumber > 0 then + callback { + start = source.loc.value.start, + finish = source.step.finish, + message = lang.script('DIAG_COUNT_DOWN_LOOP', ('%s, -%s'):format(text:sub(source.loc.value.start, source.max.finish), source.step[1])) + } + end + end + end) +end diff --git a/script/proto/define.lua b/script/proto/define.lua index 50f4cd87..f6f8e9cc 100644 --- a/script/proto/define.lua +++ b/script/proto/define.lua @@ -135,7 +135,14 @@ m.DiagnosticSeverity = { Hint = 4, } +---@alias DiagnosticDefaultSeverity +---| '"Hint"' +---| '"Information"' +---| '"Warning"' +---| '"Error"' + --- 诊断类型与默认等级 +---@type table<string, DiagnosticDefaultSeverity> m.DiagnosticDefaultSeverity = { ['unused-local'] = 'Hint', ['unused-function'] = 'Hint', @@ -158,6 +165,7 @@ m.DiagnosticDefaultSeverity = { ['code-after-break'] = 'Hint', ['unbalanced-assignments'] = 'Warning', ['close-non-object'] = 'Warning', + ['count-down-loop'] = 'Warning', ['duplicate-doc-class'] = 'Warning', ['undefined-doc-class'] = 'Warning', @@ -169,6 +177,10 @@ m.DiagnosticDefaultSeverity = { ['duplicate-doc-field'] = 'Warning', } +---@alias DiagnosticDefaultNeededFileStatus +---| '"Any"' +---| '"Opened"' + -- 文件状态 m.FileStatus = { Any = 1, @@ -176,6 +188,7 @@ m.FileStatus = { } --- 诊断类型与需要的文件状态(可以控制只分析打开的文件、还是所有文件) +---@type table<string, DiagnosticDefaultNeededFileStatus> m.DiagnosticDefaultNeededFileStatus = { ['unused-local'] = 'Opened', ['unused-function'] = 'Opened', @@ -198,6 +211,7 @@ m.DiagnosticDefaultNeededFileStatus = { ['code-after-break'] = 'Opened', ['unbalanced-assignments'] = 'Any', ['close-non-object'] = 'Any', + ['count-down-loop'] = 'Any', ['duplicate-doc-class'] = 'Any', ['undefined-doc-class'] = 'Any', |