Skip to content

Commit 49860bd

Browse files
committed
feat(suggestion): notify accepted length
1 parent 16de163 commit 49860bd

File tree

3 files changed

+21
-12
lines changed

3 files changed

+21
-12
lines changed

lua/copilot/api.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ function mod.get_version(client, callback)
9797
return mod.request(client, "getVersion", {}, callback)
9898
end
9999

100-
---@alias copilot_notify_accepted_params { uuid: string }
100+
---@alias copilot_notify_accepted_params { uuid: string, acceptedLength?: integer }
101101

102102
---@param params copilot_notify_accepted_params
103103
function mod.notify_accepted(client, params, callback)

lua/copilot/suggestion.lua

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -453,21 +453,20 @@ function mod.accept(modifier)
453453
cancel_inflight_requests(ctx)
454454
reset_ctx(ctx)
455455

456-
with_client(function(client)
457-
if modifier then
458-
-- do not notify_accepted for partial accept.
459-
-- revisit if upstream copilot.vim adds this feature.
460-
return
461-
end
462-
463-
api.notify_accepted(client, { uuid = suggestion.uuid }, function() end)
464-
end)
465-
clear_preview()
466-
467456
if type(modifier) == "function" then
468457
suggestion = modifier(suggestion)
469458
end
470459

460+
with_client(function(client)
461+
api.notify_accepted(
462+
client,
463+
{ uuid = suggestion.uuid, acceptedLength = util.strutf16len(suggestion.text) },
464+
function() end
465+
)
466+
end)
467+
468+
clear_preview()
469+
471470
local range, newText = suggestion.range, suggestion.text
472471

473472
-- Hack for 'autoindent', makes the indent persist. Check `:help 'autoindent'`.

lua/copilot/util.lua

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,4 +294,14 @@ M.auth = function()
294294
require("copilot.auth").signin()
295295
end
296296

297+
---@param str string
298+
---@return integer
299+
function M.strutf16len(str)
300+
return vim.fn.strchars(vim.fn.substitute(str, [==[\\%#=2[^\u0001-\uffff]]==], " ", "g"))
301+
end
302+
303+
if vim.fn.strutf16len then
304+
M.strutf16len = vim.fn.strutf16len
305+
end
306+
297307
return M

0 commit comments

Comments
 (0)