|
256 | 256 | function M:clear_chat() |
257 | 257 | local chat = self.containers and self.containers.chat |
258 | 258 | 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. |
259 | 277 | self._welcome_message_applied = true |
260 | 278 | self._force_welcome = false |
261 | 279 | vim.api.nvim_buf_set_lines(chat.bufnr, 0, -1, false, {}) |
@@ -343,11 +361,12 @@ function M:_create_containers() |
343 | 361 | and self.containers.chat.bufnr |
344 | 362 | or nil |
345 | 363 |
|
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 |
351 | 370 | pcall(old_chat.unmount, old_chat) |
352 | 371 | end |
353 | 372 |
|
|
0 commit comments