Skip to content

Commit 7dca492

Browse files
committed
refactor(session_formatter): context file helper
1 parent 2483a6e commit 7dca492

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

lua/opencode/ui/session_formatter.lua

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -423,18 +423,27 @@ function M._format_user_message(text, message)
423423

424424
if context.current_file then
425425
M.output:add_empty_line()
426-
local path = context.current_file or ''
427-
if vim.startswith(path, vim.fn.getcwd()) then
428-
path = path:sub(#vim.fn.getcwd() + 2)
429-
end
430-
M.output:add_line(string.format('[%s](%s)', path, context.current_file))
426+
M._format_context_file(context.current_file)
431427
end
432428

433429
local end_line = M.output:get_line_count()
434430

435431
M._add_vertical_border(start_line, end_line, 'OpencodeMessageRoleUser', -3)
436432
end
437433

434+
---Format and display the file path in the context
435+
---@param path string|nil File path
436+
function M._format_context_file(path)
437+
if not path or path == '' then
438+
return
439+
end
440+
local cwd = vim.fn.getcwd()
441+
if vim.startswith(path, cwd) then
442+
path = path:sub(#cwd + 2)
443+
end
444+
return M.output:add_line(string.format('[%s](%s)', path, path))
445+
end
446+
438447
---@param text string
439448
function M._format_assistant_message(text)
440449
-- M.output:add_empty_line()
@@ -750,11 +759,7 @@ function M.format_part_isolated(part, message_info)
750759
M._format_patch(part)
751760
content_added = true
752761
elseif part.type == 'file' then
753-
local path = part.filename
754-
if vim.startswith(path, vim.fn.getcwd()) then
755-
path = path:sub(#vim.fn.getcwd() + 2)
756-
end
757-
local file_line = M.output:add_line(string.format('[%s](%s)', path, part.filename))
762+
local file_line = M._format_context_file(part.filename)
758763
if message_info.role == 'user' then
759764
-- when streaming, the file comes in as a separate event, connect it to user
760765
-- message

0 commit comments

Comments
 (0)