Skip to content

Commit

Permalink
feat: add --check_format=json|pretty
Browse files Browse the repository at this point in the history
Adds a new CLI option --check_format which accepts the values: 'json'
(current behaviour), and a new 'pretty' value which prints a colorized
human readable report to stdout, similar to common compilers and
linters.

Results are printed with color unless NO_COLOR is defined in the users
environment, as per https://no-color.org.

If --check_out_path is provided, then the results are always saved to
file regardless of the --check_format value.
  • Loading branch information
lewis6991 committed Jan 24, 2025
1 parent 2a7cc44 commit 6998ad5
Show file tree
Hide file tree
Showing 9 changed files with 162 additions and 27 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
cfgs[2] = {} -- only warns missing `b`
```
This enables the previous missing field check behavior before [#2970](https://github.com/LuaLS/lua-language-server/issues/2970)
* `NEW` Added `--check_format=json|pretty` for use with `--check` to output diagnostics in a human readable format.

## 3.13.5
`2024-12-20`
Expand Down
4 changes: 3 additions & 1 deletion locale/en-us/script.lua
Original file line number Diff line number Diff line change
Expand Up @@ -648,8 +648,10 @@ CLI_CHECK_SUCCESS =
'Diagnosis completed, no problems found'
CLI_CHECK_PROGRESS =
'Found {} problems in {} files'
CLI_CHECK_RESULTS =
CLI_CHECK_RESULTS_OUTPATH =
'Diagnosis complete, {} problems found, see {}'
CLI_CHECK_RESULTS_PRETTY =
'Diagnosis complete, {} problems found'
CLI_CHECK_MULTIPLE_WORKERS =
'Starting {} worker tasks, progress output will be disabled. This may take a few minutes.'
CLI_DOC_INITING =
Expand Down
4 changes: 3 additions & 1 deletion locale/ja-jp/script.lua
Original file line number Diff line number Diff line change
Expand Up @@ -649,8 +649,10 @@ CLI_CHECK_SUCCESS =
'診断が完了しました。問題は見つかりませんでした'
CLI_CHECK_PROGRESS =
'{} ファイルに渡り、{} 個の問題が発見されました'
CLI_CHECK_RESULTS =
CLI_CHECK_RESULTS_OUTPATH =
'診断が完了しました。{} 個の問題が発見されました。詳しくは {} をご確認ください'
CLI_CHECK_RESULTS_PRETTY =
'診断が完了しました。{} 個の問題が発見されました'
CLI_CHECK_MULTIPLE_WORKERS =
'{} 個のワーカータスクを開始しているため、進行状況の出力が無効になります。完了まで数分かかることがあります。'
CLI_DOC_INITING =
Expand Down
4 changes: 3 additions & 1 deletion locale/pt-br/script.lua
Original file line number Diff line number Diff line change
Expand Up @@ -648,8 +648,10 @@ CLI_CHECK_SUCCESS =
'Diagnóstico completo, nenhum problema encontrado'
CLI_CHECK_PROGRESS = -- TODO: need translate!
'Found {} problems in {} files'
CLI_CHECK_RESULTS =
CLI_CHECK_RESULTS_OUTPATH =
'Diagnóstico completo, {} problemas encontrados, veja {}'
CLI_CHECK_RESULTS_PRETTY =
'Diagnóstico completo, {} problemas encontrados'
CLI_CHECK_MULTIPLE_WORKERS = -- TODO: need translate!
'Starting {} worker tasks, progress output will be disabled. This may take a few minutes.'
CLI_DOC_INITING = -- TODO: need translate!
Expand Down
4 changes: 3 additions & 1 deletion locale/zh-cn/script.lua
Original file line number Diff line number Diff line change
Expand Up @@ -648,8 +648,10 @@ CLI_CHECK_SUCCESS =
'诊断完成,没有发现问题'
CLI_CHECK_PROGRESS =
'检测到问题 {} 在文件 {} 中'
CLI_CHECK_RESULTS =
CLI_CHECK_RESULTS_OUTPATH =
'诊断完成,共有 {} 个问题,请查看 {}'
CLI_CHECK_RESULTS_PRETTY =
'诊断完成,共有 {} 个问题'
CLI_CHECK_MULTIPLE_WORKERS =
'开启 {} 个工作任务,进度输出将会被禁用。这可能会花费几分钟。'
CLI_DOC_INITING =
Expand Down
4 changes: 3 additions & 1 deletion locale/zh-tw/script.lua
Original file line number Diff line number Diff line change
Expand Up @@ -648,8 +648,10 @@ CLI_CHECK_SUCCESS =
'診斷完成,沒有發現問題'
CLI_CHECK_PROGRESS = -- TODO: need translate!
'Found {} problems in {} files'
CLI_CHECK_RESULTS =
CLI_CHECK_RESULTS_OUTPATH =
'診斷完成,共有 {} 個問題,請查看 {}'
CLI_CHECK_RESULTS_PRETTY =
'診斷完成,共有 {} 個問題'
CLI_CHECK_MULTIPLE_WORKERS = -- TODO: need translate!
'Starting {} worker tasks, progress output will be disabled. This may take a few minutes.'
CLI_DOC_INITING = -- TODO: need translate!
Expand Down
38 changes: 25 additions & 13 deletions script/cli/check.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ local function logFileForThread(threadId)
return LOGPATH .. '/check-partial-' .. threadId .. '.json'
end

local function buildArgs(exe, numThreads, threadId)
local function buildArgs(exe, numThreads, threadId, format, quiet)
local args = {exe}
local skipNext = false
for i = 1, #arg do
Expand All @@ -35,7 +35,13 @@ local function buildArgs(exe, numThreads, threadId)
args[#args + 1] = '--thread_id'
args[#args + 1] = tostring(threadId)
if numThreads > 1 then
args[#args + 1] = '--quiet'
if quiet then
args[#args + 1] = '--quiet'
end
if format then
args[#args + 1] = '--check_format=' .. format
end
args[#args + 1] = '--quiet_worker'
args[#args + 1] = '--check_out_path'
args[#args + 1] = logFileForThread(threadId)
end
Expand All @@ -62,7 +68,7 @@ function export.runCLI()

local procs = {}
for i = 1, numThreads do
local process, err = subprocess.spawn({buildArgs(exe, numThreads, i)})
local process, err = subprocess.spawn({buildArgs(exe, numThreads, i, CHECK_FORMAT, QUIET)})
if err then
print(err)
end
Expand All @@ -76,11 +82,6 @@ function export.runCLI()
checkPassed = process:wait() == 0 and checkPassed
end

local outpath = CHECK_OUT_PATH
if outpath == nil then
outpath = LOGPATH .. '/check.json'
end

if numThreads > 1 then
local mergedResults = {}
local count = 0
Expand All @@ -95,11 +96,22 @@ function export.runCLI()
end
end
end
util.saveFile(outpath, jsonb.beautify(mergedResults))
if count == 0 then
print(lang.script('CLI_CHECK_SUCCESS'))
else
print(lang.script('CLI_CHECK_RESULTS', count, outpath))

local outpath = nil

if CHECK_FORMAT == 'json' or CHECK_OUT_PATH then
outpath = CHECK_OUT_PATH or LOGPATH .. '/check.json'
util.saveFile(outpath, jsonb.beautify(mergedResults))
end

if not QUIET then
if count == 0 then
print(lang.script('CLI_CHECK_SUCCESS'))
elseif outpath then
print(lang.script('CLI_CHECK_RESULTS_OUTPATH', count, outpath))
else
print(lang.script('CLI_CHECK_RESULTS_PRETTY', count))
end
end
end
return checkPassed and 0 or 1
Expand Down
125 changes: 116 additions & 9 deletions script/cli/check_worker.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,109 @@ require 'vm'

local export = {}

local colors

if not os.getenv('NO_COLOR') then
colors = {
red = '\27[31m',
green = '\27[32m',
yellow = '\27[33m',
blue = '\27[34m',
magenta = '\27[35m',
white = '\27[37m',
grey = '\27[90m',
reset = '\27[0m'
}
else
colors = {
red = '',
green = '',
yellow = '',
blue = '',
magenta = '',
white = '',
grey = '',
reset = ''
}
end

--- @type table<DiagnosticSeverity, string>
local severity_colors = {
Error = colors.red,
Warning = colors.yellow,
Information = colors.white,
Hint = colors.white,
}

local severity_str = {} --- @type table<integer,DiagnosticSeverity>
for k, v in pairs(define.DiagnosticSeverity) do
severity_str[v] = k
end

local pwd

---@param path string
---@return string
local function relpath(path)
if not pwd then
pwd = furi.decode(furi.encode(fs.current_path():string()))
end
if pwd and path:sub(1, #pwd) == pwd then
path = path:sub(#pwd + 2)
end
return path
end

local function report_pretty(uri, diags)
local path = relpath(furi.decode(uri))

local lines = {} --- @type string[]
pcall(function()
for line in io.lines(path) do
table.insert(lines, line)
end
end)

for _, d in ipairs(diags) do
local rstart = d.range.start
local rend = d.range['end']
local severity = severity_str[d.severity]
print(
('%s%s:%s:%s%s [%s%s%s] %s %s(%s)%s'):format(
colors.blue,
path,
rstart.line + 1, -- Use 1-based indexing
rstart.character + 1, -- Use 1-based indexing
colors.reset,
severity_colors[severity],
severity,
colors.reset,
d.message,
colors.magenta,
d.code,
colors.reset
)
)
if #lines > 0 then
io.write(' ', lines[rstart.line + 1], '\n')
io.write(' ', colors.grey, (' '):rep(rstart.character), '^')
if rstart.line == rend.line then
io.write(('^'):rep(rend.character - rstart.character - 1))
end
io.write(colors.reset, '\n')
end
end
end

local function clear_line()
-- Write out empty space to ensure that the previous lien is cleared.
io.write('\x0D', (' '):rep(80), '\x0D')
end

local function quiet()
return QUIET or QUIET_WORKER
end

--- @param i integer
--- @param max integer
--- @param results table<string, table[]>
Expand Down Expand Up @@ -127,9 +225,13 @@ function export.runCLI()

client:register('textDocument/publishDiagnostics', function (params)
results[params.uri] = params.diagnostics
if not QUIET and (CHECK_FORMAT == nil or CHECK_FORMAT == 'pretty') then
clear_line()
report_pretty(params.uri, params.diagnostics)
end
end)

if not QUIET then
if not quiet() then
io.write(lang.script('CLI_CHECK_INITING'))
end

Expand All @@ -153,15 +255,15 @@ function export.runCLI()
diag.doDiagnostic(uri, true)
-- Print regularly but always print the last entry to ensure
-- that logs written to files don't look incomplete.
if not QUIET and (os.clock() - lastClock > 0.2 or i == #uris) then
if not quiet() and (os.clock() - lastClock > 0.2 or i == #uris) then
lastClock = os.clock()
client:update()
report_progress(i, max, results)
end
end
end
if not QUIET then
io.write('\x0D')
if not quiet() then
clear_line()
end
end)

Expand All @@ -173,16 +275,21 @@ function export.runCLI()
end
end

local outpath = CHECK_OUT_PATH or LOGPATH .. '/check.json'
local outpath = nil

-- Always write result, even if it's empty to make sure no one accidentally looks at an old output after a successful run.
util.saveFile(outpath, jsonb.beautify(results))
if CHECK_FORMAT == 'json' or CHECK_OUT_PATH then
outpath = CHECK_OUT_PATH or LOGPATH .. '/check.json'
-- Always write result, even if it's empty to make sure no one accidentally looks at an old output after a successful run.
util.saveFile(outpath, jsonb.beautify(results))
end

if not QUIET then
if not quiet() then
if count == 0 then
print(lang.script('CLI_CHECK_SUCCESS'))
elseif outpath then
print(lang.script('CLI_CHECK_RESULTS_OUTPATH', count, outpath))
else
print(lang.script('CLI_CHECK_RESULTS', count, outpath))
print(lang.script('CLI_CHECK_RESULTS_PRETTY', count))
end
end
return count == 0 and 0 or 1
Expand Down
5 changes: 5 additions & 0 deletions script/global.d.lua
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ CHECKLEVEL = 'Warning'
---@type string|nil
CHECK_OUT_PATH = ''

---@type string | 'json' | 'pretty'
CHECK_FORMAT = 'pretty'

---@type 'trace' | 'debug' | 'info' | 'warn' | 'error'
LOGLEVEL = 'warn'

Expand Down Expand Up @@ -106,3 +109,5 @@ THREAD_ID = 1
CHECK_WORKER = ''

QUIET = false

QUIET_WORKER = false

0 comments on commit 6998ad5

Please sign in to comment.