diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2022-03-01 19:51:49 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2022-03-01 19:51:49 +0800 |
commit | 268aee4c5e0851b9cd5a391569bec5371aa124eb (patch) | |
tree | 5eea638e16fb2ad637a346e1cbaf1c590c9e91a5 /script | |
parent | 73c405b176ef9aa8468c6279fab883943def7ade (diff) | |
download | lua-language-server-268aee4c5e0851b9cd5a391569bec5371aa124eb.zip |
offline diagnostic
Diffstat (limited to 'script')
-rw-r--r-- | script/ci/check.lua | 1 | ||||
-rw-r--r-- | script/cli/check.lua | 76 | ||||
-rw-r--r-- | script/cli/init.lua (renamed from script/ci/init.lua) | 4 | ||||
-rw-r--r-- | script/cli/version.lua (renamed from script/ci/version.lua) | 0 | ||||
-rw-r--r-- | script/client.lua | 2 | ||||
-rw-r--r-- | script/global.d.lua | 3 | ||||
-rw-r--r-- | script/lclient.lua | 24 | ||||
-rw-r--r-- | script/proto/define.lua | 2 | ||||
-rw-r--r-- | script/proto/proto.lua | 5 | ||||
-rw-r--r-- | script/provider/diagnostic.lua | 68 |
10 files changed, 140 insertions, 45 deletions
diff --git a/script/ci/check.lua b/script/ci/check.lua deleted file mode 100644 index 90834234..00000000 --- a/script/ci/check.lua +++ /dev/null @@ -1 +0,0 @@ -print(CHECK) diff --git a/script/cli/check.lua b/script/cli/check.lua new file mode 100644 index 00000000..e5385c4e --- /dev/null +++ b/script/cli/check.lua @@ -0,0 +1,76 @@ +local lclient = require 'lclient' +local furi = require 'file-uri' +local ws = require 'workspace' +local files = require 'files' +local diag = require 'provider.diagnostic' +local util = require 'utility' +local json = require 'json-beautify' +local lang = require 'language' +local define = require 'proto.define' +local config = require 'config.config' + +if type(CHECK) ~= 'string' then + print(('The argument of CHECK must be a string, but got %s'):format(type(CHECK))) +end + +local rootUri = furi.encode(CHECK) +if not rootUri then + print(('The argument of CHECK must be a valid uri, but got %s'):format(CHECK)) +end + +util.enableCloseFunction() + +local lastClock = os.clock() +local results = {} +---@async +lclient():start(function (client) + client:registerFakers() + + client:initialize { + rootUri = rootUri, + } + + client:register('textDocument/publishDiagnostics', function (params) + results[params.uri] = params.diagnostics + end) + + ws.awaitReady(rootUri) + + local checkLevel = define.DiagnosticSeverity[CHECKLEVEL] or define.DiagnosticSeverity.Warning + local disables = 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) + + local uris = files.getAllUris(rootUri) + local max = #uris + for i, uri in ipairs(uris) do + files.open(uri) + diag.doDiagnostic(uri, true) + if os.clock() - lastClock > 0.2 then + lastClock = os.clock() + print(('%d/%d'):format(i, max)) + end + end +end) + +local count = 0 +for uri, result in pairs(results) do + count = count + #result + if #result == 0 then + results[uri] = nil + end +end + +if count == 0 then + print(lang.script('CLI_CHECK_SUCCESS')) +else + local outpath = LOGPATH .. '/check.json' + util.saveFile(outpath, json.beautify(results)) + + print(lang.script('CLI_CHECK_RESULTS', count, outpath)) +end diff --git a/script/ci/init.lua b/script/cli/init.lua index 8b142291..c2fc7aa8 100644 --- a/script/ci/init.lua +++ b/script/cli/init.lua @@ -1,9 +1,9 @@ if _G['VERSION'] then - require 'ci.version' + require 'cli.version' os.exit(0, true) end if _G['CHECK'] then - require 'ci.check' + require 'cli.check' os.exit(0, true) end diff --git a/script/ci/version.lua b/script/cli/version.lua index 03926c28..03926c28 100644 --- a/script/ci/version.lua +++ b/script/cli/version.lua diff --git a/script/client.lua b/script/client.lua index f265cf9a..6c3e503d 100644 --- a/script/client.lua +++ b/script/client.lua @@ -389,7 +389,7 @@ function m.isReady() end local function hookPrint() - if TEST then + if TEST or CLI then return end print = function (...) diff --git a/script/global.d.lua b/script/global.d.lua index 2bc32c29..793f687d 100644 --- a/script/global.d.lua +++ b/script/global.d.lua @@ -43,3 +43,6 @@ PREVIEW = false --check path ---@type string CHECK = '' + +---@type string | '"Error"' | '"Warning"' | '"Information"' | '"Hint"' +CHECKLEVEL = 'Warning' diff --git a/script/lclient.lua b/script/lclient.lua index 8da8d138..ce4c6c60 100644 --- a/script/lclient.lua +++ b/script/lclient.lua @@ -15,6 +15,7 @@ local counter = util.counter() ---@field _gc gc ---@field _waiting table ---@field _methods table +---@field onSend function local mt = {} mt.__index = mt @@ -25,6 +26,9 @@ end function mt:_fakeProto() proto.send = function (data) self._outs[#self._outs+1] = data + if self.onSend then + self:onSend(data) + end end end @@ -76,7 +80,10 @@ function mt:reportHangs() end ---@param callback async fun(client: languageClient) +---@return languageClient function mt:start(callback) + CLI = true + self:_fakeProto() self:_flushServer() self:_localLoadFile() @@ -97,7 +104,7 @@ function mt:start(callback) local jumpedTime = 0 while true do - if finished then + if finished and #self._outs == 0 then break end if await.step() then @@ -119,6 +126,8 @@ function mt:start(callback) end self:remove() + + CLI = false end function mt:gc(obj) @@ -172,10 +181,15 @@ function mt:update() if out.method then local callback = self._methods[out.method] if callback then - proto.doResponse { - id = out.id, - result = callback(out.params), - } + local result = callback(out.params) + await.call(function () + if out.id then + proto.doResponse { + id = out.id, + result = result, + } + end + end) elseif out.method:sub(1, 2) ~= '$/' then error('Unknown method: ' .. out.method) end diff --git a/script/proto/define.lua b/script/proto/define.lua index 8776641b..f1487a4d 100644 --- a/script/proto/define.lua +++ b/script/proto/define.lua @@ -57,7 +57,7 @@ m.DiagnosticDefaultSeverity = { ['duplicate-doc-param'] = 'Warning', ['doc-field-no-class'] = 'Warning', ['duplicate-doc-field'] = 'Warning', - ['unknown-diag-code'] = 'Waiting', + ['unknown-diag-code'] = 'Warning', ['codestyle-check'] = "Warning", } diff --git a/script/proto/proto.lua b/script/proto/proto.lua index 4ba99b88..83a188f9 100644 --- a/script/proto/proto.lua +++ b/script/proto/proto.lua @@ -107,14 +107,11 @@ end function m.request(name, params, callback) local id = reqCounter() - local buf = jsonrpc.encode { + m.send { id = id, method = name, params = params, } - --log.debug('Request', name, #buf) - logSend(buf) - io.write(buf) m.waiting[id] = { id = id, method = name, diff --git a/script/provider/diagnostic.lua b/script/provider/diagnostic.lua index 9f188501..de1a735e 100644 --- a/script/provider/diagnostic.lua +++ b/script/provider/diagnostic.lua @@ -359,6 +359,42 @@ local function askForDisable(uri) end end +---@async +function m.awaitDiagnosticsScope(uri) + local scp = scope.getScope(uri) + while loading.count() > 0 do + await.sleep(1.0) + end + local clock = os.clock() + local bar <close> = progress.create(scope.getScope(uri), lang.script.WORKSPACE_DIAGNOSTIC, 1) + local cancelled + bar:onCancel(function () + log.debug('Cancel workspace diagnostics') + cancelled = true + ---@async + await.call(function () + askForDisable(uri) + end) + end) + local uris = files.getAllUris(uri) + log.info(('Diagnostics scope [%s], files count:[%d]'):format(scp:getName(), #uris)) + for i, uri in ipairs(uris) do + while loading.count() > 0 do + await.sleep(1.0) + end + bar:setMessage(('%d/%d'):format(i, #uris)) + bar:setPercentage(i / #uris * 100) + xpcall(m.doDiagnostic, log.error, uri, true) + await.delay() + if cancelled then + log.debug('Break workspace diagnostics') + break + end + end + bar:remove() + log.debug(('Diagnostics scope [%s] finished, takes [%.3f] sec.'):format(scp:getName(), os.clock() - clock)) +end + function m.diagnosticsScope(uri, force) if not ws.isReady(uri) then return @@ -371,37 +407,7 @@ function m.diagnosticsScope(uri, force) local id = 'diagnosticsScope:' .. scp:getName() await.close(id) await.call(function () ---@async - while loading.count() > 0 do - await.sleep(1.0) - end - local clock = os.clock() - local bar <close> = progress.create(scope.getScope(uri), lang.script.WORKSPACE_DIAGNOSTIC, 1) - local cancelled - bar:onCancel(function () - log.debug('Cancel workspace diagnostics') - cancelled = true - ---@async - await.call(function () - askForDisable(uri) - end) - end) - local uris = files.getAllUris(uri) - log.info(('Diagnostics scope [%s], files count:[%d]'):format(scp:getName(), #uris)) - for i, uri in ipairs(uris) do - while loading.count() > 0 do - await.sleep(1.0) - end - bar:setMessage(('%d/%d'):format(i, #uris)) - bar:setPercentage(i / #uris * 100) - xpcall(m.doDiagnostic, log.error, uri, true) - await.delay() - if cancelled then - log.debug('Break workspace diagnostics') - break - end - end - bar:remove() - log.debug(('Diagnostics scope [%s] finished, takes [%.3f] sec.'):format(scp:getName(), os.clock() - clock)) + m.awaitDiagnosticsScope(uri) end, id) end |