Skip to content

Commit 9fa0dee

Browse files
authored
feat: accept next word and next line completions. (Exafunction#380)
* feat: accept next word and next line completions. The process of inserting suggestions is always roughly the same, so abstracted that into the `s:CompletionInserter` function. With that, refactored `codeium#Accept` and introduced `codeium#AcceptNextWord` and `codeium#AcceptNextLine`. Fixes Exafunction#27 * feat: add default bindings for the new functions * refactor(AcceptNextWord): handle when completionParts is undefined
1 parent decfb54 commit 9fa0dee

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

autoload/codeium.vim

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,14 @@ function! codeium#CompletionText() abort
3939
endtry
4040
endfunction
4141

42-
function! codeium#Accept() abort
42+
function! s:CompletionInserter(current_completion, insert_text) abort
4343
let default = get(g:, 'codeium_tab_fallback', pumvisible() ? "\<C-N>" : "\t")
4444

4545
if mode() !~# '^[iR]' || !exists('b:_codeium_completions')
4646
return default
4747
endif
4848

49-
let current_completion = s:GetCurrentCompletionItem()
49+
let current_completion = a:current_completion
5050
if current_completion is v:null
5151
return default
5252
endif
@@ -58,7 +58,7 @@ function! codeium#Accept() abort
5858
let start_offset = get(range, 'startOffset', 0)
5959
let end_offset = get(range, 'endOffset', 0)
6060

61-
let text = current_completion.completion.text . suffix_text
61+
let text = a:insert_text . suffix_text
6262
if empty(text)
6363
return default
6464
endif
@@ -87,6 +87,29 @@ function! codeium#Accept() abort
8787
return delete_range . insert_text . cursor_text
8888
endfunction
8989

90+
function! codeium#Accept() abort
91+
let current_completion = s:GetCurrentCompletionItem()
92+
return s:CompletionInserter(current_completion, current_completion.completion.text)
93+
endfunction
94+
95+
function! codeium#AcceptNextWord() abort
96+
let current_completion = s:GetCurrentCompletionItem()
97+
let completion_parts = get(current_completion, 'completionParts', [])
98+
if len(completion_parts) == 0
99+
return ''
100+
endif
101+
let prefix_text = get(completion_parts[0], 'prefix', '')
102+
let completion_text = get(completion_parts[0], 'text', '')
103+
let next_word = matchstr(completion_text, '\v^\W*\k*')
104+
return s:CompletionInserter(current_completion, prefix_text . next_word)
105+
endfunction
106+
107+
function! codeium#AcceptNextLine() abort
108+
let current_completion = s:GetCurrentCompletionItem()
109+
let text = substitute(current_completion.completion.text, '\v\n.*$', '', '')
110+
return s:CompletionInserter(current_completion, text)
111+
endfunction
112+
90113
function! s:HandleCompletionsResult(out, err, status) abort
91114
if exists('b:_codeium_completions')
92115
let response_text = join(a:out, '')

plugin/codeium.vim

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ if !get(g:, 'codeium_disable_bindings')
5858
if empty(mapcheck('<M-Bslash>', 'i'))
5959
imap <M-Bslash> <Plug>(codeium-complete)
6060
endif
61+
if empty(mapcheck('<C-Right>', 'i'))
62+
imap <script><silent><nowait><expr> <C-Right> codeium#AcceptNextWord()
63+
endif
64+
if empty(mapcheck('<Right>', 'i'))
65+
imap <script><silent><nowait><expr> <Right> codeium#AcceptNextLine()
66+
endif
6167
endif
6268

6369
call s:SetStyle()

0 commit comments

Comments
 (0)