Skip to content

Commit 6ec86de

Browse files
committed
feat(copilot): Enable inline suggestion cycling
1 parent c013148 commit 6ec86de

4 files changed

Lines changed: 36 additions & 3 deletions

File tree

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,4 @@ See `:help copilot` for more information.
4545

4646
## Limitations
4747

48-
Copilot.vim does not yet support cycling through alternate suggestions on
49-
Alt+[ and Alt+], or opening the GitHub Copilot panel on Ctrl+Enter.
48+
Copilot.vim does not yet support opening the GitHub Copilot panel on Ctrl+Enter.

autoload/copilot.vim

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@ if exists('g:autoloaded_copilot')
22
finish
33
endif
44
let g:autoloaded_copilot = 1
5+
let g:copilot_echo_complettions = 1
56

67
scriptencoding utf-8
78

89
let s:has_ghost_text = has('nvim-0.6') && exists('*nvim_buf_get_mark')
910

1011
let s:hlgroup = 'CopilotSuggestion'
1112

13+
let b:_current_copilot_suggestion_index = 0
14+
let b:_current_copilot_suggestions = {}
15+
1216
if len($XDG_CONFIG_HOME)
1317
let s:config_root = $XDG_CONFIG_HOME
1418
elseif has('win32')
@@ -207,7 +211,7 @@ function! copilot#Complete(...) abort
207211
let doc = copilot#doc#Get()
208212
if !exists('g:_copilot_completion.params.doc') || g:_copilot_completion.params.doc !=# doc
209213
let g:_copilot_completion =
210-
\ copilot#agent#Request('getCompletions', {'doc': doc, 'options': {}})
214+
\ copilot#agent#Request('getCompletionsCycling', {'doc': doc, 'options': {}})
211215
let g:_copilot_last_completion = g:_copilot_completion
212216
endif
213217
let completion = g:_copilot_completion
@@ -337,14 +341,35 @@ endfunction
337341

338342
function! s:HandleTriggerResult(result) abort
339343
if exists('a:result.completions')
344+
if g:copilot_echo_complettions
345+
echo 'copilot:' . len(a:result.completions) . ' completions'
346+
endif
347+
let b:_current_copilot_suggestions = a:result.completions
348+
let b:_current_copilot_suggestion_index = 0
340349
let b:_copilot_suggestion = get(a:result.completions, 0, {})
341350
else
342351
let b:_copilot_suggestion = {}
352+
let b:_current_copilot_suggestion_index = 0
353+
let b:_current_copilot_suggestions = {}
343354
endif
344355
let b:_copilot_completion = b:_copilot_suggestion
345356
call s:UpdatePreview()
346357
endfunction
347358

359+
function! copilot#NextResult(direction)
360+
let index = b:_current_copilot_suggestion_index + a:direction
361+
if index < 0
362+
let b:_current_copilot_suggestion_index = len(b:_current_copilot_suggestions) - 1
363+
elseif index >= len(b:_current_copilot_suggestions)
364+
let b:_current_copilot_suggestion_index = 0
365+
else
366+
let b:_current_copilot_suggestion_index = index
367+
endif
368+
let b:_copilot_suggestion = get(b:_current_copilot_suggestions, b:_current_copilot_suggestion_index, {})
369+
call s:UpdatePreview()
370+
return ""
371+
endfunction
372+
348373
function! s:Trigger(bufnr, timer) abort
349374
let timer = get(g:, '_copilot_timer', -1)
350375
unlet! g:_copilot_timer

doc/copilot.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,12 @@ Other Maps ~
7878
*copilot-i_CTRL-]*
7979
<C-]> Dismiss the current suggestion.
8080

81+
*copilot-i_ALT-]*
82+
<A-]> Show next inline suggestion.
83+
84+
*copilot-i_ALT-[*
85+
<A-[> Show previous inline suggestion.
86+
8187
SYNTAX HIGHLIGHTING *copilot-highlighting*
8288

8389
Inline suggestions are highlighted using the CopilotSuggestion group,

plugin/copilot.vim

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,6 @@ let s:dir = expand('<sfile>:h:h')
7676
if getftime(s:dir . '/doc/copilot.txt') > getftime(s:dir . '/doc/tags')
7777
silent! execute 'helptags' fnameescape(s:dir . '/doc')
7878
endif
79+
80+
imap <silent><nowait><expr> <A-]> copilot#NextResult(1)
81+
imap <silent><nowait><expr> <A-[> copilot#NextResult(-1)

0 commit comments

Comments
 (0)