-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathformatter.lua
More file actions
714 lines (618 loc) · 23.4 KB
/
formatter.lua
File metadata and controls
714 lines (618 loc) · 23.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
local context_module = require('opencode.context')
local icons = require('opencode.ui.icons')
local util = require('opencode.util')
local Output = require('opencode.ui.output')
local state = require('opencode.state')
local config = require('opencode.config')
local snapshot = require('opencode.snapshot')
local mention = require('opencode.ui.mention')
local permission_window = require('opencode.ui.permission_window')
local tool_formatters = require('opencode.ui.formatter.tools')
local format_utils = require('opencode.ui.formatter.utils')
local M = {}
M.separator = {
'----',
'',
}
---@param output Output
---@param part OpencodeMessagePart
function M._format_reasoning(output, part)
local text = vim.trim(part.text or '')
local start_line = output:get_line_count() + 1
local title = 'Reasoning'
local time = part.time
if time and type(time) == 'table' and time.start then
local duration_text = util.format_duration_seconds(time.start, time['end'])
if duration_text then
title = string.format('%s %s', title, duration_text)
end
end
format_utils.format_action(output, icons.get('reasoning'), title, '')
if config.ui.output.tools.show_reasoning_output and text ~= '' then
output:add_empty_line()
output:add_lines(vim.split(text, '\n'))
output:add_empty_line()
end
local end_line = output:get_line_count()
if end_line - start_line > 1 then
M.add_vertical_border(output, start_line, end_line, 'OpencodeToolBorder', -1, 'OpencodeReasoningText')
else
output:add_extmark(start_line - 1, {
line_hl_group = 'OpencodeReasoningText',
} --[[@as OutputExtmark]])
end
end
---Format the revert callout with statistics
---@param session_data OpencodeMessage[] All messages in the session
---@param start_idx number Index of the message where revert occurred
---@return Output output object representing the lines, extmarks, and actions
function M._format_revert_message(session_data, start_idx)
local output = Output.new()
local stats = format_utils.calculate_revert_stats(session_data, start_idx, state.active_session.revert)
local message_text = stats.messages == 1 and 'message' or 'messages'
local tool_text = stats.tool_calls == 1 and 'tool call' or 'tool calls'
output:add_line(
string.format('> %d %s reverted, %d %s reverted', stats.messages, message_text, stats.tool_calls, tool_text)
)
output:add_line('>')
output:add_line('> type `/redo` to restore.')
output:add_empty_line()
if stats.files and next(stats.files) then
for file, fstats in pairs(stats.files) do
local file_diff = {}
if fstats.additions > 0 then
table.insert(file_diff, '+' .. fstats.additions)
end
if fstats.deletions > 0 then
table.insert(file_diff, '-' .. fstats.deletions)
end
if #file_diff > 0 then
local line_str = string.format(icons.get('file') .. '%s: %s', file, table.concat(file_diff, ' '))
local line_idx = output:add_line(line_str)
local col = #(' ' .. file .. ': ')
for _, diff in ipairs(file_diff) do
local hl_group = diff:sub(1, 1) == '+' and 'OpencodeDiffAddText' or 'OpencodeDiffDeleteText'
output:add_extmark(line_idx - 1, {
virt_text = { { diff, hl_group } },
virt_text_pos = 'inline',
virt_text_win_col = col,
priority = 1000,
} --[[@as OutputExtmark]])
col = col + #diff + 1
end
end
end
end
output:add_empty_line()
return output
end
---@param hidden_count integer
---@return Output
function M._format_hidden_messages_notice(hidden_count)
local output = Output.new()
local message_text = hidden_count == 1 and 'message is' or 'messages are'
output:add_line(string.format('> %d older %s not displayed.', hidden_count, message_text))
output:add_action({
text = 'Show [A]ll messages',
type = 'toggle_max_messages',
args = {},
key = 'A',
display_line = output:get_line_count() - 1,
range = { from = output:get_line_count() - 1, to = output:get_line_count() - 1 },
})
output:add_empty_line()
return output
end
---@param output Output
---@param text string
---@param action_type string
---@param args any[]
---@param key? string
---@param line? integer
local function add_action(output, text, action_type, args, key, line)
-- actions use api-indexing (e.g. 0 indexed)
line = (line or output:get_line_count()) - 1
output:add_action({
text = text,
type = action_type,
args = args,
key = key,
display_line = line,
range = { from = line, to = line },
})
end
---@param output Output Output object to write to
---@param part OpencodeMessagePart
function M._format_patch(output, part)
if not part.hash then
return
end
local restore_points = snapshot.get_restore_points_by_parent(part.hash) or {}
format_utils.format_action(output, icons.get('snapshot'), 'Created Snapshot', vim.trim(part.hash:sub(1, 8)))
-- Anchor all snapshot-level actions to the snapshot header line
add_action(output, '[R]evert file', 'diff_revert_selected_file', { part.hash }, 'R')
add_action(output, 'Revert [A]ll', 'diff_revert_all', { part.hash }, 'A')
add_action(output, '[D]iff', 'diff_open', { part.hash }, 'D')
if #restore_points > 0 then
for _, restore_point in ipairs(restore_points) do
output:add_line(
string.format(
' %s Restore point `%s` - %s ',
icons.get('restore_point'),
vim.trim(restore_point.id:sub(1, 8)),
util.format_time(restore_point.created_at)
)
)
add_action(output, 'Restore [A]ll', 'diff_restore_snapshot_all', { restore_point.id }, 'A')
add_action(output, '[R]estore file', 'diff_restore_snapshot_file', { restore_point.id }, 'R')
end
end
end
---@param output Output Output object to write to
---@param message MessageInfo
function M._format_error(output, message)
output:add_empty_line()
M._format_callout(output, 'ERROR', vim.inspect(message.error))
end
---@param message OpencodeMessage
---@param previous_message? OpencodeMessage
---@return Output
function M.format_message_header(message, previous_message)
local output = Output.new()
if message.info and message.info.id == '__opencode_revert_message__' then
output:add_lines(M.separator)
return output
end
if message.info and message.info.id == '__opencode_hidden_messages_notice__' then
return output
end
local role = message.info.role or 'unknown'
local icon = message.info.role == 'user' and icons.get('header_user') or icons.get('header_assistant')
local time = message.info.time and message.info.time.created or nil
local role_hl = 'OpencodeMessageRole' .. role:sub(1, 1):upper() .. role:sub(2)
local model_text = message.info.modelID and ' ' .. message.info.modelID or ''
local debug_text = config.debug.show_ids and ' [' .. message.info.id .. ']' or ''
local display_name
if role == 'assistant' then
local mode = message.info.mode
if mode and mode ~= '' then
display_name = mode:upper()
elseif state.current_mode and state.current_mode ~= '' then
display_name = state.current_mode:upper()
else
display_name = 'ASSISTANT'
end
else
display_name = role:upper()
end
local same_mode_as_previous = false
if config.ui.output.compact_assistant_headers and role == 'assistant' and previous_message then
local previous_role = previous_message.info and previous_message.info.role or nil
local previous_mode = previous_message.info and previous_message.info.mode or state.current_mode
local current_mode = message.info.mode or state.current_mode
same_mode_as_previous = previous_role == 'assistant'
and current_mode
and previous_mode
and current_mode ~= ''
and previous_mode ~= ''
and current_mode == previous_mode
end
if not same_mode_as_previous then
output:add_lines(M.separator)
else
output:add_line('')
end
if not same_mode_as_previous then
output:add_extmark(output:get_line_count() - 1, {
virt_text = {
{ icon, role_hl },
{ ' ' },
{ display_name, role_hl },
{ model_text, 'OpencodeHint' },
{ debug_text, 'OpencodeHint' },
},
virt_text_win_col = -3,
priority = 10,
} --[[@as OutputExtmark]])
end
if time then
output:add_extmark(output:get_line_count() - 1, {
virt_text = { { (same_mode_as_previous and '' or ' ') .. util.format_time(time), 'OpencodeHint' } },
virt_text_pos = 'right_align',
priority = 9,
} --[[@as OutputExtmark]])
end
-- Only want to show the error if we have no parts. If we have parts, they'll
-- handle rendering the error
if
role == 'assistant'
and message.info.error
and message.info.error ~= ''
and (not message.parts or #message.parts == 0)
then
local error = message.info.error
local error_message = error.data and error.data.message or vim.inspect(error)
output:add_line('')
M._format_callout(output, 'ERROR', error_message)
end
output:add_line('')
return output
end
---@param output Output Output object to write to
---@param callout string Callout type (e.g., 'ERROR', 'TODO')
---@param text string Callout text content
---@param title? string Optional title for the callout
function M._format_callout(output, callout, text, title)
title = title and title .. ' ' or ''
local win_width = (state.windows and state.windows.output_win and vim.api.nvim_win_is_valid(state.windows.output_win))
and vim.api.nvim_win_get_width(state.windows.output_win)
or config.ui.window_width
or 80
if #text > win_width - 4 then
local ok, substituted = pcall(vim.fn.substitute, text, '\v(.{' .. (win_width - 8) .. '})', '\1\n', 'g')
text = ok and substituted or text
end
-- Trim off any trailing newlines so there isn't an extra line in the
-- extmarks section
local lines = vim.split(text:gsub('\n$', ''), '\n')
if #lines == 1 and title == '' then
output:add_line('> [!' .. callout .. '] ' .. lines[1])
else
output:add_line('> [!' .. callout .. ']' .. title)
output:add_line('>')
output:add_lines(lines, '> ')
end
end
---@param output Output Output object to write to
---@param text string
---@param message? OpencodeMessage Optional message object to extract mentions from
function M._format_user_prompt(output, text, message)
local start_line = output:get_line_count()
output:add_lines(vim.split(text, '\n'))
local end_line = output:get_line_count()
local end_line_extmark_offset = 0
local mentions = {}
if message and message.parts then
-- message.parts will only be filled out on a re-render
-- we need to collect the mentions here
for _, part in ipairs(message.parts) do
if part.type == 'file' then
-- we're rerendering this part and we have files, the space after the user prompt
-- also needs an extmark
end_line_extmark_offset = 1
if part.source and part.source.text then
table.insert(mentions, part.source.text)
end
elseif part.type == 'agent' then
if part.source then
table.insert(mentions, part.source)
end
end
end
end
if #mentions > 0 then
mention.highlight_mentions_in_output(output, text, mentions, start_line)
end
M.add_vertical_border(output, start_line, end_line + end_line_extmark_offset, 'OpencodeMessageRoleUser', -3)
end
---@param output Output Output object to write to
---@param part OpencodeMessagePart
function M._format_selection_context(output, part)
local part_message = part._message_context
local json = context_module.decode_json_context(part.text or '', 'selection')
if not json then
return
end
local start_line = output:get_line_count() + 1
if part_message and part_message.parts then
for i, message_part in ipairs(part_message.parts) do
if message_part.id == part.id then
local previous_part = part_message.parts[i - 1]
if previous_part and previous_part.type == 'text' and previous_part.synthetic then
local has_selection = context_module.decode_json_context(previous_part.text or '', 'selection') ~= nil
local has_cursor = context_module.decode_json_context(previous_part.text or '', 'cursor-data') ~= nil
local diagnostics = context_module.decode_json_context(previous_part.text or '', 'diagnostics')
local has_diagnostics = diagnostics
and diagnostics.content
and type(diagnostics.content) == 'table'
and #diagnostics.content > 0
if has_selection or has_cursor or has_diagnostics then
start_line = output:get_line_count()
end
end
break
end
end
end
output:add_lines(vim.split(json.content or '', '\n'))
output:add_empty_line()
local end_line = output:get_line_count()
M.add_vertical_border(output, start_line, end_line, 'OpencodeMessageRoleUser', -3)
end
---@param output Output Output object to write to
---@param part OpencodeMessagePart
function M._format_cursor_data_context(output, part)
local json = context_module.decode_json_context(part.text or '', 'cursor-data')
if not json then
return
end
local start_line = output:get_line_count()
output:add_line('Line ' .. json.line .. ':')
output:add_lines(vim.split(json.line_content or '', '\n'))
output:add_empty_line()
local end_line = output:get_line_count()
M.add_vertical_border(output, start_line, end_line, 'OpencodeMessageRoleUser', -3)
end
---@param output Output Output object to write to
---@param part OpencodeMessagePart
function M._format_diagnostics_context(output, part)
local json = context_module.decode_json_context(part.text or '', 'diagnostics')
if not json then
return
end
local start_line = output:get_line_count()
local diagnostics = json.content --[[@as OpencodeDiagnostic[] ]]
if not diagnostics or type(diagnostics) ~= 'table' or #diagnostics == 0 then
return
end
local diagnostics_count = { error = 0, warn = 0, info = 0 }
local diagnostics_icons = {
error = icons.get('error'),
warn = icons.get('warning'),
info = icons.get('info'),
}
for _, diag in ipairs(diagnostics) do
local name = vim.diagnostic.severity[diag.severity]:lower()
diagnostics_count[name] = diagnostics_count[name] + 1
end
local diag_line = '**Diagnostics:**'
for name, count in pairs(diagnostics_count) do
if count > 0 then
diag_line = diag_line .. (string.format(' %s(%d)', diagnostics_icons[name], count))
end
end
output:add_line(diag_line)
output:add_empty_line()
local end_line = output:get_line_count()
M.add_vertical_border(output, start_line, end_line, 'OpencodeMessageRoleUser', -3)
end
---@param part OpencodeMessagePart|nil
---@return string|nil
local function get_visible_user_part_kind(part)
if not part then
return nil
end
if part.type == 'file' and part.filename and part.filename ~= '' then
return 'file'
end
if part.type ~= 'text' or not part.text or part.text == '' then
return nil
end
if not part.synthetic then
return 'text'
end
if context_module.decode_json_context(part.text, 'selection') then
return 'selection'
end
if context_module.decode_json_context(part.text, 'cursor-data') then
return 'cursor-data'
end
local diagnostics = context_module.decode_json_context(part.text, 'diagnostics')
if diagnostics and diagnostics.content and type(diagnostics.content) == 'table' and #diagnostics.content > 0 then
return 'diagnostics'
end
return nil
end
---@param message OpencodeMessage|nil
---@param part OpencodeMessagePart|nil
---@return string|nil previous_kind
---@return string|nil next_kind
local function get_user_part_neighbors(message, part)
if not message or not message.parts or not part or not part.id then
return nil, nil
end
local current_index = nil
for i, message_part in ipairs(message.parts) do
if message_part.id == part.id then
current_index = i
break
end
end
if not current_index then
return nil, nil
end
local previous_kind = nil
for i = current_index - 1, 1, -1 do
previous_kind = get_visible_user_part_kind(message.parts[i])
if previous_kind then
break
end
end
local next_kind = nil
for i = current_index + 1, #message.parts do
next_kind = get_visible_user_part_kind(message.parts[i])
if next_kind then
break
end
end
return previous_kind, next_kind
end
---Format and display the file path in the context
---@param output Output Output object to write to
---@param path string|nil File path
function M._format_context_file(output, path)
if not path or path == '' then
return
end
local cwd = vim.fn.getcwd()
if vim.startswith(path, cwd) then
path = path:sub(#cwd + 2)
end
return output:add_line(string.format('[`%s`](%s)', path, path))
end
---@param output Output Output object to write to
---@param text string
---@param message_id string|nil Optional message ID for reference parsing
function M._format_assistant_message(output, text, message_id)
local reference_picker = require('opencode.ui.reference_picker')
local references = reference_picker.parse_references(text, message_id)
-- If no references, just add the text as-is
if #references == 0 then
output:add_lines(vim.split(text, '\n'))
return
end
-- Sort references by match_start position (ascending)
table.sort(references, function(a, b)
return a.match_start < b.match_start
end)
-- Build a new text with icons inserted before each reference
local result = ''
local last_pos = 1
local ref_icon = icons.get('reference')
for _, ref in ipairs(references) do
-- Add text before this reference
result = result .. text:sub(last_pos, ref.match_start - 1)
-- Add the icon and the reference
result = result .. ref_icon .. text:sub(ref.match_start, ref.match_end)
last_pos = ref.match_end + 1
end
-- Add any remaining text after the last reference
if last_pos <= #text then
result = result .. text:sub(last_pos)
end
output:add_lines(vim.split(result, '\n'))
end
---@param output Output Output object to write to
---@param part OpencodeMessagePart
---@param get_child_parts? fun(session_id: string): OpencodeMessagePart[]?
function M.format_tool(output, part, get_child_parts)
local tool = part.tool
if not tool or not part.state then
return
end
local start_line = output:get_line_count() + 1
local formatter = tool_formatters[tool] or tool_formatters.tool
formatter.format(output, part, get_child_parts)
if part.state.status == 'error' and part.state.error then
output:add_line('')
M._format_callout(output, 'ERROR', part.state.error)
---@diagnostic disable-next-line: undefined-field
elseif part.state.input and part.state.input.error then
output:add_line('')
---I'm not sure about the type with state.input.error
---@diagnostic disable-next-line: undefined-field
M._format_callout(output, 'ERROR', part.state.input.error)
end
local end_line = output:get_line_count()
if end_line - start_line > 1 then
M.add_vertical_border(output, start_line, end_line, 'OpencodeToolBorder', -1)
end
end
---@param output Output Output object to write to
---@param start_line number
---@param end_line number
---@param hl_group string Highlight group for the border character
---@param win_col number
---@param text_hl_group? string Optional highlight group for the background/foreground of text lines
function M.add_vertical_border(output, start_line, end_line, hl_group, win_col, text_hl_group)
local extmark_opts = {
virt_text = { { require('opencode.ui.icons').get('border'), hl_group } },
virt_text_pos = 'overlay',
virt_text_win_col = win_col,
virt_text_repeat_linebreak = true,
line_hl_group = text_hl_group or nil,
}
for line = start_line, end_line do
output:add_extmark(line - 1, extmark_opts --[[@as OutputExtmark]])
end
end
---Formats a single message part and returns the resulting output object
---@param part OpencodeMessagePart The part to format
---@param message? OpencodeMessage Optional message object to extract role and mentions from
---@param is_last_part? boolean Whether this is the last part in the message, used to show an error if there is one
---@param get_child_parts? fun(session_id: string): OpencodeMessagePart[]?
---@return Output
function M.format_part(part, message, is_last_part, get_child_parts)
local output = Output.new()
if not message or not message.info or not message.info.role then
return output
end
local content_added = false
local role = message.info.role
if role == 'user' then
if part.type == 'text' and part.text then
if part.synthetic == true then
part._message_context = message
M._format_selection_context(output, part)
M._format_cursor_data_context(output, part)
M._format_diagnostics_context(output, part)
part._message_context = nil
else
M._format_user_prompt(output, vim.trim(part.text), message)
content_added = true
end
elseif part.type == 'file' then
local file_line = M._format_context_file(output, part.filename)
if file_line then
local previous_kind, next_kind = get_user_part_neighbors(message, part)
local previous_is_context = previous_kind == 'selection'
or previous_kind == 'cursor-data'
or previous_kind == 'diagnostics'
if next_kind == 'text' or (previous_is_context and not next_kind) then
M.add_vertical_border(output, file_line - 1, file_line, 'OpencodeMessageRoleUser', -3)
elseif next_kind == 'file' then
M.add_vertical_border(output, file_line, file_line + 1, 'OpencodeMessageRoleUser', -3)
else
M.add_vertical_border(output, file_line, file_line, 'OpencodeMessageRoleUser', -3)
end
content_added = true
end
end
elseif role == 'assistant' then
if part.type == 'text' and part.text then
M._format_assistant_message(output, vim.trim(part.text), part.messageID)
content_added = true
elseif part.type == 'reasoning' then
M._format_reasoning(output, part)
content_added = true
elseif part.type == 'tool' then
M.format_tool(output, part, get_child_parts)
content_added = true
elseif part.type == 'patch' and part.hash then
M._format_patch(output, part)
content_added = true
end
elseif role == 'system' then
if part.type == 'permissions-display' then
permission_window.format_display(output)
content_added = true
elseif part.type == 'questions-display' then
local question_window = require('opencode.ui.question_window')
question_window.format_display(output)
content_added = true
elseif part.type == 'revert-display' then
local revert_index = part.state and part.state.revert_index
if revert_index then
output = M._format_revert_message(state.messages or {}, revert_index)
content_added = output:get_line_count() > 0
end
elseif part.type == 'hidden-messages-display' then
local hidden_count = part.state and part.state.hidden_count
if type(hidden_count) == 'number' and hidden_count > 0 then
output = M._format_hidden_messages_notice(hidden_count)
content_added = output:get_line_count() > 0
end
end
end
if content_added then
output:add_empty_line()
end
if is_last_part and role == 'assistant' and message.info.error and message.info.error ~= '' then
local error = message.info.error
local error_message = error.data and error.data.message or vim.inspect(error)
M._format_callout(output, 'ERROR', error_message)
output:add_empty_line()
end
return output
end
return M