From 8c7fbf96d990aec964a8d7480f944c7f465780ae Mon Sep 17 00:00:00 2001 From: Tomas Slusny Date: Sat, 23 Aug 2025 23:55:15 +0200 Subject: [PATCH] feat(ui): add auto_fold option for chat messages 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 --- lua/CopilotChat/config.lua | 2 ++ lua/CopilotChat/ui/chat.lua | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/lua/CopilotChat/config.lua b/lua/CopilotChat/config.lua index a3fce3a9..c9a86dbb 100644 --- a/lua/CopilotChat/config.lua +++ b/lua/CopilotChat/config.lua @@ -31,6 +31,7 @@ ---@field highlight_headers boolean? ---@field auto_follow_cursor boolean? ---@field auto_insert_mode boolean? +---@field auto_fold boolean? ---@field insert_at_end boolean? ---@field clear_chat_on_new_prompt boolean? @@ -90,6 +91,7 @@ return { highlight_headers = true, -- Highlight headers in chat auto_follow_cursor = true, -- Auto-follow cursor in chat auto_insert_mode = false, -- Automatically enter insert mode when opening window and on new prompt + auto_fold = false, -- Automatically non-assistant messages in chat insert_at_end = false, -- Move cursor to end of buffer when inserting text clear_chat_on_new_prompt = false, -- Clears chat on every new prompt diff --git a/lua/CopilotChat/ui/chat.lua b/lua/CopilotChat/ui/chat.lua index e7f0c75c..25d875d5 100644 --- a/lua/CopilotChat/ui/chat.lua +++ b/lua/CopilotChat/ui/chat.lua @@ -450,6 +450,18 @@ function Chat:add_message(message, replace) or current_message.role ~= message.role or (message.id and current_message.id ~= message.id) + if + self.config.auto_fold + and current_message + and current_message.role ~= constants.ROLE.ASSISTANT + and message.role ~= constants.ROLE.USER + and self:visible() + then + vim.api.nvim_win_call(self.winnr, function() + vim.cmd('normal! zc') + end) + end + if is_new then -- Add appropriate header based on role and generate a new ID if not provided message.id = message.id or utils.uuid() @@ -489,6 +501,12 @@ function Chat:add_message(message, replace) current_message.content = current_message.content .. message.content self:append(message.content) end + + if self.config.auto_fold and message.role == constants.ROLE.ASSISTANT and self:visible() then + vim.api.nvim_win_call(self.winnr, function() + vim.cmd('normal! zo') + end) + end end --- Remove a message from the chat window by role.