Skip to content

Commit edbaa32

Browse files
committed
fix: cleanup nui Split autocmds on toggle and encapsulate EcaChatClear logic
1 parent d22bab0 commit edbaa32

2 files changed

Lines changed: 18 additions & 6 deletions

File tree

lua/eca/commands.lua

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -550,12 +550,7 @@ function M.setup()
550550
vim.api.nvim_create_user_command("EcaChatClear", function()
551551
local sidebar = require("eca").get()
552552
if sidebar then
553-
sidebar._welcome_message_applied = true
554-
sidebar._force_welcome = false
555-
local chat = sidebar.containers and sidebar.containers.chat
556-
if chat and chat.bufnr and vim.api.nvim_buf_is_valid(chat.bufnr) then
557-
vim.api.nvim_buf_set_lines(chat.bufnr, 0, -1, false, {})
558-
end
553+
sidebar:clear_chat()
559554
end
560555
end, {
561556
desc = "Clear ECA chat buffer",

lua/eca/sidebar.lua

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,15 @@ function M:reset()
253253
end
254254
end
255255

256+
function M:clear_chat()
257+
local chat = self.containers and self.containers.chat
258+
if chat and chat.bufnr and vim.api.nvim_buf_is_valid(chat.bufnr) then
259+
self._welcome_message_applied = true
260+
self._force_welcome = false
261+
vim.api.nvim_buf_set_lines(chat.bufnr, 0, -1, false, {})
262+
end
263+
end
264+
256265
function M:new_chat()
257266
self:reset()
258267
self._force_welcome = true
@@ -334,6 +343,14 @@ function M:_create_containers()
334343
and self.containers.chat.bufnr
335344
or nil
336345

346+
-- Clean up the old chat Split's autocmds before creating a new one.
347+
-- Detach the buffer first so that unmount() does not delete it.
348+
if existing_chat_bufnr then
349+
local old_chat = self.containers.chat
350+
old_chat.bufnr = nil
351+
pcall(old_chat.unmount, old_chat)
352+
end
353+
337354
-- Create and mount main chat container first
338355
self.containers.chat = Split({
339356
relative = "editor",

0 commit comments

Comments
 (0)