Skip to content

Commit 8a613b7

Browse files
committed
Initial Implementation.
<Tab> accepts one word from suggestion. <S-Tab> accepts all.
1 parent 8c84164 commit 8a613b7

2 files changed

Lines changed: 59 additions & 5 deletions

File tree

autoload/copilot.vim

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ endfunction
419419

420420
function! copilot#IsMapped() abort
421421
return get(g:, 'copilot_assume_mapped') ||
422-
\ hasmapto('copilot#Accept(', 'i')
422+
\ hasmapto('copilot#AcceptOne(', 'i')
423423
endfunction
424424
let s:is_mapped = copilot#IsMapped()
425425

@@ -465,7 +465,57 @@ function! copilot#TextQueuedForInsertion() abort
465465
endtry
466466
endfunction
467467

468-
function! copilot#Accept(...) abort
468+
let s:sugg_buffer = {'sugg': '', 'count': 0, 'text': '', 'valid': v:false}
469+
function! copilot#AcceptOne(...) abort
470+
471+
if !s:sugg_buffer.valid
472+
let s = copilot#GetDisplayedSuggestion()
473+
if !empty(s)
474+
" split the suggestion into words, keep the trailing space
475+
let s:sugg_buffer = {'sugg': s, 'count': 0, 'text': split(s.text, '[\t ]\zs') , 'valid': v:true}
476+
" if the text is whitespace, invalidate the buffer
477+
if (len(s:sugg_buffer.text)==0) || !(s:sugg_buffer.text[0] =~# '\S')
478+
let s:sugg_buffer.valid = v:false
479+
endif
480+
endif
481+
endif
482+
483+
if s:sugg_buffer.valid
484+
let s:suggestion_text = s:sugg_buffer.text[s:sugg_buffer.count]
485+
let s:sugg_buffer.count += 1
486+
if s:sugg_buffer.count >= len(s:sugg_buffer.text)
487+
let s:sugg_buffer.valid = v:false
488+
unlet! b:_copilot
489+
call copilot#Request('notifyAccepted', {'uuid': s:sugg_buffer.sugg.uuid})
490+
unlet! s:uuid
491+
call s:ClearPreview()
492+
endif
493+
if s:sugg_buffer.valid && s:sugg_buffer.count == 1
494+
return repeat("\<Left>\<Del>", s:sugg_buffer.sugg.outdentSize) . repeat("\<Del>", s:sugg_buffer.sugg.deleteSize) .
495+
\ "\<C-R>\<C-O>=copilot#TextQueuedForInsertion()\<CR>"
496+
endif
497+
return "\<C-R>\<C-O>=copilot#TextQueuedForInsertion()\<CR>"
498+
endif
499+
500+
let default = get(g:, 'copilot_tab_fallback', pumvisible() ? "\<C-N>" : "\t")
501+
if !a:0
502+
return default
503+
elseif type(a:1) == v:t_string
504+
return a:1
505+
elseif type(a:1) == v:t_func
506+
try
507+
return call(a:1, [])
508+
catch
509+
call copilot#logger#Exception()
510+
return default
511+
endtry
512+
else
513+
return default
514+
endif
515+
endfunction
516+
517+
518+
function! copilot#AcceptAll(...) abort
469519
let s = copilot#GetDisplayedSuggestion()
470520
if !empty(s.text)
471521
unlet! b:_copilot

plugin/copilot.vim

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@ function! s:ColorScheme() abort
2020
hi def link CopilotAnnotation Normal
2121
endfunction
2222

23+
2324
function! s:MapTab() abort
2425
if get(g:, 'copilot_no_tab_map') || get(g:, 'copilot_no_maps')
2526
return
2627
endif
2728
let tab_map = maparg('<Tab>', 'i', 0, 1)
2829
if !has_key(tab_map, 'rhs')
29-
imap <script><silent><nowait><expr> <Tab> copilot#Accept()
30+
imap <script><silent><nowait><expr> <Tab> copilot#AcceptOne()
31+
imap <script><silent><nowait><expr> <S-Tab> copilot#AcceptAll()
3032
elseif tab_map.rhs !~# 'copilot'
3133
if tab_map.expr
3234
let tab_fallback = '{ -> ' . tab_map.rhs . ' }'
@@ -35,9 +37,11 @@ function! s:MapTab() abort
3537
endif
3638
let tab_fallback = substitute(tab_fallback, '<SID>', '<SNR>' . get(tab_map, 'sid') . '_', 'g')
3739
if get(tab_map, 'noremap') || get(tab_map, 'script') || mapcheck('<Left>', 'i') || mapcheck('<Del>', 'i')
38-
exe 'imap <script><silent><nowait><expr> <Tab> copilot#Accept(' . tab_fallback . ')'
40+
exe 'imap <script><silent><nowait><expr> <Tab> copilot#AcceptOne(' . tab_fallback . ')'
41+
exe 'imap <script><silent><nowait><expr> <S-Tab> copilot#AcceptAll(' . tab_fallback . ')'
3942
else
40-
exe 'imap <silent><nowait><expr> <Tab> copilot#Accept(' . tab_fallback . ')'
43+
exe 'imap <silent><nowait><expr> <Tab> copilot#AcceptOne(' . tab_fallback . ')'
44+
exe 'imap <silent><nowait><expr> <S-Tab> copilot#AcceptAll(' . tab_fallback . ')'
4145
endif
4246
endif
4347
endfunction

0 commit comments

Comments
 (0)