Skip to content

Commit 76c6617

Browse files
committed
refactor: centralize some logic
1 parent ac01005 commit 76c6617

File tree

1 file changed

+47
-41
lines changed

1 file changed

+47
-41
lines changed

lua/copilot/suggestion/init.lua

Lines changed: 47 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -453,30 +453,6 @@ local function get_suggestions_cycling(callback, ctx)
453453
end
454454
end
455455

456-
function M.has_next()
457-
local ctx = get_ctx()
458-
459-
-- no completions at all
460-
if not ctx.suggestions or #ctx.suggestions == 0 then
461-
return false
462-
end
463-
464-
return (ctx.choice < #ctx.suggestions or not ctx.cycling)
465-
end
466-
467-
local function advance(count, ctx)
468-
if ctx ~= get_ctx() then
469-
return
470-
end
471-
472-
ctx.choice = (ctx.choice + count) % #ctx.suggestions
473-
if ctx.choice < 1 then
474-
ctx.choice = #ctx.suggestions
475-
end
476-
477-
update_preview(ctx)
478-
end
479-
480456
local function schedule(ctx)
481457
local function is_authenticated()
482458
return auth.is_authenticated(function()
@@ -505,14 +481,53 @@ local function schedule(ctx)
505481
end)
506482
end
507483

484+
---@param context string
485+
local function request_suggestion(context)
486+
logger.trace("suggestion on " .. context)
487+
c.buf_attach()
488+
schedule()
489+
end
490+
491+
---@param context string
492+
local function request_suggestion_when_auto_trigger(context)
493+
if not should_auto_trigger() then
494+
return
495+
end
496+
497+
request_suggestion(context)
498+
end
499+
500+
function M.has_next()
501+
local ctx = get_ctx()
502+
503+
-- no completions at all
504+
if not ctx.suggestions or #ctx.suggestions == 0 then
505+
return false
506+
end
507+
508+
return (ctx.choice < #ctx.suggestions or not ctx.cycling)
509+
end
510+
511+
local function advance(count, ctx)
512+
if ctx ~= get_ctx() then
513+
return
514+
end
515+
516+
ctx.choice = (ctx.choice + count) % #ctx.suggestions
517+
if ctx.choice < 1 then
518+
ctx.choice = #ctx.suggestions
519+
end
520+
521+
update_preview(ctx)
522+
end
523+
508524
---@param ctx copilot_suggestion_context
509525
---@param caller_context string
510526
---@return boolean
511527
function M.first_request_scheduled(ctx, caller_context)
512528
if not ctx.first then
513529
logger.trace("suggestion " .. caller_context .. ", no first request", ctx)
514-
c.buf_attach()
515-
schedule(ctx)
530+
request_suggestion(caller_context)
516531
return true
517532
end
518533

@@ -720,22 +735,16 @@ local function on_buf_leave()
720735
end
721736

722737
local function on_insert_enter()
723-
if should_auto_trigger() then
724-
logger.trace("suggestion on insert enter")
725-
c.buf_attach()
726-
schedule()
727-
end
738+
request_suggestion_when_auto_trigger("insert enter")
728739
end
729740

730741
local function on_filetype()
731-
logger.trace("suggestion on file type")
732-
on_insert_enter()
742+
request_suggestion_when_auto_trigger("file type")
733743
end
734744

735745
local function on_buf_enter()
736746
if vim.fn.mode():match("^[iR]") then
737-
logger.trace("suggestion on buf enter")
738-
on_insert_enter()
747+
request_suggestion_when_auto_trigger("buf enter")
739748
end
740749
end
741750

@@ -747,18 +756,15 @@ local function on_cursor_moved_i()
747756

748757
local ctx = get_ctx()
749758
if copilot._copilot_timer or ctx.params or should_auto_trigger() then
750-
logger.trace("suggestion on cursor moved insert")
751-
c.buf_attach()
752-
schedule(ctx)
759+
request_suggestion("cursor moved insert")
753760
end
754761
end
755762

756763
local function on_text_changed_p()
757764
local ctx = get_ctx()
765+
758766
if not copilot.hide_during_completion and (copilot._copilot_timer or ctx.params or should_auto_trigger()) then
759-
logger.trace("suggestion on text changed pum")
760-
c.buf_attach()
761-
schedule(ctx)
767+
request_suggestion("text changed pum")
762768
end
763769
end
764770

0 commit comments

Comments
 (0)