Skip to content

Commit df17fda

Browse files
fix: handle chat or request stopped
1 parent c8b2d76 commit df17fda

2 files changed

Lines changed: 16 additions & 11 deletions

File tree

lua/codecompanion-spinner/spinner-manager.lua

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
local log = require("codecompanion-spinner.log")
12
local Spinner = require("codecompanion-spinner.spinner")
23

34
local M = {}
@@ -9,6 +10,7 @@ M.setup = function()
910
vim.api.nvim_create_autocmd("User", {
1011
pattern = "CodeCompanionChatCreated",
1112
callback = function(args)
13+
log.debug("CodeCompanionChatCreated")
1214
local chat_id = args.data.id
1315
assert(spinners[chat_id] == nil)
1416
active_spinner = Spinner:new(chat_id, args.buf)
@@ -19,6 +21,7 @@ M.setup = function()
1921
vim.api.nvim_create_autocmd("User", {
2022
pattern = "CodeCompanionChatClosed",
2123
callback = function(args)
24+
log.debug("CodeCompanionChatClosed")
2225
local chat_id = args.data.id
2326
if spinners[chat_id] then
2427
spinners[chat_id]:stop()
@@ -30,6 +33,8 @@ M.setup = function()
3033
vim.api.nvim_create_autocmd("User", {
3134
pattern = "CodeCompanionChatOpened",
3235
callback = function(args)
36+
log.debug("CodeCompanionChatOpened")
37+
3338
local spinner = spinners[args.data.id]
3439

3540
-- When a new chat is created, this event is triggered but no spinner is
@@ -47,6 +52,7 @@ M.setup = function()
4752
vim.api.nvim_create_autocmd("User", {
4853
pattern = "CodeCompanionChatHidden",
4954
callback = function(args)
55+
log.debug("CodeCompanionChatHidden")
5056
local spinner = spinners[args.data.id]
5157
spinner:disable()
5258
end,
@@ -55,6 +61,7 @@ M.setup = function()
5561
vim.api.nvim_create_autocmd("User", {
5662
pattern = "CodeCompanionRequestStarted",
5763
callback = function(args)
64+
log.debug("CodeCompanionRequestStarted")
5865
assert(active_spinner)
5966
active_spinner:start(args.data.id)
6067
end,
@@ -63,21 +70,18 @@ M.setup = function()
6370
vim.api.nvim_create_autocmd("User", {
6471
pattern = "CodeCompanionRequestFinished",
6572
callback = function(args)
73+
log.debug("CodeCompanionRequestFinished")
74+
75+
-- Search for the spinner is handling the request.
76+
-- Note: If the chat was stopped, the spinner was deleted.
77+
-- In that case, no spinner will be found.
6678
local request_id = args.data.id
6779
for _, spinner in pairs(spinners) do
6880
if spinner.request_id == request_id then
6981
spinner:stop()
70-
return
82+
break
7183
end
7284
end
73-
error("No spinner found for request ID: " .. request_id)
74-
end,
75-
})
76-
77-
vim.api.nvim_create_autocmd("User", {
78-
pattern = "CodeCompanionChatStopped",
79-
callback = function(args)
80-
spinners[args.data.id]:stop()
8185
end,
8286
})
8387
end

lua/codecompanion-spinner/spinner.lua

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ function M:_update_text()
4444
end
4545

4646
function M:_clear_text()
47-
vim.api.nvim_buf_clear_namespace(self.buffer, self.namespace_id, 0, -1)
47+
if vim.api.nvim_buf_is_valid(self.buffer) then -- if not closed already
48+
vim.api.nvim_buf_clear_namespace(self.buffer, self.namespace_id, 0, -1)
49+
end
4850
end
4951

5052
function M:_start_timer()
@@ -73,7 +75,6 @@ function M:start(request_id)
7375
end
7476

7577
function M:stop()
76-
log.debug("in stop of spinner", self.chat_id, "... chat_in_buffer:", self.chat_in_buffer)
7778
if self.chat_in_buffer then
7879
self:_clear_text()
7980
self:_stop_timer()

0 commit comments

Comments
 (0)