Skip to content

Commit 88c2056

Browse files
committed
feat(session_formatter): add selected text to format isolated part
The order of the context has been changed in order to have selection before the current file The seperator has been changed to '----' it was causing the user part to not be formatted by render-markdown
1 parent 33dca77 commit 88c2056

File tree

2 files changed

+28
-9
lines changed

2 files changed

+28
-9
lines changed

lua/opencode/context.lua

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,10 @@ function M.format_message(prompt, opts)
327327
end
328328
end
329329

330+
for _, sel in ipairs(context.selections or {}) do
331+
table.insert(parts, format_selection_part(sel))
332+
end
333+
330334
for _, agent in ipairs(context.mentioned_subagents or {}) do
331335
table.insert(parts, format_subagents_part(agent, prompt))
332336
end
@@ -335,10 +339,6 @@ function M.format_message(prompt, opts)
335339
table.insert(parts, format_file_part(context.current_file.path))
336340
end
337341

338-
for _, sel in ipairs(context.selections or {}) do
339-
table.insert(parts, format_selection_part(sel))
340-
end
341-
342342
if context.linter_errors then
343343
table.insert(parts, format_diagnostics_part(context.linter_errors))
344344
end
@@ -350,10 +350,10 @@ function M.format_message(prompt, opts)
350350
return parts
351351
end
352352

353-
---@param part OpencodeMessagePart
353+
---@param text string
354354
---@param context_type string|nil
355-
local function decode_json_context(part, context_type)
356-
local ok, result = pcall(vim.json.decode, part.text)
355+
function M.decode_json_context(text, context_type)
356+
local ok, result = pcall(vim.json.decode, text)
357357
if not ok or (context_type and result.context_type ~= context_type) then
358358
return nil
359359
end
@@ -371,7 +371,7 @@ function M.extract_from_opencode_message(message)
371371
ctx.prompt = ctx.prompt or part.text or ''
372372
end,
373373
text_context = function(part)
374-
local json = decode_json_context(part, 'selection')
374+
local json = M.decode_json_context(part.text, 'selection')
375375
ctx.selected_text = json and json.content or ctx.selected_text
376376
end,
377377
file = function(part)

lua/opencode/ui/session_formatter.lua

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ local M = {
1414
}
1515

1616
M.separator = {
17-
'---',
17+
'----',
1818
'',
1919
}
2020

@@ -418,6 +418,7 @@ function M._format_user_message(text, message)
418418
M.output:add_lines(vim.split(context.prompt, '\n'))
419419

420420
if context.selected_text then
421+
M.output:add_empty_line()
421422
M.output:add_lines(vim.split(context.selected_text, '\n'))
422423
end
423424

@@ -431,6 +432,22 @@ function M._format_user_message(text, message)
431432
M._add_vertical_border(start_line, end_line, 'OpencodeMessageRoleUser', -3)
432433
end
433434

435+
---@param part MessagePart
436+
function M._format_selection_context(part)
437+
local json = context_module.decode_json_context(part.text, 'selection')
438+
if not json then
439+
return
440+
end
441+
local start_line = M.output:get_line_count()
442+
M.output:add_empty_line()
443+
M.output:add_lines(vim.split(json.content, '\n'))
444+
M.output:add_empty_line()
445+
446+
local end_line = M.output:get_line_count()
447+
448+
M._add_vertical_border(start_line, end_line, 'OpencodeMessageRoleUser', -3)
449+
end
450+
434451
---Format and display the file path in the context
435452
---@param path string|nil File path
436453
function M._format_context_file(path)
@@ -747,6 +764,8 @@ function M.format_part_isolated(part, message_info)
747764
if message_info.role == 'user' and part.synthetic ~= true then
748765
M._format_user_message(vim.trim(part.text), message_info.message)
749766
content_added = true
767+
elseif part.synthetic == true and message_info.role == 'user' then
768+
M._format_selection_context(part)
750769
elseif message_info.role == 'assistant' then
751770
M._format_assistant_message(vim.trim(part.text))
752771
content_added = true

0 commit comments

Comments
 (0)