Skip to content

Commit 3de3e90

Browse files
committed
feat(copilot.suggestions): export clear_preview and update_preview functions
Signed-off-by: sami <sami@appscode.com>
1 parent 46e9895 commit 3de3e90

3 files changed

Lines changed: 23 additions & 16 deletions

File tree

doc/copilot.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,8 @@ The `copilot.suggestion` module exposes the following functions:
272272
require("copilot.suggestion").accept_line()
273273
require("copilot.suggestion").next()
274274
require("copilot.suggestion").prev()
275+
require("copilot.suggestion").clear_preview()
276+
require("copilot.suggestion").update_preview()
275277
require("copilot.suggestion").dismiss()
276278
require("copilot.suggestion").toggle_auto_trigger()
277279
<

lua/copilot/suggestion/init.lua

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ local function cancel_inflight_requests(ctx)
197197
end)
198198
end
199199

200-
local function clear_preview()
200+
function M.clear_preview()
201201
logger.trace("suggestion clear preview")
202202
vim.api.nvim_buf_del_extmark(0, copilot.ns_id, copilot.extmark_id)
203203
end
@@ -236,14 +236,14 @@ local function get_current_suggestion(ctx)
236236
end
237237

238238
---@param ctx? copilot_suggestion_context
239-
local function update_preview(ctx)
239+
function M.update_preview(ctx)
240240
ctx = ctx or get_ctx()
241241
logger.trace("suggestion update preview", ctx)
242242

243243
local suggestion = get_current_suggestion(ctx)
244244
local displayLines = suggestion and vim.split(suggestion.displayText, "\n", { plain = true }) or {}
245245

246-
clear_preview()
246+
M.clear_preview()
247247

248248
if not suggestion or #displayLines == 0 then
249249
return
@@ -321,7 +321,7 @@ local function clear(ctx)
321321
ctx = ctx or get_ctx()
322322
stop_timer()
323323
cancel_inflight_requests(ctx)
324-
update_preview(ctx)
324+
M.update_preview(ctx)
325325
reset_ctx(ctx)
326326
end
327327

@@ -352,7 +352,7 @@ local function handle_trigger_request(err, data)
352352
ctx.suggestions = data and data.completions or {}
353353
ctx.choice = 1
354354
ctx.shown_choices = {}
355-
update_preview()
355+
M.update_preview()
356356
end
357357

358358
local function trigger(bufnr, timer)
@@ -423,7 +423,7 @@ local function get_suggestions_cycling(callback, ctx)
423423
get_suggestions_cycling_callback(ctx, err, data)
424424
end)
425425
ctx.cycling = id --[[@as integer]]
426-
update_preview(ctx)
426+
M.update_preview(ctx)
427427
end)
428428
end
429429
end
@@ -449,7 +449,7 @@ local function schedule(bufnr)
449449
stop_timer()
450450
end
451451

452-
update_preview()
452+
M.update_preview()
453453
bufnr = bufnr or vim.api.nvim_get_current_buf()
454454
copilot._copilot_timer = vim.fn.timer_start(copilot.debounce, function(timer)
455455
logger.trace("suggestion schedule timer", bufnr)
@@ -494,7 +494,7 @@ local function advance(count, ctx)
494494
ctx.choice = #ctx.suggestions
495495
end
496496

497-
update_preview(ctx)
497+
M.update_preview(ctx)
498498
end
499499

500500
---@param ctx copilot_suggestion_context
@@ -588,7 +588,7 @@ function M.accept(modifier)
588588
ctx.accepted_partial = true
589589
ignore_next_cursor_moved = true
590590
else
591-
clear_preview()
591+
M.clear_preview()
592592
newText = suggestion.text
593593
end
594594

@@ -641,7 +641,7 @@ function M.accept(modifier)
641641
end
642642

643643
update_ctx_suggestion_position(ctx.choice, new_cursor_line - 1, last_col, bufnr)
644-
update_preview(ctx)
644+
M.update_preview(ctx)
645645
end
646646
end)()
647647
end
@@ -687,7 +687,7 @@ function M.dismiss()
687687
local ctx = get_ctx()
688688
reject()
689689
clear(ctx)
690-
update_preview(ctx)
690+
M.update_preview(ctx)
691691
end
692692

693693
function M.is_visible()

plugin/copilot.lua

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ local completion_store = {
22
[""] = { "auth", "attach", "detach", "disable", "enable", "panel", "status", "suggestion", "toggle", "version" },
33
auth = { "signin", "signout", "info" },
44
panel = { "accept", "jump_next", "jump_prev", "open", "refresh", "toggle", "close", "is_open" },
5-
suggestion = { "accept", "accept_line", "accept_word", "dismiss", "next", "prev", "toggle_auto_trigger" },
5+
suggestion = { "accept", "accept_line", "accept_word", "dismiss", "next", "prev", "toggle_auto_trigger", "clear_preview", "update_preview" },
66
workspace = { "add" },
77
}
88

@@ -50,10 +50,15 @@ vim.api.nvim_create_user_command("Copilot", function(opts)
5050
end
5151

5252
require("copilot.client").use_client(function()
53-
local result = mod[action_name]({
54-
force = opts.bang,
55-
args = remaining_args,
56-
})
53+
local result
54+
if mod_name == "suggestion" and action_name == "update_preview" then
55+
result = mod[action_name]()
56+
else
57+
result = mod[action_name]({
58+
force = opts.bang,
59+
args = remaining_args,
60+
})
61+
end
5762

5863
if result ~= nil then
5964
print(tostring(result))

0 commit comments

Comments
 (0)