Skip to content

Commit bb53413

Browse files
committed
chore(renderer): cleaning up types/diagnostics
1 parent 77045bd commit bb53413

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

lua/opencode/session.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ end
150150

151151
---Get messages for a session
152152
---@param session Session
153-
---@return Promise<OpencodeMessage[]?>
153+
---@return Promise<OpencodeMessage[]>
154154
function M.get_messages(session)
155155
local state = require('opencode.state')
156156
if not session then

lua/opencode/ui/renderer.lua

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ local M = {}
1010
M._subscriptions = {}
1111
M._prev_line_count = 0
1212
M._render_state = RenderState.new()
13-
M._last_part_formatted = { part_id = nil, formatted_data = nil }
13+
M._last_part_formatted = {
14+
part_id = nil,
15+
formatted_data = nil --[[@as Output|nil]],
16+
}
1417

1518
local trigger_on_data_rendered = require('opencode.util').debounce(function()
1619
local cb_type = type(config.ui.output.rendering.on_data_rendered)
@@ -99,6 +102,8 @@ function M.teardown()
99102
M.reset()
100103
end
101104

105+
---Fetch full session messages from server
106+
---@return Promise<OpencodeMessage[]> Promise resolving to list of OpencodeMessage
102107
local function fetch_session()
103108
local session = state.active_session
104109
if not session or not session or session == '' then
@@ -120,7 +125,7 @@ end
120125
function M._render_full_session_data(session_data)
121126
M.reset()
122127

123-
if not state.active_session then
128+
if not state.active_session or not state.messages then
124129
return
125130
end
126131

@@ -215,6 +220,10 @@ end
215220
---@param part_id? string Optional part ID to store actions
216221
---@return {line_start: integer, line_end: integer}? Range where data was written
217222
function M._write_formatted_data(formatted_data, part_id)
223+
if not state.windows or not state.windows.output_buf then
224+
return
225+
end
226+
218227
local buf = state.windows.output_buf
219228
local start_line = output_window.get_buf_line_count()
220229
local new_lines = formatted_data.lines
@@ -288,6 +297,7 @@ function M._replace_part_in_buffer(part_id, formatted_data)
288297
local write_start_line = cached.line_start
289298

290299
if can_optimize then
300+
---@cast old_formatted { formatted_data: { lines: string[] } }
291301
local old_lines = old_formatted.formatted_data.lines
292302
local first_diff_line = nil
293303

@@ -367,6 +377,17 @@ function M._remove_message_from_buffer(message_id)
367377
M._render_state:remove_message(message_id)
368378
end
369379

380+
---Adds a message (most likely just a header) to the buffer
381+
---@param message OpencodeMessage Message to add
382+
function M._add_message_to_buffer(message)
383+
local header_data = formatter.format_message_header(message)
384+
local range = M._write_formatted_data(header_data)
385+
386+
if range then
387+
M._render_state:set_message(message, range.line_start, range.line_end)
388+
end
389+
end
390+
370391
---Replace existing message header in buffer
371392
---@param message_id string Message ID
372393
---@param formatted_data Output Formatted header as Output object
@@ -442,12 +463,7 @@ function M.on_message_updated(message, revert_index)
442463
else
443464
table.insert(state.messages, msg)
444465

445-
local header_data = formatter.format_message_header(msg)
446-
local range = M._write_formatted_data(header_data)
447-
448-
if range then
449-
M._render_state:set_message(msg, range.line_start, range.line_end)
450-
end
466+
M._add_message_to_buffer(msg)
451467

452468
state.current_message = msg
453469
if message.info.role == 'user' then

0 commit comments

Comments
 (0)