Skip to content

Commit 06edce8

Browse files
committed
feat(suggestion): update rejection handling
1 parent 2335272 commit 06edce8

1 file changed

Lines changed: 17 additions & 12 deletions

File tree

lua/copilot/suggestion.lua

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ local util = require("copilot.util")
66

77
local mod = {}
88

9-
---@alias copilot_suggestion_context { first?: integer, cycling?: integer, cycling_callbacks?: (fun(ctx: copilot_suggestion_context):nil)[], params?: table, suggestions?: copilot_get_completions_data_completion[], choice?: integer }
9+
---@alias copilot_suggestion_context { first?: integer, cycling?: integer, cycling_callbacks?: (fun(ctx: copilot_suggestion_context):nil)[], params?: table, suggestions?: copilot_get_completions_data_completion[], choice?: integer, shown_choices?: table<string, true> }
1010

1111
local copilot = {
1212
setup_done = false,
@@ -60,6 +60,7 @@ local function reset_ctx(ctx)
6060
ctx.params = nil
6161
ctx.suggestions = nil
6262
ctx.choice = nil
63+
ctx.shown_choices = nil
6364
end
6465

6566
local function set_keymap(keymap)
@@ -147,14 +148,19 @@ local function stop_timer()
147148
end
148149
end
149150

150-
---@param bufnr integer
151+
---@param bufnr? integer
151152
local function reject(bufnr)
152-
local uuid = vim.fn.getbufvar(bufnr, "_copilot_uuid", "")
153-
if uuid ~= "" then
153+
local ctx = get_ctx(bufnr)
154+
if not ctx.shown_choices then
155+
return
156+
end
157+
158+
local uuids = vim.tbl_keys(ctx.shown_choices)
159+
if #uuids > 0 then
154160
with_client(function(client)
155-
api.notify_rejected(client, { uuids = { uuid } }, function() end)
161+
api.notify_rejected(client, { uuids = uuids }, function() end)
156162
end)
157-
vim.api.nvim_buf_set_var(bufnr, "_copilot_uuid", "")
163+
ctx.shown_choices = {}
158164
end
159165
end
160166

@@ -264,9 +270,8 @@ local function update_preview(ctx)
264270

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

267-
if suggestion.uuid ~= vim.fn.getbufvar(0, "_copilot_uuid", "") then
268-
reject(0)
269-
vim.api.nvim_buf_set_var(0, "_copilot_uuid", suggestion.uuid)
273+
if not ctx.shown_choices[suggestion.uuid] then
274+
ctx.shown_choices[suggestion.uuid] = true
270275
with_client(function(client)
271276
api.notify_shown(client, { uuid = suggestion.uuid }, function() end)
272277
end)
@@ -306,6 +311,7 @@ local function handle_trigger_request(err, data)
306311
local ctx = get_ctx()
307312
ctx.suggestions = data and data.completions or {}
308313
ctx.choice = 1
314+
ctx.shown_choices = {}
309315
update_preview()
310316
end
311317

@@ -443,7 +449,6 @@ function mod.accept(modifier)
443449
cancel_inflight_requests(ctx)
444450
reset_ctx(ctx)
445451

446-
vim.api.nvim_buf_set_var(0, "_copilot_uuid", "")
447452
with_client(function(client)
448453
if modifier then
449454
-- do not notify_accepted for partial accept.
@@ -506,7 +511,7 @@ end
506511

507512
function mod.dismiss()
508513
local ctx = get_ctx()
509-
reject(0)
514+
reject()
510515
clear(ctx)
511516
update_preview(ctx)
512517
end
@@ -560,7 +565,7 @@ local function on_buf_unload(info)
560565
end
561566

562567
local function on_vim_leave_pre()
563-
reject(0)
568+
reject()
564569
end
565570

566571
local function create_autocmds()

0 commit comments

Comments
 (0)