From 2c2256bd420d2f185e60598828af9adec55a47af Mon Sep 17 00:00:00 2001 From: Tomas Slusny Date: Fri, 22 Nov 2024 00:41:43 +0100 Subject: [PATCH 1/2] fix: improve chat prompt clearing and cwd detection Clear chat prompt logic has been moved to dedicated method in Chat class to improve code reusability and maintainability. Window local cwd detection has been simplified and now uses VimEnter/WinEnter events instead of DirChanged for more reliable current working directory tracking. Signed-off-by: Tomas Slusny --- lua/CopilotChat/chat.lua | 18 ++++++++++++++++++ lua/CopilotChat/init.lua | 19 +++---------------- lua/CopilotChat/utils.lua | 4 ++-- 3 files changed, 23 insertions(+), 18 deletions(-) diff --git a/lua/CopilotChat/chat.lua b/lua/CopilotChat/chat.lua index 51a35cfc..d2eb5001 100644 --- a/lua/CopilotChat/chat.lua +++ b/lua/CopilotChat/chat.lua @@ -4,6 +4,7 @@ ---@field sections table ---@field get_closest_section fun(self: CopilotChat.Chat): table|nil ---@field get_closest_block fun(self: CopilotChat.Chat): table|nil +---@field clear_prompt fun(self: CopilotChat.Chat) ---@field valid fun(self: CopilotChat.Chat) ---@field visible fun(self: CopilotChat.Chat) ---@field active fun(self: CopilotChat.Chat) @@ -278,6 +279,23 @@ function Chat:get_closest_block() } end +function Chat:clear_prompt() + if not self:visible() then + return + end + + self:render() + + local section = self.sections[#self.sections] + if not section or section.answer then + return + end + + vim.bo[self.bufnr].modifiable = true + vim.api.nvim_buf_set_lines(self.bufnr, section.start_line - 1, section.end_line, false, {}) + vim.bo[self.bufnr].modifiable = false +end + function Chat:active() return vim.api.nvim_get_current_win() == self.winnr end diff --git a/lua/CopilotChat/init.lua b/lua/CopilotChat/init.lua index 0758f897..fa2a39ac 100644 --- a/lua/CopilotChat/init.lua +++ b/lua/CopilotChat/init.lua @@ -667,19 +667,8 @@ function M.ask(prompt, config) finish(config, nil, true) end - local section = state.chat:get_closest_section() - if not section or section.answer then - return - end - state.last_prompt = prompt - vim.api.nvim_buf_set_lines( - state.chat.bufnr, - section.start_line - 1, - section.end_line, - false, - {} - ) + state.chat:clear_prompt() state.chat:append('\n\n' .. prompt) state.chat:append('\n\n' .. config.answer_header .. config.separator .. '\n\n') end @@ -1327,12 +1316,10 @@ function M.setup(config) -- I dont think there is a better way to do this that functions -- with "rooter" plugins, LSP and stuff as vim.fn.getcwd() when -- i pass window number inside doesnt work - vim.api.nvim_create_autocmd('DirChanged', { + vim.api.nvim_create_autocmd({ 'VimEnter', 'WinEnter' }, { group = augroup, callback = function() - if vim.v.event and vim.v.event.cwd then - vim.api.nvim_win_set_var(0, 'cchat_cwd', vim.v.event.cwd) - end + vim.api.nvim_win_set_var(0, 'cchat_cwd', vim.fn.getcwd()) end, }) end diff --git a/lua/CopilotChat/utils.lua b/lua/CopilotChat/utils.lua index b1f2d735..510013a9 100644 --- a/lua/CopilotChat/utils.lua +++ b/lua/CopilotChat/utils.lua @@ -176,8 +176,8 @@ function M.win_cwd(winnr) return '.' end - local dir = vim.api.nvim_win_get_var(winnr, 'cchat_cwd') - if not dir or dir == '' then + local ok, dir = pcall(vim.api.nvim_win_get_var, winnr, 'cchat_cwd') + if not ok or not dir or dir == '' then return '.' end From 903a66ab13a81da1504bc113efa9c5758341b719 Mon Sep 17 00:00:00 2001 From: Tomas Janousek Date: Thu, 21 Nov 2024 23:41:01 +0000 Subject: [PATCH 2/2] refactor: simplify cwd detection Turns out nvim_win_get_var was a drama queen, throwing exceptions whenever it couldn't find its precious variable - like a toddler having a meltdown when they can't find their favorite toy. Switched to vim.w, the more chill cousin who just returns nil and goes about their day when something's missing. Now our code won't have an existential crisis just because cchat_cwd wasn't set for one window. No more exception-al behavior, just peaceful, quiet coding. --- lua/CopilotChat/init.lua | 2 +- lua/CopilotChat/utils.lua | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lua/CopilotChat/init.lua b/lua/CopilotChat/init.lua index fa2a39ac..39ae19cb 100644 --- a/lua/CopilotChat/init.lua +++ b/lua/CopilotChat/init.lua @@ -1319,7 +1319,7 @@ function M.setup(config) vim.api.nvim_create_autocmd({ 'VimEnter', 'WinEnter' }, { group = augroup, callback = function() - vim.api.nvim_win_set_var(0, 'cchat_cwd', vim.fn.getcwd()) + vim.w.cchat_cwd = vim.fn.getcwd() end, }) end diff --git a/lua/CopilotChat/utils.lua b/lua/CopilotChat/utils.lua index 510013a9..db22887b 100644 --- a/lua/CopilotChat/utils.lua +++ b/lua/CopilotChat/utils.lua @@ -176,8 +176,8 @@ function M.win_cwd(winnr) return '.' end - local ok, dir = pcall(vim.api.nvim_win_get_var, winnr, 'cchat_cwd') - if not ok or not dir or dir == '' then + local dir = vim.w[winnr].cchat_cwd + if not dir or dir == '' then return '.' end