Skip to content

Commit 48e3e4d

Browse files
committed
fix: reset full chat state and always unmount old Split on clear
1 parent edbaa32 commit 48e3e4d

2 files changed

Lines changed: 25 additions & 6 deletions

File tree

lua/eca/sidebar.lua

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,24 @@ end
256256
function M:clear_chat()
257257
local chat = self.containers and self.containers.chat
258258
if chat and chat.bufnr and vim.api.nvim_buf_is_valid(chat.bufnr) then
259+
-- Reset chat content state to prevent stale line numbers / extmark IDs.
260+
self._tool_calls = {}
261+
self._reasons = {}
262+
self._current_tool_call = nil
263+
self._is_tool_call_streaming = false
264+
self._is_streaming = false
265+
self._current_response_buffer = ""
266+
self._stream_visible_buffer = ""
267+
if self._stream_queue then
268+
self._stream_queue:clear()
269+
end
270+
-- Reset chat extmark refs (marks are invalidated when the buffer is wiped).
271+
if self.extmarks then
272+
self.extmarks.assistant = nil
273+
self.extmarks.tool_header = nil
274+
self.extmarks.tool_diff_label = nil
275+
end
276+
-- Prevent state/updated events from repopulating the cleared buffer.
259277
self._welcome_message_applied = true
260278
self._force_welcome = false
261279
vim.api.nvim_buf_set_lines(chat.bufnr, 0, -1, false, {})
@@ -343,11 +361,12 @@ function M:_create_containers()
343361
and self.containers.chat.bufnr
344362
or nil
345363

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
364+
-- Always unmount the old Split to clean up its autocmds.
365+
local old_chat = self.containers.chat
366+
if old_chat then
367+
if existing_chat_bufnr then
368+
old_chat.bufnr = nil -- detach so unmount() doesn't delete the preserved buffer
369+
end
351370
pcall(old_chat.unmount, old_chat)
352371
end
353372

tests/test_chat_clear.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ T["EcaChatClear"]["is a no-op when sidebar is closed and buffer was destroyed (p
159159
eq(ok, true)
160160
end
161161

162-
T["EcaChatClear"]["resets _welcome_message_applied and _force_welcome"] = function()
162+
T["EcaChatClear"]["marks welcome as applied and clears force_welcome after clear"] = function()
163163
setup_env(false)
164164
flush(200)
165165

0 commit comments

Comments
 (0)