Skip to content

Commit fae892d

Browse files
committed
fix(api): handle display_routes + rendering
Should correctly handle not loading a session when using a display_route and then doing a refresh when we stop using one
1 parent 6a375e7 commit fae892d

File tree

4 files changed

+30
-28
lines changed

4 files changed

+30
-28
lines changed

lua/opencode/api.lua

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ function M.close()
3434
if state.display_route then
3535
state.display_route = nil
3636
ui.clear_output()
37-
ui.scroll_to_bottom()
37+
-- need to trigger a re-render here to re-display the session
38+
ui.render_output()
3839
return
3940
end
4041

@@ -266,6 +267,13 @@ function M.prev_message()
266267
end
267268

268269
function M.submit_input_prompt()
270+
if state.display_route then
271+
-- we're displaying /help or something similar, need to clear that and refresh
272+
-- the session data before sending the command
273+
state.display_route = nil
274+
ui.render_output()
275+
end
276+
269277
input_window.handle_submit()
270278
end
271279

lua/opencode/core.lua

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,12 @@ function M.open(opts)
5858
if not state.active_session then
5959
state.active_session = session.get_last_workspace_session()
6060
else
61-
-- active session already set so no event will fire, need to force a refresh
62-
ui.render_output(true)
61+
if not state.display_route then
62+
-- We're not displaying /help or something like that but we have an active session
63+
-- so we need to do a full refresh. This mostly happens when opening the window
64+
-- after having closed it since we're not currently clearing the session on api.close()
65+
ui.render_output(false)
66+
end
6367
end
6468

6569
-- if (are_windows_closed or ui.is_output_empty()) and not state.display_route then

lua/opencode/ui/renderer.lua

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ function M._render_full_session_data(session_data)
109109
local revert_stats = M._calculate_revert_stats(state.messages, i, state.active_session.revert)
110110
local output = require('opencode.ui.output'):new()
111111
formatter._format_revert_message(output, revert_stats)
112-
M.write_output(output)
112+
M.render_output(output)
113113

114114
-- FIXME: how does reverting work? why is it breaking out of the message reading loop?
115115
break
@@ -188,9 +188,17 @@ function M._shift_parts_and_actions(from_line, delta)
188188
-- vim.notify('Shifting lines from: ' .. from_line .. ' by delta: ' .. delta .. ' examined: ' .. examined .. ' shifted: ' .. shifted)
189189
end
190190

191+
---Render lines as the entire output buffer
192+
---@param lines any
193+
function M.render_lines(lines)
194+
local output = require('opencode.ui.output'):new()
195+
output.lines = lines
196+
M.render_output(output)
197+
end
198+
191199
---Sets the entire output buffer based on output_data
192200
---@param output_data Output Output object from formatter
193-
function M.write_output(output_data)
201+
function M.render_output(output_data)
194202
if not output_window.mounted() then
195203
return
196204
end
@@ -267,17 +275,6 @@ function M._write_formatted_data(formatted_data)
267275
}
268276
end
269277

270-
---Write message header to buffer
271-
---@param message OpencodeMessage Message object
272-
---@param msg_idx integer Message index
273-
---@return {line_start: integer, line_end: integer}? Range where header was written
274-
function M._write_message_header(message, msg_idx)
275-
state.current_message = message
276-
local header_data = formatter.format_message_header_single(message, msg_idx)
277-
local line_range = M._write_formatted_data(header_data)
278-
return line_range
279-
end
280-
281278
---Insert new part at end of buffer
282279
---@param part_id string Part ID
283280
---@param formatted_data Output Formatted data as Output object

lua/opencode/ui/ui.lua

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -198,23 +198,16 @@ function M.clear_output()
198198
-- state.restore_points = {}
199199
end
200200

201-
function M.render_output(force)
202-
force = force or false
203-
-- vim.notify('render_output, force: ' .. vim.inspect(force) .. '\n' .. debug.traceback())
204-
-- output_renderer.render(state.windows, force)
205-
206-
-- FIXME: should look at all calls of render_output and see if they're needed.
207-
-- I suspect may of them can be removed and we can rely on state transitions
208-
-- to handle loading
201+
function M.render_output(_)
209202
renderer.render_full_session()
210203
end
211204

212205
function M.render_lines(lines)
213-
-- FIXME: don't use output_renderer here
214-
215206
M.clear_output()
216-
output_renderer.write_output(state.windows, lines)
217-
output_renderer.render_markdown()
207+
renderer.render_lines(lines)
208+
209+
-- FIXME: rehook up markdown at some point (user provided callback?)
210+
-- output_renderer.render_markdown()
218211
end
219212

220213
function M.select_session(sessions, cb)

0 commit comments

Comments
 (0)