@@ -360,12 +360,12 @@ end
360360--- @param output Output Output object to write to
361361--- @param part OpencodeMessagePart
362362function 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 ()
376376--- @param output Output Output object to write to
377377--- @param part OpencodeMessagePart
378378function 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 )
391391end
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
0 commit comments