diff options
-rw-r--r-- | script/cli/check.lua | 4 | ||||
-rw-r--r-- | script/config/template.lua | 22 | ||||
-rw-r--r-- | script/core/completion/completion.lua | 3 | ||||
-rw-r--r-- | script/core/diagnostics/deprecated.lua | 2 | ||||
-rw-r--r-- | script/core/diagnostics/init.lua | 4 | ||||
-rw-r--r-- | script/core/diagnostics/lowercase-global.lua | 6 | ||||
-rw-r--r-- | script/core/diagnostics/undefined-global.lua | 3 | ||||
-rw-r--r-- | script/files.lua | 2 | ||||
-rw-r--r-- | script/provider/diagnostic.lua | 2 | ||||
-rw-r--r-- | script/provider/formatting.lua | 2 | ||||
-rw-r--r-- | script/provider/spell.lua | 1 | ||||
-rw-r--r-- | script/utility.lua | 38 | ||||
-rw-r--r-- | test/diagnostics/common.lua | 15 |
13 files changed, 78 insertions, 26 deletions
diff --git a/script/cli/check.lua b/script/cli/check.lua index dd2e7737..4df94c59 100644 --- a/script/cli/check.lua +++ b/script/cli/check.lua @@ -50,14 +50,14 @@ lclient():start(function (client) ws.awaitReady(rootUri) - local disables = config.get(rootUri, 'Lua.diagnostics.disable') + local disables = util.arrayToHash(config.get(rootUri, 'Lua.diagnostics.disable')) for name, serverity in pairs(define.DiagnosticDefaultSeverity) do serverity = config.get(rootUri, 'Lua.diagnostics.severity')[name] or 'Warning' if define.DiagnosticSeverity[serverity] > checkLevel then disables[name] = true end end - config.set(nil, 'Lua.diagnostics.disable', disables) + config.set(nil, 'Lua.diagnostics.disable', util.getTableKeys(disables, true)) local uris = files.getAllUris(rootUri) local max = #uris diff --git a/script/config/template.lua b/script/config/template.lua index 968fea1b..45310c7e 100644 --- a/script/config/template.lua +++ b/script/config/template.lua @@ -42,10 +42,11 @@ local units = {} local function register(name, default, checker, loader, caller) units[name] = { - default = default, + name = name, + default = default, _checker = checker, - loader = loader, - caller = caller, + loader = loader, + caller = caller, } end @@ -194,7 +195,7 @@ local template = { ), ['Lua.runtime.meta'] = Type.String >> '${version} ${language} ${encoding}', ['Lua.runtime.unicodeName'] = Type.Boolean, - ['Lua.runtime.nonstandardSymbol'] = Type.Hash(Type.String, Type.Boolean, ';'), + ['Lua.runtime.nonstandardSymbol'] = Type.Array(Type.String), ['Lua.runtime.plugin'] = Type.String, ['Lua.runtime.fileEncoding'] = Type.String >> 'utf8' << { 'utf8', @@ -202,10 +203,17 @@ local template = { 'utf16le', 'utf16be', }, - ['Lua.runtime.builtin'] = Type.Hash(Type.String, Type.String), + ['Lua.runtime.builtin'] = Type.Hash( + Type.String << util.getTableKeys(define.BuiltIn, true), + Type.String >> 'default' << { + 'default', + 'enable', + 'disable', + } + ), ['Lua.diagnostics.enable'] = Type.Boolean >> true, - ['Lua.diagnostics.globals'] = Type.Hash(Type.String, Type.Boolean, ';'), - ['Lua.diagnostics.disable'] = Type.Hash(Type.String, Type.Boolean, ';'), + ['Lua.diagnostics.globals'] = Type.Array(Type.String), + ['Lua.diagnostics.disable'] = Type.Array(Type.String), ['Lua.diagnostics.severity'] = Type.Hash(Type.String, Type.String) >> util.deepCopy(define.DiagnosticDefaultSeverity), ['Lua.diagnostics.neededFileStatus'] = Type.Hash(Type.String, Type.String) diff --git a/script/core/completion/completion.lua b/script/core/completion/completion.lua index d4c20c60..d2d0a040 100644 --- a/script/core/completion/completion.lua +++ b/script/core/completion/completion.lua @@ -356,6 +356,7 @@ local function checkModule(state, word, position, results) if not config.get(state.uri, 'Lua.completion.autoRequire') then return end + local globals = util.arrayToHash(config.get(state.uri, 'Lua.diagnostics.globals')) local locals = guide.getVisibleLocals(state.ast, position) for uri in files.eachFile(state.uri) do if uri == guide.getUri(state.ast) then @@ -366,7 +367,7 @@ local function checkModule(state, word, position, results) local stemName = fileName:gsub('%..+', '') if not locals[stemName] and not vm.hasGlobalSets(state.uri, 'variable', stemName) - and not config.get(state.uri, 'Lua.diagnostics.globals')[stemName] + and not globals[stemName] and stemName:match '^[%a_][%w_]*$' and matchKey(word, stemName) then local targetState = files.getState(uri) diff --git a/script/core/diagnostics/deprecated.lua b/script/core/diagnostics/deprecated.lua index 27920c43..85ae2d19 100644 --- a/script/core/diagnostics/deprecated.lua +++ b/script/core/diagnostics/deprecated.lua @@ -15,7 +15,7 @@ return function (uri, callback) return end - local dglobals = config.get(uri, 'Lua.diagnostics.globals') + local dglobals = util.arrayToHash(config.get(uri, 'Lua.diagnostics.globals')) local rspecial = config.get(uri, 'Lua.runtime.special') guide.eachSourceTypes(ast.ast, types, function (src) ---@async diff --git a/script/core/diagnostics/init.lua b/script/core/diagnostics/init.lua index b4ae3715..a2eb2482 100644 --- a/script/core/diagnostics/init.lua +++ b/script/core/diagnostics/init.lua @@ -3,6 +3,7 @@ local define = require 'proto.define' local config = require 'config' local await = require 'await' local vm = require "vm.vm" +local util = require 'utility' -- 把耗时最长的诊断放到最后面 local diagSort = { @@ -52,7 +53,8 @@ end ---@param isScopeDiag boolean ---@param response async fun(result: any) local function check(uri, name, isScopeDiag, response) - if config.get(uri, 'Lua.diagnostics.disable')[name] then + local disables = config.get(uri, 'Lua.diagnostics.disable') + if util.arrayHas(disables, name) then return end local level = config.get(uri, 'Lua.diagnostics.severity')[name] diff --git a/script/core/diagnostics/lowercase-global.lua b/script/core/diagnostics/lowercase-global.lua index d03e8c70..68bec234 100644 --- a/script/core/diagnostics/lowercase-global.lua +++ b/script/core/diagnostics/lowercase-global.lua @@ -3,6 +3,7 @@ local guide = require 'parser.guide' local lang = require 'language' local config = require 'config' local vm = require 'vm' +local util = require 'utility' local function isDocClass(source) if not source.bindDocs then @@ -23,10 +24,7 @@ return function (uri, callback) return end - local definedGlobal = {} - for name in pairs(config.get(uri, 'Lua.diagnostics.globals')) do - definedGlobal[name] = true - end + local definedGlobal = util.arrayToHash(config.get(uri, 'Lua.diagnostics.globals')) guide.eachSourceType(ast.ast, 'setglobal', function (source) local name = guide.getKeyName(source) diff --git a/script/core/diagnostics/undefined-global.lua b/script/core/diagnostics/undefined-global.lua index bd0aae69..7804aeee 100644 --- a/script/core/diagnostics/undefined-global.lua +++ b/script/core/diagnostics/undefined-global.lua @@ -4,6 +4,7 @@ local lang = require 'language' local config = require 'config' local guide = require 'parser.guide' local await = require 'await' +local util = require 'utility' local requireLike = { ['include'] = true, @@ -19,7 +20,7 @@ return function (uri, callback) return end - local dglobals = config.get(uri, 'Lua.diagnostics.globals') + local dglobals = util.arrayToHash(config.get(uri, 'Lua.diagnostics.globals')) local rspecial = config.get(uri, 'Lua.runtime.special') local cache = {} diff --git a/script/files.lua b/script/files.lua index 22c9ae31..afbd64cb 100644 --- a/script/files.lua +++ b/script/files.lua @@ -488,7 +488,7 @@ function m.compileState(uri, text) , { special = config.get(uri, 'Lua.runtime.special'), unicodeName = config.get(uri, 'Lua.runtime.unicodeName'), - nonstandardSymbol = config.get(uri, 'Lua.runtime.nonstandardSymbol'), + nonstandardSymbol = util.arrayToHash(config.get(uri, 'Lua.runtime.nonstandardSymbol')), } ) local passed = os.clock() - clock diff --git a/script/provider/diagnostic.lua b/script/provider/diagnostic.lua index 076a613e..d6d6c4a3 100644 --- a/script/provider/diagnostic.lua +++ b/script/provider/diagnostic.lua @@ -183,7 +183,7 @@ function m.syntaxErrors(uri, ast) local results = {} pcall(function () - local disables = config.get(uri, 'Lua.diagnostics.disable') + local disables = util.arrayToHash(config.get(uri, 'Lua.diagnostics.disable')) for _, err in ipairs(ast.errs) do if not disables[err.type:lower():gsub('_', '-')] then results[#results+1] = buildSyntaxError(uri, err) diff --git a/script/provider/formatting.lua b/script/provider/formatting.lua index 4c153330..7bb802e1 100644 --- a/script/provider/formatting.lua +++ b/script/provider/formatting.lua @@ -83,7 +83,7 @@ function m.updateNonStandardSymbols(symbols) end local eqTokens = {} - for token in pairs(symbols) do + for _, token in ipairs(symbols) do if token:find("=") and token ~= "!=" then table.insert(eqTokens, token) end diff --git a/script/provider/spell.lua b/script/provider/spell.lua index 60efc223..3909ddd2 100644 --- a/script/provider/spell.lua +++ b/script/provider/spell.lua @@ -7,6 +7,7 @@ local fs = require 'bee.filesystem' local config = require 'config' local diagnostics = require 'provider.diagnostic' local pformatting = require 'provider.formatting' +local util = require 'utility' local m = {} diff --git a/script/utility.lua b/script/utility.lua index 47b0c8d8..0edf6be0 100644 --- a/script/utility.lua +++ b/script/utility.lua @@ -8,6 +8,7 @@ local ipairs = ipairs local next = next local rawset = rawset local move = table.move +local tableRemove = table.remove local setmetatable = debug.setmetatable local mathType = math.type local mathCeil = math.ceil @@ -791,6 +792,43 @@ function m.multiTable(count, default) return current end +function m.getTableKeys(t, sorter) + local keys = {} + for k in pairs(t) do + keys[#keys+1] = k + end + if sorter == true then + tableSort(keys) + elseif sorter then + tableSort(keys, sorter) + end + return keys +end + +function m.arrayHas(array, value) + for i = 1, #array do + if array[i] == value then + return true + end + end + return false +end + +function m.arrayInsert(array, value) + if not m.arrayHas(array, value) then + array[#array+1] = value + end +end + +function m.arrayRemove(array, value) + for i = 1, #array do + if array[i] == value then + tableRemove(array, i) + return + end + end +end + m.MODE_K = { __mode = 'k' } m.MODE_V = { __mode = 'v' } m.MODE_KV = { __mode = 'kv' } diff --git a/test/diagnostics/common.lua b/test/diagnostics/common.lua index eddf9c26..74cd09f3 100644 --- a/test/diagnostics/common.lua +++ b/test/diagnostics/common.lua @@ -1,4 +1,7 @@ local config = require 'config' +local util = require 'utility' + +local disables = config.get(nil, 'Lua.diagnostics.disable') TEST [[ local <!x!> @@ -125,7 +128,7 @@ local _ENV = { print = print } print(1) ]] -config.get(nil, 'Lua.diagnostics.disable')['undefined-env-child'] = true +util.arrayInsert(disables, 'undefined-env-child') TEST [[ _ENV = nil <!GLOBAL!> = 1 --> _ENV.GLOBAL = 1 @@ -151,7 +154,7 @@ GLOBAL = 1 _ENV = nil ]] -config.get(nil, 'Lua.diagnostics.disable')['undefined-env-child'] = nil +util.arrayRemove(disables, 'undefined-env-child') TEST [[ <!print() ('string')!>:sub(1, 1) @@ -355,12 +358,12 @@ return [[ ]] ]=] -config.get(nil, 'Lua.diagnostics.disable')['close-non-object'] = true +util.arrayInsert(disables, 'close-non-object') TEST [[ local _ <close> = function () end ]] +util.arrayRemove(disables, 'close-non-object') -config.get(nil, 'Lua.diagnostics.disable')['close-non-object'] = nil TEST [[ local _ <close> = <!1!> ]] @@ -373,7 +376,7 @@ TEST [[ local c <close> = <!(function () return 1 end)()!> ]] -config.get(nil, 'Lua.diagnostics.disable')['unused-local'] = true +util.arrayInsert(disables, 'unused-local') TEST [[ local f = <!function () end!> ]] @@ -401,7 +404,7 @@ local <!function foo () end!> ]] -config.get(nil, 'Lua.diagnostics.disable')['unused-local'] = nil +util.arrayRemove(disables, 'unused-local') TEST [[ local mt, x function mt:m() |