Skip to content

Commit 5dd548c

Browse files
authored
Merge pull request #1 from Lucklyric/feat/cycling-alternate-suggestions
Feat/cycling alternate suggestions
2 parents c013148 + 27974ad commit 5dd548c

4 files changed

Lines changed: 39 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: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ let s:has_ghost_text = has('nvim-0.6') && exists('*nvim_buf_get_mark')
99

1010
let s:hlgroup = 'CopilotSuggestion'
1111

12+
let b:_current_copilot_suggestion_index = 0
13+
let b:_current_copilot_suggestions = {}
14+
1215
if len($XDG_CONFIG_HOME)
1316
let s:config_root = $XDG_CONFIG_HOME
1417
elseif has('win32')
@@ -207,7 +210,7 @@ function! copilot#Complete(...) abort
207210
let doc = copilot#doc#Get()
208211
if !exists('g:_copilot_completion.params.doc') || g:_copilot_completion.params.doc !=# doc
209212
let g:_copilot_completion =
210-
\ copilot#agent#Request('getCompletions', {'doc': doc, 'options': {}})
213+
\ copilot#agent#Request('getCompletionsCycling', {'doc': doc, 'options': {}})
211214
let g:_copilot_last_completion = g:_copilot_completion
212215
endif
213216
let completion = g:_copilot_completion
@@ -337,14 +340,35 @@ endfunction
337340

338341
function! s:HandleTriggerResult(result) abort
339342
if exists('a:result.completions')
343+
if g:copilot_echo_num_completions
344+
echo 'copilot:' . len(a:result.completions) . ' completions'
345+
endif
346+
let b:_current_copilot_suggestions = a:result.completions
347+
let b:_current_copilot_suggestion_index = 0
340348
let b:_copilot_suggestion = get(a:result.completions, 0, {})
341349
else
342350
let b:_copilot_suggestion = {}
351+
let b:_current_copilot_suggestion_index = 0
352+
let b:_current_copilot_suggestions = {}
343353
endif
344354
let b:_copilot_completion = b:_copilot_suggestion
345355
call s:UpdatePreview()
346356
endfunction
347357

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

doc/copilot.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ b:copilot_enabled Set to v:false to disable GitHub Copilot for the
5555
current buffer. Or set to v:true to force enabling
5656
it, overriding g:copilot_filetypes.
5757

58+
*g:copilot_echo_complettions*
59+
g:copilot_echo_num_completions Enabling echo of number of suggestions.
60+
5861
MAPS *copilot-maps*
5962

6063
*copilot-i_<Tab>*
@@ -78,6 +81,12 @@ Other Maps ~
7881
*copilot-i_CTRL-]*
7982
<C-]> Dismiss the current suggestion.
8083

84+
*copilot-i_ALT-]*
85+
<A-]> Show next inline suggestion.
86+
87+
*copilot-i_ALT-[*
88+
<A-[> Show previous inline suggestion.
89+
8190
SYNTAX HIGHLIGHTING *copilot-highlighting*
8291

8392
Inline suggestions are highlighted using the CopilotSuggestion group,

plugin/copilot.vim

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ if exists('g:loaded_copilot')
22
finish
33
endif
44
let g:loaded_copilot = 1
5+
let g:copilot_echo_num_completions = 1
56

67
scriptencoding utf-8
78

@@ -76,3 +77,6 @@ let s:dir = expand('<sfile>:h:h')
7677
if getftime(s:dir . '/doc/copilot.txt') > getftime(s:dir . '/doc/tags')
7778
silent! execute 'helptags' fnameescape(s:dir . '/doc')
7879
endif
80+
81+
imap <silent><nowait><expr> <A-]> copilot#NextResult(1)
82+
imap <silent><nowait><expr> <A-[> copilot#NextResult(-1)

0 commit comments

Comments
 (0)