Skip to content

Commit 80a0994

Browse files
authored
feat(ui): add auto_fold option for chat messages (#1354)
Introduce the `auto_fold` config option to automatically fold non-assistant messages in the chat window and unfold assistant messages. This improves readability and navigation in long chat sessions. Closes #1300 Signed-off-by: Tomas Slusny <[email protected]>
1 parent 3496a48 commit 80a0994

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

lua/CopilotChat/config.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
---@field highlight_headers boolean?
3232
---@field auto_follow_cursor boolean?
3333
---@field auto_insert_mode boolean?
34+
---@field auto_fold boolean?
3435
---@field insert_at_end boolean?
3536
---@field clear_chat_on_new_prompt boolean?
3637

@@ -90,6 +91,7 @@ return {
9091
highlight_headers = true, -- Highlight headers in chat
9192
auto_follow_cursor = true, -- Auto-follow cursor in chat
9293
auto_insert_mode = false, -- Automatically enter insert mode when opening window and on new prompt
94+
auto_fold = false, -- Automatically non-assistant messages in chat
9395
insert_at_end = false, -- Move cursor to end of buffer when inserting text
9496
clear_chat_on_new_prompt = false, -- Clears chat on every new prompt
9597

lua/CopilotChat/ui/chat.lua

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,18 @@ function Chat:add_message(message, replace)
450450
or current_message.role ~= message.role
451451
or (message.id and current_message.id ~= message.id)
452452

453+
if
454+
self.config.auto_fold
455+
and current_message
456+
and current_message.role ~= constants.ROLE.ASSISTANT
457+
and message.role ~= constants.ROLE.USER
458+
and self:visible()
459+
then
460+
vim.api.nvim_win_call(self.winnr, function()
461+
vim.cmd('normal! zc')
462+
end)
463+
end
464+
453465
if is_new then
454466
-- Add appropriate header based on role and generate a new ID if not provided
455467
message.id = message.id or utils.uuid()
@@ -489,6 +501,12 @@ function Chat:add_message(message, replace)
489501
current_message.content = current_message.content .. message.content
490502
self:append(message.content)
491503
end
504+
505+
if self.config.auto_fold and message.role == constants.ROLE.ASSISTANT and self:visible() then
506+
vim.api.nvim_win_call(self.winnr, function()
507+
vim.cmd('normal! zo')
508+
end)
509+
end
492510
end
493511

494512
--- Remove a message from the chat window by role.

0 commit comments

Comments
 (0)