From 2e95d6ae8360722e4f7135d80239449669ae555a Mon Sep 17 00:00:00 2001 From: Tomas Slusny Date: Wed, 17 Sep 2025 18:50:39 +0200 Subject: [PATCH] fix(chat): do not create multiple chat isntances this breaks notification listeners Signed-off-by: Tomas Slusny --- lua/CopilotChat/init.lua | 58 ++++++++++++++++++------------------- lua/CopilotChat/ui/chat.lua | 2 +- 2 files changed, 30 insertions(+), 30 deletions(-) diff --git a/lua/CopilotChat/init.lua b/lua/CopilotChat/init.lua index 98c1c1e6..268405bd 100644 --- a/lua/CopilotChat/init.lua +++ b/lua/CopilotChat/init.lua @@ -1100,41 +1100,41 @@ function M.setup(config) if M.chat then M.chat:close(state.source.bufnr) M.chat:delete() - end - - M.chat = require('CopilotChat.ui.chat')(M.config, function(bufnr) - for name, _ in pairs(M.config.mappings) do - map_key(name, bufnr) - end - - require('CopilotChat.completion').enable(bufnr, M.config.chat_autocomplete) + else + M.chat = require('CopilotChat.ui.chat')(M.config, function(bufnr) + for name, _ in pairs(M.config.mappings) do + map_key(name, bufnr) + end - vim.api.nvim_create_autocmd({ 'BufEnter', 'BufLeave' }, { - buffer = bufnr, - callback = function(ev) - if ev.event == 'BufEnter' then - update_source() - end + require('CopilotChat.completion').enable(bufnr, M.config.chat_autocomplete) - vim.schedule(function() - select.highlight(state.source.bufnr, not (M.config.highlight_selection and M.chat:focused())) - end) - end, - }) - - if M.config.insert_at_end then - vim.api.nvim_create_autocmd({ 'InsertEnter' }, { + vim.api.nvim_create_autocmd({ 'BufEnter', 'BufLeave' }, { buffer = bufnr, - callback = function() - vim.cmd('normal! 0') - vim.cmd('normal! G$') - vim.v.char = 'x' + callback = function(ev) + if ev.event == 'BufEnter' then + update_source() + end + + vim.schedule(function() + select.highlight(state.source.bufnr, not (M.config.highlight_selection and M.chat:focused())) + end) end, }) - end - finish(true) - end) + if M.config.insert_at_end then + vim.api.nvim_create_autocmd({ 'InsertEnter' }, { + buffer = bufnr, + callback = function() + vim.cmd('normal! 0') + vim.cmd('normal! G$') + vim.v.char = 'x' + end, + }) + end + + finish(true) + end) + end for name, prompt in pairs(list_prompts()) do if prompt.prompt then diff --git a/lua/CopilotChat/ui/chat.lua b/lua/CopilotChat/ui/chat.lua index 267b8aed..8cd08c11 100644 --- a/lua/CopilotChat/ui/chat.lua +++ b/lua/CopilotChat/ui/chat.lua @@ -164,7 +164,7 @@ local Chat = class(function(self, config, on_buf_create) if not msg or msg == '' then self.chat_overlay:restore(self.winnr, self.bufnr) else - self:overlay({ text = msg }) + self.chat_overlay:show(msg, self.winnr) end end) end, Overlay)