Skip to content

Commit 5808ec4

Browse files
committed
feat: add --check_format=json|pretty
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.
1 parent 5275a75 commit 5808ec4

File tree

9 files changed

+167
-32
lines changed

9 files changed

+167
-32
lines changed

changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
cfgs[2] = {} -- only warns missing `b`
3131
```
3232
This enables the previous missing field check behavior before [#2970](https://github.com/LuaLS/lua-language-server/issues/2970)
33+
* `NEW` Added `--check_format=json|pretty` for use with `--check` to output diagnostics in a human readable format.
3334

3435
## 3.13.5
3536
`2024-12-20`

locale/en-us/script.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -648,8 +648,10 @@ CLI_CHECK_SUCCESS =
648648
'Diagnosis completed, no problems found'
649649
CLI_CHECK_PROGRESS =
650650
'Found {} problems in {} files'
651-
CLI_CHECK_RESULTS =
651+
CLI_CHECK_RESULTS_OUTPATH =
652652
'Diagnosis complete, {} problems found, see {}'
653+
CLI_CHECK_RESULTS_PRETTY =
654+
'Diagnosis complete, {} problems found'
653655
CLI_CHECK_MULTIPLE_WORKERS =
654656
'Starting {} worker tasks, progress output will be disabled. This may take a few minutes.'
655657
CLI_DOC_INITING =

locale/ja-jp/script.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -649,8 +649,10 @@ CLI_CHECK_SUCCESS =
649649
'診断が完了しました。問題は見つかりませんでした'
650650
CLI_CHECK_PROGRESS =
651651
'{} ファイルに渡り、{} 個の問題が発見されました'
652-
CLI_CHECK_RESULTS =
652+
CLI_CHECK_RESULTS_OUTPATH =
653653
'診断が完了しました。{} 個の問題が発見されました。詳しくは {} をご確認ください'
654+
CLI_CHECK_RESULTS_PRETTY =
655+
'診断が完了しました。{} 個の問題が発見されました'
654656
CLI_CHECK_MULTIPLE_WORKERS =
655657
'{} 個のワーカータスクを開始しているため、進行状況の出力が無効になります。完了まで数分かかることがあります。'
656658
CLI_DOC_INITING =

locale/pt-br/script.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -648,8 +648,10 @@ CLI_CHECK_SUCCESS =
648648
'Diagnóstico completo, nenhum problema encontrado'
649649
CLI_CHECK_PROGRESS = -- TODO: need translate!
650650
'Found {} problems in {} files'
651-
CLI_CHECK_RESULTS =
651+
CLI_CHECK_RESULTS_OUTPATH =
652652
'Diagnóstico completo, {} problemas encontrados, veja {}'
653+
CLI_CHECK_RESULTS_PRETTY =
654+
'Diagnóstico completo, {} problemas encontrados'
653655
CLI_CHECK_MULTIPLE_WORKERS = -- TODO: need translate!
654656
'Starting {} worker tasks, progress output will be disabled. This may take a few minutes.'
655657
CLI_DOC_INITING = -- TODO: need translate!

locale/zh-cn/script.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -648,8 +648,10 @@ CLI_CHECK_SUCCESS =
648648
'诊断完成,没有发现问题'
649649
CLI_CHECK_PROGRESS =
650650
'检测到问题 {} 在文件 {} 中'
651-
CLI_CHECK_RESULTS =
651+
CLI_CHECK_RESULTS_OUTPATH =
652652
'诊断完成,共有 {} 个问题,请查看 {}'
653+
CLI_CHECK_RESULTS_PRETTY =
654+
'诊断完成,共有 {} 个问题'
653655
CLI_CHECK_MULTIPLE_WORKERS =
654656
'开启 {} 个工作任务,进度输出将会被禁用。这可能会花费几分钟。'
655657
CLI_DOC_INITING =

locale/zh-tw/script.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -648,8 +648,10 @@ CLI_CHECK_SUCCESS =
648648
'診斷完成,沒有發現問題'
649649
CLI_CHECK_PROGRESS = -- TODO: need translate!
650650
'Found {} problems in {} files'
651-
CLI_CHECK_RESULTS =
651+
CLI_CHECK_RESULTS_OUTPATH =
652652
'診斷完成,共有 {} 個問題,請查看 {}'
653+
CLI_CHECK_RESULTS_PRETTY =
654+
'診斷完成,共有 {} 個問題'
653655
CLI_CHECK_MULTIPLE_WORKERS = -- TODO: need translate!
654656
'Starting {} worker tasks, progress output will be disabled. This may take a few minutes.'
655657
CLI_DOC_INITING = -- TODO: need translate!

script/cli/check.lua

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,17 @@ function export.runCLI()
4949
args[#args + 1] = '--thread_id'
5050
args[#args + 1] = tostring(threadId)
5151
if numThreads > 1 then
52-
args[#args + 1] = '--quiet'
52+
if QUIET then
53+
args[#args + 1] = '--quiet'
54+
end
55+
args[#args + 1] = '--quiet_worker'
5356
args[#args + 1] = '--check_out_path'
5457
args[#args + 1] = logFileForThread(threadId)
5558
end
5659
return args
5760
end
5861

59-
if numThreads > 1 then
62+
if not QUIET and numThreads > 1 then
6063
print(lang.script('CLI_CHECK_MULTIPLE_WORKERS', numThreads))
6164
end
6265

@@ -76,11 +79,6 @@ function export.runCLI()
7679
checkPassed = process:wait() == 0 and checkPassed
7780
end
7881

79-
local outpath = CHECK_OUT_PATH
80-
if outpath == nil then
81-
outpath = LOGPATH .. '/check.json'
82-
end
83-
8482
if numThreads > 1 then
8583
local mergedResults = {}
8684
local count = 0
@@ -95,11 +93,22 @@ function export.runCLI()
9593
end
9694
end
9795
end
98-
util.saveFile(outpath, jsonb.beautify(mergedResults))
99-
if count == 0 then
100-
print(lang.script('CLI_CHECK_SUCCESS'))
101-
else
102-
print(lang.script('CLI_CHECK_RESULTS', count, outpath))
96+
97+
local outpath = nil
98+
99+
if CHECK_FORMAT == 'json' or CHECK_OUT_PATH then
100+
outpath = CHECK_OUT_PATH or LOGPATH .. '/check.json'
101+
util.saveFile(outpath, jsonb.beautify(mergedResults))
102+
end
103+
104+
if not QUIET then
105+
if count == 0 then
106+
print(lang.script('CLI_CHECK_SUCCESS'))
107+
elseif outpath then
108+
print(lang.script('CLI_CHECK_RESULTS_OUTPATH', count, outpath))
109+
else
110+
print(lang.script('CLI_CHECK_RESULTS_PRETTY', count))
111+
end
103112
end
104113
end
105114
return checkPassed and 0 or 1

script/cli/check_worker.lua

Lines changed: 125 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,109 @@ require 'vm'
1717

1818
local export = {}
1919

20+
local colors
21+
22+
if not os.getenv('NO_COLOR') then
23+
colors = {
24+
red = '\27[31m',
25+
green = '\27[32m',
26+
yellow = '\27[33m',
27+
blue = '\27[34m',
28+
magenta = '\27[35m',
29+
white = '\27[37m',
30+
grey = '\27[90m',
31+
reset = '\27[0m'
32+
}
33+
else
34+
colors = {
35+
red = '',
36+
green = '',
37+
yellow = '',
38+
blue = '',
39+
magenta = '',
40+
white = '',
41+
grey = '',
42+
reset = ''
43+
}
44+
end
45+
46+
--- @type table<DiagnosticSeverity, string>
47+
local severity_colors = {
48+
Error = colors.red,
49+
Warning = colors.yellow,
50+
Information = colors.white,
51+
Hint = colors.white,
52+
}
53+
54+
local severity_str = {} --- @type table<integer,DiagnosticSeverity>
55+
for k, v in pairs(define.DiagnosticSeverity) do
56+
severity_str[v] = k
57+
end
58+
59+
local pwd
60+
61+
---@param path string
62+
---@return string
63+
local function relpath(path)
64+
if not pwd then
65+
pwd = furi.decode(furi.encode(fs.current_path():string()))
66+
end
67+
if pwd and path:sub(1, #pwd) == pwd then
68+
path = path:sub(#pwd + 2)
69+
end
70+
return path
71+
end
72+
73+
local function report_pretty(uri, diags)
74+
local path = relpath(furi.decode(uri))
75+
76+
local lines = {} --- @type string[]
77+
pcall(function()
78+
for line in io.lines(path) do
79+
table.insert(lines, line)
80+
end
81+
end)
82+
83+
for _, d in ipairs(diags) do
84+
local rstart = d.range.start
85+
local rend = d.range['end']
86+
local severity = severity_str[d.severity]
87+
print(
88+
('%s%s:%s:%s%s [%s%s%s] %s %s(%s)%s'):format(
89+
colors.blue,
90+
path,
91+
rstart.line + 1, -- Use 1-based indexing
92+
rstart.character + 1, -- Use 1-based indexing
93+
colors.reset,
94+
severity_colors[severity],
95+
severity,
96+
colors.reset,
97+
d.message,
98+
colors.magenta,
99+
d.code,
100+
colors.reset
101+
)
102+
)
103+
if #lines > 0 then
104+
io.write(' ', lines[rstart.line + 1], '\n')
105+
io.write(' ', colors.grey, (' '):rep(rstart.character), '^')
106+
if rstart.line == rend.line then
107+
io.write(('^'):rep(rend.character - rstart.character - 1))
108+
end
109+
io.write(colors.reset, '\n')
110+
end
111+
end
112+
end
113+
114+
local function clear_line()
115+
-- Write out empty space to ensure that the previous lien is cleared.
116+
io.write('\x0D', (' '):rep(80), '\x0D')
117+
end
118+
119+
local function quiet()
120+
return QUIET or QUIET_WORKER
121+
end
122+
20123
function export.runCLI()
21124
lang(LOCALE)
22125

@@ -65,9 +168,13 @@ function export.runCLI()
65168

66169
client:register('textDocument/publishDiagnostics', function (params)
67170
results[params.uri] = params.diagnostics
171+
if not QUIET and (CHECK_FORMAT == nil or CHECK_FORMAT == 'pretty') then
172+
clear_line()
173+
report_pretty(params.uri, params.diagnostics)
174+
end
68175
end)
69176

70-
if not QUIET then
177+
if not quiet() then
71178
io.write(lang.script('CLI_CHECK_INITING'))
72179
end
73180

@@ -89,14 +196,14 @@ function export.runCLI()
89196

90197
-- Downgrade file opened status to Opened for everything to avoid reporting during compilation on files that do not belong to this thread
91198
local diagStatus = config.get(rootUri, 'Lua.diagnostics.neededFileStatus')
92-
for diag, status in pairs(diagStatus) do
199+
for d, status in pairs(diagStatus) do
93200
if status == 'Any' or status == 'Any!' then
94-
diagStatus[diag] = 'Opened!'
201+
diagStatus[d] = 'Opened!'
95202
end
96203
end
97-
for diag, status in pairs(protoDiag.getDefaultStatus()) do
204+
for d, status in pairs(protoDiag.getDefaultStatus()) do
98205
if status == 'Any' or status == 'Any!' then
99-
diagStatus[diag] = 'Opened!'
206+
diagStatus[d] = 'Opened!'
100207
end
101208
end
102209
config.set(rootUri, 'Lua.diagnostics.neededFileStatus', diagStatus)
@@ -109,7 +216,7 @@ function export.runCLI()
109216
files.open(uri)
110217
diag.doDiagnostic(uri, true)
111218
-- Print regularly but always print the last entry to ensure that logs written to files don't look incomplete.
112-
if (os.clock() - lastClock > 0.2 or i == #uris) and not QUIET then
219+
if not quiet() and (os.clock() - lastClock > 0.2 or i == #uris) then
113220
lastClock = os.clock()
114221
client:update()
115222
local output = '\x0D'
@@ -133,8 +240,8 @@ function export.runCLI()
133240
end
134241
end
135242
end
136-
if not QUIET then
137-
io.write('\x0D')
243+
if not quiet() then
244+
clear_line()
138245
end
139246
end)
140247

@@ -146,18 +253,21 @@ function export.runCLI()
146253
end
147254
end
148255

149-
local outpath = CHECK_OUT_PATH
150-
if outpath == nil then
151-
outpath = LOGPATH .. '/check.json'
256+
local outpath = nil
257+
258+
if CHECK_FORMAT == 'json' or CHECK_OUT_PATH then
259+
outpath = CHECK_OUT_PATH or LOGPATH .. '/check.json'
260+
-- Always write result, even if it's empty to make sure no one accidentally looks at an old output after a successful run.
261+
util.saveFile(outpath, jsonb.beautify(results))
152262
end
153-
-- Always write result, even if it's empty to make sure no one accidentally looks at an old output after a successful run.
154-
util.saveFile(outpath, jsonb.beautify(results))
155263

156-
if not QUIET then
264+
if not quiet() then
157265
if count == 0 then
158266
print(lang.script('CLI_CHECK_SUCCESS'))
267+
elseif outpath then
268+
print(lang.script('CLI_CHECK_RESULTS_OUTPATH', count, outpath))
159269
else
160-
print(lang.script('CLI_CHECK_RESULTS', count, outpath))
270+
print(lang.script('CLI_CHECK_RESULTS_PRETTY', count))
161271
end
162272
end
163273
return count == 0 and 0 or 1

script/global.d.lua

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ CHECKLEVEL = 'Warning'
6969
---@type string|nil
7070
CHECK_OUT_PATH = ''
7171

72+
---@type string | 'json' | 'pretty'
73+
CHECK_FORMAT = 'pretty'
74+
7275
---@type 'trace' | 'debug' | 'info' | 'warn' | 'error'
7376
LOGLEVEL = 'warn'
7477

@@ -106,3 +109,5 @@ THREAD_ID = 1
106109
CHECK_WORKER = ''
107110

108111
QUIET = false
112+
113+
QUIET_WORKER = false

0 commit comments

Comments
 (0)