Skip to content

Commit

Permalink
feat: accept next word and next line completions. (#380)
Browse files Browse the repository at this point in the history
* 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 #27

* feat: add default bindings for the new functions

* refactor(AcceptNextWord): handle when completionParts is undefined
  • Loading branch information
LeonardoMor committed Jun 12, 2024
1 parent decfb54 commit 9fa0dee
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
29 changes: 26 additions & 3 deletions autoload/codeium.vim
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ function! codeium#CompletionText() abort
endtry
endfunction

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

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

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

let text = current_completion.completion.text . suffix_text
let text = a:insert_text . suffix_text
if empty(text)
return default
endif
Expand Down Expand Up @@ -87,6 +87,29 @@ function! codeium#Accept() abort
return delete_range . insert_text . cursor_text
endfunction

function! codeium#Accept() abort
let current_completion = s:GetCurrentCompletionItem()
return s:CompletionInserter(current_completion, current_completion.completion.text)
endfunction

function! codeium#AcceptNextWord() abort
let current_completion = s:GetCurrentCompletionItem()
let completion_parts = get(current_completion, 'completionParts', [])
if len(completion_parts) == 0
return ''
endif
let prefix_text = get(completion_parts[0], 'prefix', '')
let completion_text = get(completion_parts[0], 'text', '')
let next_word = matchstr(completion_text, '\v^\W*\k*')
return s:CompletionInserter(current_completion, prefix_text . next_word)
endfunction

function! codeium#AcceptNextLine() abort
let current_completion = s:GetCurrentCompletionItem()
let text = substitute(current_completion.completion.text, '\v\n.*$', '', '')
return s:CompletionInserter(current_completion, text)
endfunction

function! s:HandleCompletionsResult(out, err, status) abort
if exists('b:_codeium_completions')
let response_text = join(a:out, '')
Expand Down
6 changes: 6 additions & 0 deletions plugin/codeium.vim
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ if !get(g:, 'codeium_disable_bindings')
if empty(mapcheck('<M-Bslash>', 'i'))
imap <M-Bslash> <Plug>(codeium-complete)
endif
if empty(mapcheck('<C-Right>', 'i'))
imap <script><silent><nowait><expr> <C-Right> codeium#AcceptNextWord()
endif
if empty(mapcheck('<Right>', 'i'))
imap <script><silent><nowait><expr> <Right> codeium#AcceptNextLine()
endif

This comment has been minimized.

Copy link
@wert2all

wert2all Jun 14, 2024

this code disable right navigation on i-mode.

This comment has been minimized.

Copy link
@dror-g

dror-g Jun 15, 2024

yup. same here.

This comment has been minimized.

Copy link
@Akulen

Akulen Jun 17, 2024

I had to add

" Unbind horrible codeium bindings
autocmd VimEnter * iunmap <Right>
autocmd VimEnter * iunmap <C-Right>

To my init.vim to fix this for now, but having better defaults, or a way to disable this, would be better

This comment has been minimized.

Copy link
@klew

klew Jun 17, 2024

Contributor

this code disable right navigation on i-mode.

That's the penalty for using arrow keys in Vim :P

This comment has been minimized.

Copy link
@dror-g

dror-g Jun 17, 2024

this code disable right navigation on i-mode.

That's the penalty for using arrow keys in Vim :P

haha was waiting for this one.. 🤣
But you can't use HJKL when in insert mode, can you?

Do you really never move right?
Or have custom ctrl+hjkl mapping everyone is expected to have?

This comment has been minimized.

Copy link
@klew

klew Jun 17, 2024

Contributor

The only right move is to hit esc to leave insert mode ;)
It seems that I rarely use arrow keys in insert mode. But I had to do a test to observe if I do it ;)

Anyway, I do fully support removal of those key bindings.

endif

call s:SetStyle()
Expand Down

0 comments on commit 9fa0dee

Please sign in to comment.