From 6ec86de119e07a8e378faadeae9b4d0b2b13a805 Mon Sep 17 00:00:00 2001 From: "Alvin(Xinyao) Sun" Date: Sat, 15 Jan 2022 13:56:45 -0800 Subject: [PATCH 1/3] feat(copilot): Enable inline suggestion cycling --- README.md | 3 +-- autoload/copilot.vim | 27 ++++++++++++++++++++++++++- doc/copilot.txt | 6 ++++++ plugin/copilot.vim | 3 +++ 4 files changed, 36 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index a7dbdc05..22ffd4ca 100644 --- a/README.md +++ b/README.md @@ -45,5 +45,4 @@ See `:help copilot` for more information. ## Limitations -Copilot.vim does not yet support cycling through alternate suggestions on -Alt+[ and Alt+], or opening the GitHub Copilot panel on Ctrl+Enter. +Copilot.vim does not yet support opening the GitHub Copilot panel on Ctrl+Enter. diff --git a/autoload/copilot.vim b/autoload/copilot.vim index 291cfb12..1d70b3a3 100644 --- a/autoload/copilot.vim +++ b/autoload/copilot.vim @@ -2,6 +2,7 @@ if exists('g:autoloaded_copilot') finish endif let g:autoloaded_copilot = 1 +let g:copilot_echo_complettions = 1 scriptencoding utf-8 @@ -9,6 +10,9 @@ let s:has_ghost_text = has('nvim-0.6') && exists('*nvim_buf_get_mark') let s:hlgroup = 'CopilotSuggestion' +let b:_current_copilot_suggestion_index = 0 +let b:_current_copilot_suggestions = {} + if len($XDG_CONFIG_HOME) let s:config_root = $XDG_CONFIG_HOME elseif has('win32') @@ -207,7 +211,7 @@ function! copilot#Complete(...) abort let doc = copilot#doc#Get() if !exists('g:_copilot_completion.params.doc') || g:_copilot_completion.params.doc !=# doc let g:_copilot_completion = - \ copilot#agent#Request('getCompletions', {'doc': doc, 'options': {}}) + \ copilot#agent#Request('getCompletionsCycling', {'doc': doc, 'options': {}}) let g:_copilot_last_completion = g:_copilot_completion endif let completion = g:_copilot_completion @@ -337,14 +341,35 @@ endfunction function! s:HandleTriggerResult(result) abort if exists('a:result.completions') + if g:copilot_echo_complettions + echo 'copilot:' . len(a:result.completions) . ' completions' + endif + let b:_current_copilot_suggestions = a:result.completions + let b:_current_copilot_suggestion_index = 0 let b:_copilot_suggestion = get(a:result.completions, 0, {}) else let b:_copilot_suggestion = {} + let b:_current_copilot_suggestion_index = 0 + let b:_current_copilot_suggestions = {} endif let b:_copilot_completion = b:_copilot_suggestion call s:UpdatePreview() endfunction +function! copilot#NextResult(direction) + let index = b:_current_copilot_suggestion_index + a:direction + if index < 0 + let b:_current_copilot_suggestion_index = len(b:_current_copilot_suggestions) - 1 + elseif index >= len(b:_current_copilot_suggestions) + let b:_current_copilot_suggestion_index = 0 + else + let b:_current_copilot_suggestion_index = index + endif + let b:_copilot_suggestion = get(b:_current_copilot_suggestions, b:_current_copilot_suggestion_index, {}) + call s:UpdatePreview() + return "" +endfunction + function! s:Trigger(bufnr, timer) abort let timer = get(g:, '_copilot_timer', -1) unlet! g:_copilot_timer diff --git a/doc/copilot.txt b/doc/copilot.txt index 1d2e36d4..1b4749bf 100644 --- a/doc/copilot.txt +++ b/doc/copilot.txt @@ -78,6 +78,12 @@ Other Maps ~ *copilot-i_CTRL-]* Dismiss the current suggestion. + *copilot-i_ALT-]* + Show next inline suggestion. + + *copilot-i_ALT-[* + Show previous inline suggestion. + SYNTAX HIGHLIGHTING *copilot-highlighting* Inline suggestions are highlighted using the CopilotSuggestion group, diff --git a/plugin/copilot.vim b/plugin/copilot.vim index 1309553f..7e9419c4 100644 --- a/plugin/copilot.vim +++ b/plugin/copilot.vim @@ -76,3 +76,6 @@ let s:dir = expand(':h:h') if getftime(s:dir . '/doc/copilot.txt') > getftime(s:dir . '/doc/tags') silent! execute 'helptags' fnameescape(s:dir . '/doc') endif + +imap copilot#NextResult(1) +imap copilot#NextResult(-1) From 27974ad28cce098f333aff1343f1a7d4244053b8 Mon Sep 17 00:00:00 2001 From: "Alvin(Xinyao) Sun" Date: Sat, 15 Jan 2022 14:05:41 -0800 Subject: [PATCH 2/3] docs(copilot): Improve documentation --- autoload/copilot.vim | 3 +-- doc/copilot.txt | 3 +++ plugin/copilot.vim | 1 + 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/autoload/copilot.vim b/autoload/copilot.vim index 1d70b3a3..1c663fc2 100644 --- a/autoload/copilot.vim +++ b/autoload/copilot.vim @@ -2,7 +2,6 @@ if exists('g:autoloaded_copilot') finish endif let g:autoloaded_copilot = 1 -let g:copilot_echo_complettions = 1 scriptencoding utf-8 @@ -341,7 +340,7 @@ endfunction function! s:HandleTriggerResult(result) abort if exists('a:result.completions') - if g:copilot_echo_complettions + if g:copilot_echo_num_completions echo 'copilot:' . len(a:result.completions) . ' completions' endif let b:_current_copilot_suggestions = a:result.completions diff --git a/doc/copilot.txt b/doc/copilot.txt index 1b4749bf..f754692e 100644 --- a/doc/copilot.txt +++ b/doc/copilot.txt @@ -55,6 +55,9 @@ b:copilot_enabled Set to v:false to disable GitHub Copilot for the current buffer. Or set to v:true to force enabling it, overriding g:copilot_filetypes. + *g:copilot_echo_complettions* +g:copilot_echo_num_completions Enabling echo of number of suggestions. + MAPS *copilot-maps* *copilot-i_* diff --git a/plugin/copilot.vim b/plugin/copilot.vim index 7e9419c4..ebf0de96 100644 --- a/plugin/copilot.vim +++ b/plugin/copilot.vim @@ -2,6 +2,7 @@ if exists('g:loaded_copilot') finish endif let g:loaded_copilot = 1 +let g:copilot_echo_num_completions = 1 scriptencoding utf-8 From bfdd099497c5f853cc40ba5c33fe1cf613023050 Mon Sep 17 00:00:00 2001 From: "Alvin(Xinyao) Sun" Date: Sat, 15 Jan 2022 14:22:27 -0800 Subject: [PATCH 3/3] docs(copilot): Fix a typo in docs --- doc/copilot.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/copilot.txt b/doc/copilot.txt index f754692e..005faa43 100644 --- a/doc/copilot.txt +++ b/doc/copilot.txt @@ -55,7 +55,7 @@ b:copilot_enabled Set to v:false to disable GitHub Copilot for the current buffer. Or set to v:true to force enabling it, overriding g:copilot_filetypes. - *g:copilot_echo_complettions* + *g:copilot_echo_num_completions* g:copilot_echo_num_completions Enabling echo of number of suggestions. MAPS *copilot-maps*