Skip to content

Commit 7a6d2fc

Browse files
committed
feat(suggestion): update suggestion uuid tracking
1 parent c4f2950 commit 7a6d2fc

File tree

1 file changed

+23
-11
lines changed

1 file changed

+23
-11
lines changed

lua/copilot/suggestion.lua

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ local copilot = {
1313
ns_id = vim.api.nvim_create_namespace("copilot.suggestion"),
1414
extmark_id = 1,
1515

16-
uuid = nil,
1716
_copilot_timer = nil,
1817
_copilot = {
1918
first = nil,
@@ -143,12 +142,14 @@ local function stop_timer()
143142
end
144143
end
145144

146-
local function reject_current()
147-
if copilot.uuid then
145+
---@param bufnr integer
146+
local function reject(bufnr)
147+
local uuid = vim.fn.getbufvar(bufnr, "_copilot_uuid", "")
148+
if uuid ~= "" then
148149
with_client(function(client)
149-
api.notify_rejected(client, { uuids = { copilot.uuid } }, function() end)
150+
api.notify_rejected(client, { uuids = { uuid } }, function() end)
150151
end)
151-
copilot.uuid = nil
152+
vim.api.nvim_buf_set_var(bufnr, "_copilot_uuid", "")
152153
end
153154
end
154155

@@ -249,9 +250,9 @@ local function update_preview()
249250

250251
vim.api.nvim_buf_set_extmark(0, copilot.ns_id, vim.fn.line(".") - 1, cursor_col - 1, extmark)
251252

252-
if suggestion.uuid ~= copilot.uuid then
253-
reject_current()
254-
copilot.uuid = suggestion.uuid
253+
if suggestion.uuid ~= vim.fn.getbufvar(0, "_copilot_uuid", "") then
254+
reject(0)
255+
vim.api.nvim_buf_set_var(0, "_copilot_uuid", suggestion.uuid)
255256
with_client(function(client)
256257
api.notify_shown(client, { uuid = suggestion.uuid }, function() end)
257258
end)
@@ -414,6 +415,7 @@ function mod.accept(modifier)
414415
cancel_inflight_requests()
415416
reset_state()
416417

418+
vim.api.nvim_buf_set_var(0, "_copilot_uuid", "")
417419
with_client(function(client)
418420
if modifier then
419421
-- do not notify_accepted for partial accept.
@@ -423,7 +425,6 @@ function mod.accept(modifier)
423425

424426
api.notify_accepted(client, { uuid = suggestion.uuid }, function() end)
425427
end)
426-
copilot.uuid = nil
427428
clear_preview()
428429

429430
if type(modifier) == "function" then
@@ -476,7 +477,7 @@ function mod.accept_line()
476477
end
477478

478479
function mod.dismiss()
479-
reject_current()
480+
reject(0)
480481
clear()
481482
update_preview()
482483
end
@@ -522,8 +523,13 @@ local function on_complete_changed()
522523
clear()
523524
end
524525

526+
---@param info { buf: integer }
527+
local function on_buf_unload(info)
528+
reject(info.buf)
529+
end
530+
525531
local function on_vim_leave_pre()
526-
reject_current()
532+
reject(0)
527533
end
528534

529535
local function create_autocmds()
@@ -565,6 +571,12 @@ local function create_autocmds()
565571
desc = "[copilot] (suggestion) complete changed",
566572
})
567573

574+
vim.api.nvim_create_autocmd("BufUnload", {
575+
group = copilot.augroup,
576+
callback = on_buf_unload,
577+
desc = "[copilot] (suggestion) buf unload",
578+
})
579+
568580
vim.api.nvim_create_autocmd("VimLeavePre", {
569581
group = copilot.augroup,
570582
callback = on_vim_leave_pre,

0 commit comments

Comments
 (0)