From 8a613b73b452d52f13f6bb0ed53a79e3ba856b4c Mon Sep 17 00:00:00 2001 From: Muhammad Abdullah Date: Fri, 24 Feb 2023 02:11:27 -0500 Subject: [PATCH 1/3] Initial Implementation. accepts one word from suggestion. accepts all. --- autoload/copilot.vim | 54 ++++++++++++++++++++++++++++++++++++++++++-- plugin/copilot.vim | 10 +++++--- 2 files changed, 59 insertions(+), 5 deletions(-) diff --git a/autoload/copilot.vim b/autoload/copilot.vim index c8fd8eb3..68ccc00b 100644 --- a/autoload/copilot.vim +++ b/autoload/copilot.vim @@ -419,7 +419,7 @@ endfunction function! copilot#IsMapped() abort return get(g:, 'copilot_assume_mapped') || - \ hasmapto('copilot#Accept(', 'i') + \ hasmapto('copilot#AcceptOne(', 'i') endfunction let s:is_mapped = copilot#IsMapped() @@ -465,7 +465,57 @@ function! copilot#TextQueuedForInsertion() abort endtry endfunction -function! copilot#Accept(...) abort +let s:sugg_buffer = {'sugg': '', 'count': 0, 'text': '', 'valid': v:false} +function! copilot#AcceptOne(...) abort + + if !s:sugg_buffer.valid + let s = copilot#GetDisplayedSuggestion() + if !empty(s) + " split the suggestion into words, keep the trailing space + let s:sugg_buffer = {'sugg': s, 'count': 0, 'text': split(s.text, '[\t ]\zs') , 'valid': v:true} + " if the text is whitespace, invalidate the buffer + if (len(s:sugg_buffer.text)==0) || !(s:sugg_buffer.text[0] =~# '\S') + let s:sugg_buffer.valid = v:false + endif + endif + endif + + if s:sugg_buffer.valid + let s:suggestion_text = s:sugg_buffer.text[s:sugg_buffer.count] + let s:sugg_buffer.count += 1 + if s:sugg_buffer.count >= len(s:sugg_buffer.text) + let s:sugg_buffer.valid = v:false + unlet! b:_copilot + call copilot#Request('notifyAccepted', {'uuid': s:sugg_buffer.sugg.uuid}) + unlet! s:uuid + call s:ClearPreview() + endif + if s:sugg_buffer.valid && s:sugg_buffer.count == 1 + return repeat("\\", s:sugg_buffer.sugg.outdentSize) . repeat("\", s:sugg_buffer.sugg.deleteSize) . + \ "\\=copilot#TextQueuedForInsertion()\" + endif + return "\\=copilot#TextQueuedForInsertion()\" + endif + + let default = get(g:, 'copilot_tab_fallback', pumvisible() ? "\" : "\t") + if !a:0 + return default + elseif type(a:1) == v:t_string + return a:1 + elseif type(a:1) == v:t_func + try + return call(a:1, []) + catch + call copilot#logger#Exception() + return default + endtry + else + return default + endif +endfunction + + +function! copilot#AcceptAll(...) abort let s = copilot#GetDisplayedSuggestion() if !empty(s.text) unlet! b:_copilot diff --git a/plugin/copilot.vim b/plugin/copilot.vim index 54407ddd..4d4a9b95 100644 --- a/plugin/copilot.vim +++ b/plugin/copilot.vim @@ -20,13 +20,15 @@ function! s:ColorScheme() abort hi def link CopilotAnnotation Normal endfunction + function! s:MapTab() abort if get(g:, 'copilot_no_tab_map') || get(g:, 'copilot_no_maps') return endif let tab_map = maparg('', 'i', 0, 1) if !has_key(tab_map, 'rhs') - imap