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 | |
parent | e39b4a734ec3b4e769a584601f522ba4e5a84a99 (diff) | |
download | lua-language-server-216f3d54901c6759b99850882a549210f0270745.zip |
new dianostic: `count-down-loop`
-rw-r--r-- | changelog.md | 1 | ||||
-rw-r--r-- | locale/en-us/script.lua | 1 | ||||
-rw-r--r-- | locale/zh-cn/script.lua | 1 | ||||
-rw-r--r-- | script/core/diagnostics/count-down-loop.lua | 37 | ||||
-rw-r--r-- | script/proto/define.lua | 14 | ||||
-rw-r--r-- | test/diagnostics/init.lua | 12 |
6 files changed, 66 insertions, 0 deletions
diff --git a/changelog.md b/changelog.md index aac275e2..42435361 100644 --- a/changelog.md +++ b/changelog.md @@ -3,6 +3,7 @@ ## 1.10.0 * `NEW` workspace: supports `.dll`(`.so`) in `require` * `NEW` folding: `---@class`, `--#region` and docs of function +* `NEW` dianostic: `count-down-loop` * `CHG` supports `~` in command line * `CHG` completion: improve workspace words * `CHG` completion: show words in string diff --git a/locale/en-us/script.lua b/locale/en-us/script.lua index 1e6f3c03..6d82c3ab 100644 --- a/locale/en-us/script.lua +++ b/locale/en-us/script.lua @@ -38,6 +38,7 @@ DIAG_CODE_AFTER_BREAK = 'Unable to execute code after `break`.' DIAG_UNBALANCED_ASSIGNMENTS = 'The value is assigned as `nil` because the number of values is not enough. In Lua, `x, y = 1 ` is equivalent to `x, y = 1, nil` .' DIAG_REQUIRE_LIKE = 'You can treat `{}` as `require` by setting.' DIAG_COSE_NON_OBJECT = 'Cannot close a value of this type. (Unless set `__close` meta method)' +DIAG_COUNT_DOWN_LOOP = 'Do you mean `{}` ?' DIAG_CIRCLE_DOC_CLASS = 'Circularly inherited classes.' DIAG_DOC_FIELD_NO_CLASS = 'The field must be defined after the class.' diff --git a/locale/zh-cn/script.lua b/locale/zh-cn/script.lua index b3b59c85..c3627dc6 100644 --- a/locale/zh-cn/script.lua +++ b/locale/zh-cn/script.lua @@ -38,6 +38,7 @@ DIAG_CODE_AFTER_BREAK = '无法执行到 `break` 后的代码。' DIAG_UNBALANCED_ASSIGNMENTS = '由于值的数量不够而被赋值为了 `nil` 。在Lua中, `x, y = 1` 等价于 `x, y = 1, nil` 。' DIAG_REQUIRE_LIKE = '你可以在设置中将 `{}` 视为 `require`。' DIAG_COSE_NON_OBJECT = '无法 close 此类型的值。(除非给此类型设置 `__close` 元方法)' +DIAG_COUNT_DOWN_LOOP = '你的意思是 `{}` 吗?' DIAG_CIRCLE_DOC_CLASS = '循环继承的类。' DIAG_DOC_FIELD_NO_CLASS = '字段必须定义在类之后。' 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', diff --git a/test/diagnostics/init.lua b/test/diagnostics/init.lua index d95bf380..4ec93acf 100644 --- a/test/diagnostics/init.lua +++ b/test/diagnostics/init.lua @@ -966,3 +966,15 @@ local function f() end f(1) ]] + +TEST [[ +for i = <!10, 1!> do + print(i) +end +]] + +TEST [[ +for i = <!10, 1, 5!> do + print(i) +end +]] |