Skip to content

Commit ee5e312

Browse files
committed
feat(formatter): display diagnostics sent in user prompt
1 parent 29cb605 commit ee5e312

File tree

3 files changed

+44
-5
lines changed

3 files changed

+44
-5
lines changed

lua/opencode/config.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,4 +305,4 @@ return setmetatable(M, {
305305
__tostring = function(_)
306306
return vim.inspect(M.values)
307307
end,
308-
}) --[[@as OpencodeConfig]]
308+
}) --[[@as OpencodeConfig & OpencodeConfigModule]]

lua/opencode/ui/formatter.lua

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -360,12 +360,12 @@ end
360360
---@param output Output Output object to write to
361361
---@param part OpencodeMessagePart
362362
function M._format_selection_context(output, part)
363-
local json = context_module.decode_json_context(part.text, 'selection')
363+
local json = context_module.decode_json_context(part.text or '', 'selection')
364364
if not json then
365365
return
366366
end
367367
local start_line = output:get_line_count()
368-
output:add_lines(vim.split(json.content, '\n'))
368+
output:add_lines(vim.split(json.content or '', '\n'))
369369
output:add_empty_line()
370370

371371
local end_line = output:get_line_count()
@@ -376,7 +376,7 @@ end
376376
---@param output Output Output object to write to
377377
---@param part OpencodeMessagePart
378378
function M._format_cursor_data_context(output, part)
379-
local json = context_module.decode_json_context(part.text, 'cursor-data')
379+
local json = context_module.decode_json_context(part.text or '', 'cursor-data')
380380
if not json then
381381
return
382382
end
@@ -390,6 +390,44 @@ function M._format_cursor_data_context(output, part)
390390
M._add_vertical_border(output, start_line, end_line, 'OpencodeMessageRoleUser', -3)
391391
end
392392

393+
---@param output Output Output object to write to
394+
---@param part OpencodeMessagePart
395+
function M._format_diagnostics_context(output, part)
396+
local json = context_module.decode_json_context(part.text or '', 'diagnostics')
397+
if not json then
398+
return
399+
end
400+
local start_line = output:get_line_count()
401+
local diagnostics = json.content --[[@as vim.Diagnostic[] ]]
402+
if not diagnostics or type(diagnostics) ~= 'table' or #diagnostics == 0 then
403+
return
404+
end
405+
406+
local diagnostics_count = { error = 0, warn = 0, info = 0 }
407+
local diagnostics_icons = {
408+
error = icons.get('error'),
409+
warn = icons.get('warning'),
410+
info = icons.get('info'),
411+
}
412+
413+
for _, diag in ipairs(diagnostics) do
414+
local name = vim.diagnostic.severity[diag.severity]:lower()
415+
diagnostics_count[name] = diagnostics_count[name] + 1
416+
end
417+
418+
local diag_line = '**Diagnostics:**'
419+
for name, count in pairs(diagnostics_count) do
420+
if count > 0 then
421+
diag_line = diag_line .. (string.format(' %s(%d)', diagnostics_icons[name], count))
422+
end
423+
end
424+
output:add_line(diag_line)
425+
output:add_empty_line()
426+
local end_line = output:get_line_count()
427+
428+
M._add_vertical_border(output, start_line, end_line, 'OpencodeMessageRoleUser', -3)
429+
end
430+
393431
---Format and display the file path in the context
394432
---@param output Output Output object to write to
395433
---@param path string|nil File path
@@ -727,6 +765,7 @@ function M.format_part(part, message, is_last_part)
727765
if part.synthetic == true then
728766
M._format_selection_context(output, part)
729767
M._format_cursor_data_context(output, part)
768+
M._format_diagnostics_context(output, part)
730769
else
731770
M._format_user_prompt(output, vim.trim(part.text), message)
732771
content_added = true

lua/opencode/ui/highlight.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ function M.setup()
4949
vim.api.nvim_set_hl(0, 'OpencodeAgentBuild', { bg = '#616161', fg = '#FFFFFF', bold = true, default = true })
5050
vim.api.nvim_set_hl(0, 'OpencodeAgentCustom', { bg = '#3b4261', fg = '#FFFFFF', bold = true, default = true })
5151
vim.api.nvim_set_hl(0, 'OpencodeContextualActions', { bg = '#3b4261', fg = '#61AFEF', bold = true, default = true })
52-
vim.api.nvim_set_hl(0, 'OpencodeInputLegend', { bg = '#616161', fg = '#CCCCCC', bold = false, default = true })
52+
vim.api.nvim_set_hl(0, 'OpencodeInputLegend', { link = '@label', bold = false, default = true })
5353
vim.api.nvim_set_hl(0, 'OpencodeHint', { link = 'Comment', default = true })
5454
vim.api.nvim_set_hl(0, 'OpencodeGuardDenied', { fg = '#EF5350', bold = true, default = true })
5555
vim.api.nvim_set_hl(0, 'OpencodeContextBar', { fg = '#3b4261', default = true })

0 commit comments

Comments
 (0)