|
| 1 | +local lclient = require 'lclient' |
| 2 | +local furi = require 'file-uri' |
| 3 | +local ws = require 'workspace' |
| 4 | +local files = require 'files' |
| 5 | +local diag = require 'provider.diagnostic' |
| 6 | +local util = require 'utility' |
| 7 | +local json = require 'json-beautify' |
| 8 | +local lang = require 'language' |
| 9 | +local define = require 'proto.define' |
| 10 | +local config = require 'config.config' |
| 11 | + |
| 12 | +if type(CHECK) ~= 'string' then |
| 13 | + print(('The argument of CHECK must be a string, but got %s'):format(type(CHECK))) |
| 14 | +end |
| 15 | + |
| 16 | +local rootUri = furi.encode(CHECK) |
| 17 | +if not rootUri then |
| 18 | + print(('The argument of CHECK must be a valid uri, but got %s'):format(CHECK)) |
| 19 | +end |
| 20 | + |
| 21 | +util.enableCloseFunction() |
| 22 | + |
| 23 | +local lastClock = os.clock() |
| 24 | +local results = {} |
| 25 | +---@async |
| 26 | +lclient():start(function (client) |
| 27 | + client:registerFakers() |
| 28 | + |
| 29 | + client:initialize { |
| 30 | + rootUri = rootUri, |
| 31 | + } |
| 32 | + |
| 33 | + client:register('textDocument/publishDiagnostics', function (params) |
| 34 | + results[params.uri] = params.diagnostics |
| 35 | + end) |
| 36 | + |
| 37 | + ws.awaitReady(rootUri) |
| 38 | + |
| 39 | + local checkLevel = define.DiagnosticSeverity[CHECKLEVEL] or define.DiagnosticSeverity.Warning |
| 40 | + local disables = config.get(rootUri, 'Lua.diagnostics.disable') |
| 41 | + for name, serverity in pairs(define.DiagnosticDefaultSeverity) do |
| 42 | + serverity = config.get(rootUri, 'Lua.diagnostics.severity')[name] or 'Warning' |
| 43 | + if define.DiagnosticSeverity[serverity] > checkLevel then |
| 44 | + disables[name] = true |
| 45 | + end |
| 46 | + end |
| 47 | + config.set(nil, 'Lua.diagnostics.disable', disables) |
| 48 | + |
| 49 | + local uris = files.getAllUris(rootUri) |
| 50 | + local max = #uris |
| 51 | + for i, uri in ipairs(uris) do |
| 52 | + files.open(uri) |
| 53 | + diag.doDiagnostic(uri, true) |
| 54 | + if os.clock() - lastClock > 0.2 then |
| 55 | + lastClock = os.clock() |
| 56 | + print(('%d/%d'):format(i, max)) |
| 57 | + end |
| 58 | + end |
| 59 | +end) |
| 60 | + |
| 61 | +local count = 0 |
| 62 | +for uri, result in pairs(results) do |
| 63 | + count = count + #result |
| 64 | + if #result == 0 then |
| 65 | + results[uri] = nil |
| 66 | + end |
| 67 | +end |
| 68 | + |
| 69 | +if count == 0 then |
| 70 | + print(lang.script('CLI_CHECK_SUCCESS')) |
| 71 | +else |
| 72 | + local outpath = LOGPATH .. '/check.json' |
| 73 | + util.saveFile(outpath, json.beautify(results)) |
| 74 | + |
| 75 | + print(lang.script('CLI_CHECK_RESULTS', count, outpath)) |
| 76 | +end |
0 commit comments