Skip to content

Commit 234eec3

Browse files
committed
refactor(ui): cleaner methods
1 parent 54af697 commit 234eec3

3 files changed

Lines changed: 23 additions & 20 deletions

File tree

lua/codecompanion/interactions/chat/ui/builder.lua

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,8 @@ local function create_state(out, base_state)
136136
end
137137

138138
---Add message using centralized state
139-
---@param data table
140-
---@param opts table
139+
---@param data { content?: string, role?: string, reasoning?: { content: string } }
140+
---@param opts? { type?: string, force_role?: boolean, insert_at?: number, status?: string, _icon_info?: table, virt_text_pos?: string, fold_info?: table, state?: table }
141141
---@return number,number|nil
142142
function Builder:add_message(data, opts)
143143
opts = opts or {}
@@ -197,7 +197,9 @@ function Builder:add_message(data, opts)
197197

198198
local insert_line, icon_id
199199
if not vim.tbl_isempty(lines) then
200-
insert_line, icon_id = self:_write_to_buffer(lines, opts, fold_info, state)
200+
opts.fold_info = fold_info
201+
opts.state = state
202+
insert_line, icon_id = self:_write_to_buffer(lines, opts)
201203
end
202204

203205
if current_type then
@@ -225,24 +227,24 @@ function Builder:add_message(data, opts)
225227
end
226228

227229
---Determine if we should start a new block under the header
228-
---@param opts table
230+
---@param opts { type?: string }
229231
---@param state table
230232
---@return boolean
231233
function Builder:_should_start_new_block(opts, state)
232234
return opts.type ~= nil and opts.type ~= state.last_type
233235
end
234236

235237
---Check if we need to add a header to the chat buffer
236-
---@param data table
237-
---@param opts table
238+
---@param data { role?: string }
239+
---@param opts { force_role?: boolean }
238240
---@param state table
239241
---@return boolean
240242
function Builder:_should_add_header(data, opts, state)
241243
return (data.role and data.role ~= state.last_role) or (opts and opts.force_role)
242244
end
243245

244246
---Add appropriate spacing before header
245-
---@param lines table
247+
---@param lines string[]
246248
---@return nil
247249
function Builder:_add_header_spacing(lines)
248250
table.insert(lines, "")
@@ -263,12 +265,13 @@ function Builder:_get_formatter(data, opts)
263265
end
264266

265267
---Write lines to buffer with all the buffer management
266-
---@param lines table
267-
---@param opts table
268-
---@param fold_info table|nil
269-
---@param state table
270-
---@return number The line number where the content was written and the extmark id of any icon applied
271-
function Builder:_write_to_buffer(lines, opts, fold_info, state)
268+
---@param lines string[]
269+
---@param opts { insert_at?: number, _icon_info?: table, virt_text_pos?: string, fold_info?: table, state?: table }
270+
---@return number, number|nil
271+
function Builder:_write_to_buffer(lines, opts)
272+
local state = opts.state
273+
local fold_info = opts.fold_info
274+
272275
self.chat.ui:unlock_buf()
273276
local last_line, last_column, line_count = self.chat.ui:last()
274277

@@ -345,7 +348,7 @@ end
345348
---Update a specific line in the chat buffer
346349
---@param line_number number The line number to update (1-based)
347350
---@param content string The new content for the line
348-
---@param opts? table Optional parameters
351+
---@param opts? { status?: string, icon_id?: number, priority?: number, virt_text_pos?: string }
349352
---@return boolean success Whether the update was successful
350353
---@return number|nil icon_id The new icon extmark ID, if an icon was placed
351354
function Builder:update_line(line_number, content, opts)

lua/codecompanion/interactions/chat/ui/formatters/base.lua

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,25 @@ end
2424

2525
---Check if this formatter can handle the given data/opts
2626
---@param message table
27-
---@param opts table
28-
---@param tags table
27+
---@param opts { type?: string, status?: string }
28+
---@param tags table<string, string>
2929
---@return boolean
3030
function BaseFormatter:can_handle(message, opts, tags)
3131
error("Must implement can_handle method")
3232
end
3333

3434
---Get the message type for this formatter
35-
---@param opts table
35+
---@param opts { type?: string }
3636
---@return string
3737
function BaseFormatter:get_type(opts)
3838
error("Must implement get_type method")
3939
end
4040

4141
---Format the content into lines
4242
---@param message table
43-
---@param opts table
43+
---@param opts { type?: string, status?: string, _icon_info?: table }
4444
---@param state table
45-
---@return table lines, table? fold_info
45+
---@return string[] lines, table? fold_info
4646
function BaseFormatter:format(message, opts, state)
4747
error("Must implement format method")
4848
end

lua/codecompanion/interactions/chat/ui/icons.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ local CONSTANTS = {
1212
---@param bufnr number
1313
---@param line number 0-based line number
1414
---@param status string The tool status (in_progress, completed, failed)
15-
---@param opts table|nil Additional options
15+
---@param opts? { priority?: number, virt_text_pos?: string }
1616
---@return number|nil The extmark id or nil if status is invalid
1717
function Icons.apply(bufnr, line, status, opts)
1818
opts = vim.tbl_deep_extend("force", {

0 commit comments

Comments
 (0)