Skip to content

Commit e9e426d

Browse files
authored
refactor(chat): streamline message parsing logic (#1414)
Move message parsing to get_messages, removing redundant parse calls from get_block, get_message, add_message, and remove_message. This simplifies the code and ensures messages are always parsed when retrieved, reducing unnecessary parsing and improving performance. Signed-off-by: Tomas Slusny <slusnucky@gmail.com>
1 parent c15f65e commit e9e426d

1 file changed

Lines changed: 2 additions & 8 deletions

File tree

lua/CopilotChat/ui/chat.lua

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,6 @@ end
187187
---@param cursor boolean? If true, returns the block closest to the cursor position
188188
---@return CopilotChat.ui.chat.Block?
189189
function Chat:get_block(role, cursor)
190-
self:parse()
191190
local messages = self:get_messages()
192191

193192
if cursor then
@@ -228,6 +227,7 @@ end
228227
--- Get list of all chat messages
229228
---@return table<CopilotChat.ui.chat.Message>
230229
function Chat:get_messages()
230+
self:parse()
231231
return self.messages:values()
232232
end
233233

@@ -236,7 +236,6 @@ end
236236
---@param cursor boolean? If true, returns the message closest to the cursor position
237237
---@return CopilotChat.ui.chat.Message?
238238
function Chat:get_message(role, cursor)
239-
self:parse()
240239
local messages = self:get_messages()
241240

242241
if cursor then
@@ -499,10 +498,7 @@ end
499498
---@param message CopilotChat.ui.chat.Message
500499
---@param replace boolean? If true, replaces the last message if it has same role
501500
function Chat:add_message(message, replace)
502-
self:parse()
503-
504-
local messages = self:get_messages()
505-
local current_message = messages[#messages]
501+
local current_message = self:get_message()
506502
local is_new = not current_message
507503
or current_message.role ~= message.role
508504
or (message.id and current_message.id ~= message.id)
@@ -550,8 +546,6 @@ end
550546
---@param role string? If specified, only considers sections of the given role
551547
---@param cursor boolean? If true, removes the message closest to the cursor position
552548
function Chat:remove_message(role, cursor)
553-
self:parse()
554-
555549
local message = self:get_message(role, cursor)
556550
if not message then
557551
return

0 commit comments

Comments
 (0)