From 1a88c2d56c7130753a7c6590a978159bee01271b Mon Sep 17 00:00:00 2001 From: Rahul Sridhar Date: Tue, 12 Nov 2024 19:04:35 -0800 Subject: [PATCH 1/2] feat: add visual flash effect for completion acceptance Adds a light blue flash effect when accepting completions: - Works for both single-line and multi-line completions - Flashes for 200ms before fading - Supports both Vim and Neovim - Handles proper positioning for each line of completion --- autoload/codeium.vim | 83 ++++++++++++++++++++++++++++++++++++++++---- plugin/codeium.vim | 9 +++++ 2 files changed, 86 insertions(+), 6 deletions(-) diff --git a/autoload/codeium.vim b/autoload/codeium.vim index 0ea437e1..9237a817 100644 --- a/autoload/codeium.vim +++ b/autoload/codeium.vim @@ -2,6 +2,9 @@ let s:hlgroup = 'CodeiumSuggestion' let s:request_nonce = 0 let s:using_codeium_status = 0 +" Add global variable for flash text +let g:codeium_flash_text = '' + if !has('nvim') if empty(prop_type_get(s:hlgroup)) call prop_type_add(s:hlgroup, {'highlight': s:hlgroup}) @@ -67,11 +70,6 @@ function! s:CompletionInserter(current_completion, insert_text) abort if end_offset - start_offset > 0 let delete_bytes = end_offset - start_offset let delete_chars = strchars(strpart(getline('.'), 0, delete_bytes)) - " We insert a space, escape to normal mode, then delete the inserted space. - " This lets us "accept" any auto-inserted indentation which is otherwise - " removed when we switch to normal mode. - " \"_ sequence makes sure to delete to the void register. - " This way our current yank is not overridden. let delete_range = " \\"_x0\"_d" . delete_chars . 'li' endif @@ -83,8 +81,81 @@ function! s:CompletionInserter(current_completion, insert_text) abort else let cursor_text = "\:exe 'go' line2byte(line('.'))+col('.')+(" . delta . ")\" endif + call codeium#server#Request('AcceptCompletion', {'metadata': codeium#server#RequestMetadata(), 'completion_id': current_completion.completion.completionId}) - return delete_range . insert_text . cursor_text + + " Schedule the flash effect to occur after the text is inserted + let g:codeium_flash_text = text + let flash_cmd = "\\=execute('call timer_start(10, {-> codeium#FlashAcceptedCompletion(g:codeium_flash_text)})')\" + + return delete_range . insert_text . cursor_text . flash_cmd +endfunction + +function! codeium#FlashAcceptedCompletion(text) abort + let lines = split(a:text, "\n", 1) + let start_line = line('.') + let start_col = col('.') + + if !has('nvim') + try + let prop_ids = [] + let current_line = start_line - len(lines) + 1 + + " Handle each line of the completion + for i in range(len(lines)) + let line_text = lines[i] + let len = strchars(line_text) + if len > 0 + " For first line, account for cursor position + if i == 0 + let col_pos = start_col - len + else + let col_pos = 1 + endif + + " Add property for this line + let prop_id = prop_add(current_line, col_pos, {'length': len, 'type': 'CodeiumFlash'}) + call add(prop_ids, prop_id) + endif + let current_line += 1 + endfor + + " Remove all properties after timeout + call timer_start(200, {-> execute('for id in ' . string(prop_ids) . ' | silent! call prop_remove({"id": id, "all": 1}) | endfor')}) + catch + echom "Flash error: " . v:exception + endtry + else + let ns_id = nvim_create_namespace('codeium_flash') + try + let current_line = start_line - len(lines) + + " Handle each line of the completion + for i in range(len(lines)) + let line_text = lines[i] + let len = strchars(line_text) + if len > 0 + " For first line, account for cursor position + if i == 0 + let start_col_pos = start_col - len - 1 + let end_col_pos = start_col - 1 + else + let start_col_pos = 0 + let end_col_pos = len + endif + + " Add highlight for this line + call nvim_buf_add_highlight(0, ns_id, 'CodeiumFlash', current_line, start_col_pos, end_col_pos) + endif + let current_line += 1 + endfor + + " Clear namespace after timeout + call timer_start(200, {-> nvim_buf_clear_namespace(0, ns_id, 0, -1)}) + catch + echom "Flash error: " . v:exception + endtry + endif endfunction function! codeium#Accept() abort diff --git a/plugin/codeium.vim b/plugin/codeium.vim index 36f1bd07..68ffd781 100644 --- a/plugin/codeium.vim +++ b/plugin/codeium.vim @@ -12,10 +12,19 @@ endif function! s:SetStyle() abort if &t_Co == 256 hi def CodeiumSuggestion guifg=#808080 ctermfg=244 + hi def CodeiumFlash ctermbg=153 guibg=#ADD8E6 gui=bold cterm=bold else hi def CodeiumSuggestion guifg=#808080 ctermfg=8 + hi def CodeiumFlash ctermbg=7 guibg=LightBlue gui=bold cterm=bold endif hi def link CodeiumAnnotation Normal + + " Register the property types for Vim + if !has('nvim') + if empty(prop_type_get('CodeiumFlash')) + call prop_type_add('CodeiumFlash', {'highlight': 'CodeiumFlash'}) + endif + endif endfunction function! s:MapTab() abort From 8dbbcfa9bee946f612ca22c73ec8b643e9b7cd81 Mon Sep 17 00:00:00 2001 From: Rahul Sridhar Date: Tue, 12 Nov 2024 19:29:00 -0800 Subject: [PATCH 2/2] fix --- autoload/codeium.vim | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/autoload/codeium.vim b/autoload/codeium.vim index 9237a817..bf0cfcf2 100644 --- a/autoload/codeium.vim +++ b/autoload/codeium.vim @@ -93,20 +93,22 @@ endfunction function! codeium#FlashAcceptedCompletion(text) abort let lines = split(a:text, "\n", 1) + let num_lines = len(lines) let start_line = line('.') let start_col = col('.') if !has('nvim') try let prop_ids = [] - let current_line = start_line - len(lines) + 1 + " Start from the current line and work backwards + let current_line = start_line " Handle each line of the completion - for i in range(len(lines)) - let line_text = lines[i] + for i in range(num_lines) + let line_text = lines[num_lines - i - 1] let len = strchars(line_text) if len > 0 - " For first line, account for cursor position + " For last line (first in completion), account for cursor position if i == 0 let col_pos = start_col - len else @@ -117,7 +119,7 @@ function! codeium#FlashAcceptedCompletion(text) abort let prop_id = prop_add(current_line, col_pos, {'length': len, 'type': 'CodeiumFlash'}) call add(prop_ids, prop_id) endif - let current_line += 1 + let current_line -= 1 endfor " Remove all properties after timeout @@ -128,14 +130,15 @@ function! codeium#FlashAcceptedCompletion(text) abort else let ns_id = nvim_create_namespace('codeium_flash') try - let current_line = start_line - len(lines) + " Start from the current line and work backwards + let current_line = start_line - 1 " Handle each line of the completion - for i in range(len(lines)) - let line_text = lines[i] + for i in range(num_lines) + let line_text = lines[num_lines - i - 1] let len = strchars(line_text) if len > 0 - " For first line, account for cursor position + " For last line (first in completion), account for cursor position if i == 0 let start_col_pos = start_col - len - 1 let end_col_pos = start_col - 1 @@ -147,7 +150,7 @@ function! codeium#FlashAcceptedCompletion(text) abort " Add highlight for this line call nvim_buf_add_highlight(0, ns_id, 'CodeiumFlash', current_line, start_col_pos, end_col_pos) endif - let current_line += 1 + let current_line -= 1 endfor " Clear namespace after timeout