From 90e3bdc9421a35d715c1fc5027c9aa8de00b3fff Mon Sep 17 00:00:00 2001 From: Michael Coppola Date: Tue, 22 Oct 2024 02:17:08 -0400 Subject: [PATCH 01/19] added classic vim support --- examples/llama.vim | 163 +++++++++++++++++++++++++++++++++++---------- 1 file changed, 129 insertions(+), 34 deletions(-) diff --git a/examples/llama.vim b/examples/llama.vim index 7a60442adc3..7cc575a0396 100644 --- a/examples/llama.vim +++ b/examples/llama.vim @@ -43,8 +43,8 @@ " " colors (adjust to your liking) -highlight llama_hl_hint guifg=#ff772f -highlight llama_hl_info guifg=#77ff2f +highlight llama_hl_hint guifg=#ff772f ctermfg=202 +highlight llama_hl_info guifg=#77ff2f ctermfg=119 " general parameters: " @@ -91,8 +91,38 @@ let s:default_config = { \ 'ring_update_ms': 1000, \ } +let s:nvim_ghost_text = exists('*nvim_buf_get_mark') +let s:vim_ghost_text = has('textprop') + +if s:vim_ghost_text + let s:hint_hlgroup = 'llama_hl_hint' + let s:info_hlgroup = 'llama_hl_info' + + if empty(prop_type_get(s:hint_hlgroup)) + call prop_type_add(s:hint_hlgroup, {'highlight': s:hint_hlgroup}) + endif + if empty(prop_type_get(s:info_hlgroup)) + call prop_type_add(s:info_hlgroup, {'highlight': s:info_hlgroup}) + endif +endif + let g:llama_config = get(g:, 'llama_config', s:default_config) +function! s:get_indent(str) + let l:count = 0 + for i in range(len(a:str)) + if a:str[i] == "\t" + let l:count += &shiftwidth - 1 + elseif a:str[i] == " " + let l:count += 1 + else + break + endif + endfor + return l:count +endfunction + + function! s:rand(i0, i1) abort return a:i0 + rand() % (a:i1 - a:i0 + 1) endfunction @@ -323,7 +353,11 @@ function! s:ring_update() \ ) " no callbacks because we don't need to process the response - call jobstart(l:curl_command, {}) + if s:nvim_ghost_text + call jobstart(l:curl_command, {}) + elseif s:vim_ghost_text + call job_start(l:curl_command, {}) + endif endfunction " necessary for 'inoremap ' @@ -418,24 +452,39 @@ function! llama#fim(is_auto) abort \ 't_max_predict_ms': g:llama_config.t_max_predict_ms \ }) - let l:curl_command = printf( - \ "curl --silent --no-buffer --request POST --url %s --header \"Content-Type: application/json\" --data %s", - \ g:llama_config.endpoint, shellescape(l:request) - \ ) + let l:curl_command = [ + \ "curl", + \ "--silent", + \ "--no-buffer", + \ "--request", "POST", + \ "--url", g:llama_config.endpoint, + \ "--header", "Content-Type: application/json", + \ "--data", l:request + \ ] + if s:current_job != v:null - call jobstop(s:current_job) + if s:nvim_ghost_text + call jobstop(s:current_job) + elseif s:vim_ghost_text + call job_stop(s:current_job) + endif endif " send the request asynchronously - let s:current_job = jobstart(l:curl_command, { - \ 'on_stdout': function('s:fim_on_stdout'), - \ 'on_exit': function('s:fim_on_exit'), - \ 'stdout_buffered': v:true, - \ 'pos_x': s:pos_x, - \ 'pos_y': s:pos_y, - \ 'is_auto': a:is_auto + if s:nvim_ghost_text + let s:current_job = jobstart(l:curl_command, { + \ 'on_stdout': function('s:fim_on_stdout', [s:pos_x, s:pos_y, a:is_auto]), + \ 'on_exit': function('s:nvim_fim_on_exit'), + \ 'stdout_buffered': v:true + \ }) + elseif s:vim_ghost_text + let s:current_job = job_start(l:curl_command, { + \ 'out_cb': function('s:fim_on_stdout', [s:pos_x, s:pos_y, a:is_auto]), + \ 'close_cb': function('s:vim_fim_on_exit'), + \ 'out_io': 'buffer' \ }) + endif " TODO: per-file location let l:delta_y = abs(s:pos_y - s:pos_y_pick) @@ -482,9 +531,13 @@ function! llama#fim_cancel() " clear the virtual text let l:bufnr = bufnr('%') - let l:id_vt_fim = nvim_create_namespace('vt_fim') - - call nvim_buf_clear_namespace(l:bufnr, l:id_vt_fim, 0, -1) + if s:nvim_ghost_text + let l:id_vt_fim = nvim_create_namespace('vt_fim') + call nvim_buf_clear_namespace(l:bufnr, l:id_vt_fim, 0, -1) + elseif s:vim_ghost_text + call prop_remove({'type': s:hint_hlgroup, 'all': v:true}) + call prop_remove({'type': s:info_hlgroup, 'all': v:true}) + endif " remove the mappings silent! iunmap @@ -499,13 +552,18 @@ function! s:on_move() endfunction " callback that processes the FIM result from the server and displays the suggestion -function! s:fim_on_stdout(job_id, data, event) dict - let l:raw = join(a:data, "\n") +function! s:fim_on_stdout(pos_x, pos_y, is_auto, job_id, data, event = 0) + if s:nvim_ghost_text + let l:raw = join(a:data, "\n") + elseif s:vim_ghost_text + let l:raw = a:data + endif + if len(l:raw) == 0 return endif - if self.pos_x != col('.') - 1 || self.pos_y != line('.') + if a:pos_x != col('.') - 1 || a:pos_y != line('.') return endif @@ -514,14 +572,14 @@ function! s:fim_on_stdout(job_id, data, event) dict return endif - let s:pos_x = self.pos_x - let s:pos_y = self.pos_y + let s:pos_x = a:pos_x + let s:pos_y = a:pos_y let s:can_accept = v:true let l:has_info = v:false if s:can_accept && v:shell_error - if !self.is_auto + if !a:is_auto call add(s:content, "<| curl error: is the server on? |>") endif let s:can_accept = v:false @@ -642,7 +700,9 @@ function! s:fim_on_stdout(job_id, data, event) dict " display virtual text with the suggestion let l:bufnr = bufnr('%') - let l:id_vt_fim = nvim_create_namespace('vt_fim') + if s:nvim_ghost_text + let l:id_vt_fim = nvim_create_namespace('vt_fim') + endif " construct the info message if g:llama_config.show_info > 0 && l:has_info @@ -671,15 +731,46 @@ function! s:fim_on_stdout(job_id, data, event) dict endif " display the suggestion and append the info to the end of the first line - call nvim_buf_set_extmark(l:bufnr, l:id_vt_fim, s:pos_y - 1, s:pos_x - 1, { - \ 'virt_text': [[s:content[0], 'llama_hl_hint'], [l:info, 'llama_hl_info']], - \ 'virt_text_win_col': virtcol('.') - 1 - \ }) + if s:nvim_ghost_text + call nvim_buf_set_extmark(l:bufnr, l:id_vt_fim, s:pos_y - 1, s:pos_x - 1, { + \ 'virt_text': [[s:content[0], 'llama_hl_hint'], [l:info, 'llama_hl_info']], + \ 'virt_text_win_col': virtcol('.') - 1 + \ }) - call nvim_buf_set_extmark(l:bufnr, l:id_vt_fim, s:pos_y - 1, 0, { - \ 'virt_lines': map(s:content[1:], {idx, val -> [[val, 'llama_hl_hint']]}), - \ 'virt_text_win_col': virtcol('.') - \ }) + call nvim_buf_set_extmark(l:bufnr, l:id_vt_fim, s:pos_y - 1, 0, { + \ 'virt_lines': map(s:content[1:], {idx, val -> [[val, 'llama_hl_hint']]}), + \ 'virt_text_win_col': virtcol('.') + \ }) + elseif s:vim_ghost_text + " adapted from: + " https://github.com/github/copilot.vim/blob/release/autoload/copilot.vim + let l:text = s:content + let l:new_suffix = s:content[0] + let l:current_suffix = getline('.')[col('.') - 1 :] + let l:inset = '' + while !empty(l:new_suffix) + let last_char = matchstr(l:new_suffix, '.$') + let l:new_suffix = matchstr(l:new_suffix, '^.\{-\}\ze.$') + if last_char ==# matchstr(l:current_suffix, '.$') + if !empty(l:inset) + call prop_add(line('.'), col('.') + len(l:current_suffix), {'type': s:hlgroup, 'text': l:inset}) + let l:inset = '' + endif + let l:current_suffix = matchstr(l:current_suffix, '^.\{-\}\ze.$') + else + let l:inset = last_char . l:inset + endif + endwhile + if !empty(l:new_suffix . l:inset) + call prop_add(line('.'), col('.'), {'type': s:hint_hlgroup, 'text': l:new_suffix . l:inset}) + endif + for line in l:text[1:] + call prop_add(line('.'), 0, {'type': s:hint_hlgroup, 'text_align': 'below', 'text_padding_left': s:get_indent(line), 'text': l:new_suffix . line}) + endfor + if !empty(l:info) + call prop_add(line('.'), 0, {'type': s:info_hlgroup, 'text': ' ' . l:info, 'text_wrap': 'truncate', 'text_padding_left': col('$')}) + endif + endif " setup accept shortcuts inoremap :call llama#fim_accept(v:false) @@ -688,10 +779,14 @@ function! s:fim_on_stdout(job_id, data, event) dict let s:hint_shown = v:true endfunction -function! s:fim_on_exit(job_id, exit_code, event) dict +function! s:nvim_fim_on_exit(job_id, exit_code, event) dict if a:exit_code != 0 echom "Job failed with exit code: " . a:exit_code endif let s:current_job = v:null endfunction + +function! s:vim_fim_on_exit(job_id) + let s:current_job = v:null +endfunction From 2dcbb00e0664a886af495b1edfec19f2e0263284 Mon Sep 17 00:00:00 2001 From: Michael Coppola Date: Tue, 22 Oct 2024 02:25:51 -0400 Subject: [PATCH 02/19] fixed ring update, removed blank line --- examples/llama.vim | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/examples/llama.vim b/examples/llama.vim index 7cc575a0396..155ffc913a9 100644 --- a/examples/llama.vim +++ b/examples/llama.vim @@ -347,10 +347,15 @@ function! s:ring_update() \ 't_max_predict_ms': 1 \ }) - let l:curl_command = printf( - \ "curl --silent --no-buffer --request POST --url %s --header \"Content-Type: application/json\" --data %s", - \ g:llama_config.endpoint, shellescape(l:request) - \ ) + let l:curl_command = [ + \ "curl", + \ "--silent", + \ "--no-buffer", + \ "--request", "POST", + \ "--url", g:llama_config.endpoint, + \ "--header", "Content-Type: application/json", + \ "--data", l:request + \ ] " no callbacks because we don't need to process the response if s:nvim_ghost_text @@ -462,7 +467,6 @@ function! llama#fim(is_auto) abort \ "--data", l:request \ ] - if s:current_job != v:null if s:nvim_ghost_text call jobstop(s:current_job) From 09cbb43bcbae3cb523305508679037bff26f003a Mon Sep 17 00:00:00 2001 From: Michael Coppola Date: Tue, 22 Oct 2024 02:29:11 -0400 Subject: [PATCH 03/19] minor --- examples/llama.vim | 1 - 1 file changed, 1 deletion(-) diff --git a/examples/llama.vim b/examples/llama.vim index 155ffc913a9..f1c4e924caa 100644 --- a/examples/llama.vim +++ b/examples/llama.vim @@ -122,7 +122,6 @@ function! s:get_indent(str) return l:count endfunction - function! s:rand(i0, i1) abort return a:i0 + rand() % (a:i1 - a:i0 + 1) endfunction From 1713d34392dc545591990f8030951f88a1c2c705 Mon Sep 17 00:00:00 2001 From: Michael Coppola Date: Tue, 22 Oct 2024 02:36:26 -0400 Subject: [PATCH 04/19] minor --- examples/llama.vim | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/examples/llama.vim b/examples/llama.vim index f1c4e924caa..2ecf0c29ab3 100644 --- a/examples/llama.vim +++ b/examples/llama.vim @@ -745,8 +745,8 @@ function! s:fim_on_stdout(pos_x, pos_y, is_auto, job_id, data, event = 0) \ 'virt_text_win_col': virtcol('.') \ }) elseif s:vim_ghost_text - " adapted from: - " https://github.com/github/copilot.vim/blob/release/autoload/copilot.vim + " adapted from: + " https://github.com/github/copilot.vim/blob/release/autoload/copilot.vim let l:text = s:content let l:new_suffix = s:content[0] let l:current_suffix = getline('.')[col('.') - 1 :] @@ -756,7 +756,10 @@ function! s:fim_on_stdout(pos_x, pos_y, is_auto, job_id, data, event = 0) let l:new_suffix = matchstr(l:new_suffix, '^.\{-\}\ze.$') if last_char ==# matchstr(l:current_suffix, '.$') if !empty(l:inset) - call prop_add(line('.'), col('.') + len(l:current_suffix), {'type': s:hlgroup, 'text': l:inset}) + call prop_add(line('.'), col('.') + len(l:current_suffix), { + \ 'type': s:hlgroup, + \ 'text': l:inset + \ }) let l:inset = '' endif let l:current_suffix = matchstr(l:current_suffix, '^.\{-\}\ze.$') @@ -765,13 +768,26 @@ function! s:fim_on_stdout(pos_x, pos_y, is_auto, job_id, data, event = 0) endif endwhile if !empty(l:new_suffix . l:inset) - call prop_add(line('.'), col('.'), {'type': s:hint_hlgroup, 'text': l:new_suffix . l:inset}) + call prop_add(line('.'), col('.'), { + \ 'type': s:hint_hlgroup, + \ 'text': l:new_suffix . l:inset + \ }) endif for line in l:text[1:] - call prop_add(line('.'), 0, {'type': s:hint_hlgroup, 'text_align': 'below', 'text_padding_left': s:get_indent(line), 'text': l:new_suffix . line}) + call prop_add(line('.'), 0, { + \ 'type': s:hint_hlgroup, + \ 'text': l:new_suffix . line, + \ 'text_padding_left': s:get_indent(line), + \ 'text_align': 'below' + \ }) endfor if !empty(l:info) - call prop_add(line('.'), 0, {'type': s:info_hlgroup, 'text': ' ' . l:info, 'text_wrap': 'truncate', 'text_padding_left': col('$')}) + call prop_add(line('.'), 0, { + \ 'type': s:info_hlgroup, + \ 'text': ' ' . l:info, + \ 'text_padding_left': col('$'), + \ 'text_wrap': 'truncate' + \ }) endif endif From 3402ab812f2c284c45612b8c2c106e5661598188 Mon Sep 17 00:00:00 2001 From: Michael Coppola Date: Tue, 22 Oct 2024 02:47:03 -0400 Subject: [PATCH 05/19] minor doc update --- examples/llama.vim | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/llama.vim b/examples/llama.vim index 2ecf0c29ab3..7a1e844b1a1 100644 --- a/examples/llama.vim +++ b/examples/llama.vim @@ -2,7 +2,7 @@ " " requires: " -" - neovim +" - neovim or vim " - curl " - llama.cpp server instance " - FIM-compatible model @@ -10,7 +10,7 @@ " sample config: " " - Tab - accept the current suggestion -" - Shift+Tab - accept just the first line of the segguestion +" - Shift+Tab - accept just the first line of the suggestion " - Ctrl+F - toggle FIM completion manually " " make symlink or copy this file to ~/.config/nvim/autoload/llama.vim From edaa930f84e3504fdd24b33d45c0789b91784aaa Mon Sep 17 00:00:00 2001 From: Michael Coppola Date: Tue, 22 Oct 2024 02:56:24 -0400 Subject: [PATCH 06/19] removed uneeded var --- examples/llama.vim | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/examples/llama.vim b/examples/llama.vim index 7a1e844b1a1..dc1bb6c6134 100644 --- a/examples/llama.vim +++ b/examples/llama.vim @@ -747,7 +747,6 @@ function! s:fim_on_stdout(pos_x, pos_y, is_auto, job_id, data, event = 0) elseif s:vim_ghost_text " adapted from: " https://github.com/github/copilot.vim/blob/release/autoload/copilot.vim - let l:text = s:content let l:new_suffix = s:content[0] let l:current_suffix = getline('.')[col('.') - 1 :] let l:inset = '' @@ -773,7 +772,7 @@ function! s:fim_on_stdout(pos_x, pos_y, is_auto, job_id, data, event = 0) \ 'text': l:new_suffix . l:inset \ }) endif - for line in l:text[1:] + for line in s:content[1:] call prop_add(line('.'), 0, { \ 'type': s:hint_hlgroup, \ 'text': l:new_suffix . line, From 4f6919ce2edd732d22e9326073c1905a02ecd082 Mon Sep 17 00:00:00 2001 From: Michael Coppola Date: Tue, 22 Oct 2024 03:07:34 -0400 Subject: [PATCH 07/19] minor --- examples/llama.vim | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/examples/llama.vim b/examples/llama.vim index dc1bb6c6134..b79d4f4fee4 100644 --- a/examples/llama.vim +++ b/examples/llama.vim @@ -482,11 +482,11 @@ function! llama#fim(is_auto) abort \ 'stdout_buffered': v:true \ }) elseif s:vim_ghost_text - let s:current_job = job_start(l:curl_command, { - \ 'out_cb': function('s:fim_on_stdout', [s:pos_x, s:pos_y, a:is_auto]), - \ 'close_cb': function('s:vim_fim_on_exit'), - \ 'out_io': 'buffer' - \ }) + let s:current_job = job_start(l:curl_command, { + \ 'out_cb': function('s:fim_on_stdout', [s:pos_x, s:pos_y, a:is_auto]), + \ 'close_cb': function('s:vim_fim_on_exit'), + \ 'out_io': 'buffer' + \ }) endif " TODO: per-file location From 36ab2743318700cb9ce5e481fe4edcaf5a763357 Mon Sep 17 00:00:00 2001 From: Michael Coppola Date: Tue, 22 Oct 2024 03:10:19 -0400 Subject: [PATCH 08/19] minor --- examples/llama.vim | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/examples/llama.vim b/examples/llama.vim index b79d4f4fee4..d85237eeed9 100644 --- a/examples/llama.vim +++ b/examples/llama.vim @@ -482,11 +482,11 @@ function! llama#fim(is_auto) abort \ 'stdout_buffered': v:true \ }) elseif s:vim_ghost_text - let s:current_job = job_start(l:curl_command, { - \ 'out_cb': function('s:fim_on_stdout', [s:pos_x, s:pos_y, a:is_auto]), - \ 'close_cb': function('s:vim_fim_on_exit'), - \ 'out_io': 'buffer' - \ }) + let s:current_job = job_start(l:curl_command, { + \ 'out_cb': function('s:fim_on_stdout', [s:pos_x, s:pos_y, a:is_auto]), + \ 'close_cb': function('s:vim_fim_on_exit'), + \ 'out_io': 'buffer' + \ }) endif " TODO: per-file location From d7d18bff227c4c5b4bfaf3ec83acabb17bd5a9b8 Mon Sep 17 00:00:00 2001 From: Michael Coppola Date: Tue, 22 Oct 2024 09:39:11 -0400 Subject: [PATCH 09/19] fixed job_start creating new scratch buffers --- examples/llama.vim | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/examples/llama.vim b/examples/llama.vim index d85237eeed9..deaa73fd61b 100644 --- a/examples/llama.vim +++ b/examples/llama.vim @@ -484,8 +484,7 @@ function! llama#fim(is_auto) abort elseif s:vim_ghost_text let s:current_job = job_start(l:curl_command, { \ 'out_cb': function('s:fim_on_stdout', [s:pos_x, s:pos_y, a:is_auto]), - \ 'close_cb': function('s:vim_fim_on_exit'), - \ 'out_io': 'buffer' + \ 'close_cb': function('s:vim_fim_on_exit') \ }) endif From e30b4c9d6a615cdad8d7d8636b380d9bdcce145e Mon Sep 17 00:00:00 2001 From: Michael Coppola Date: Tue, 22 Oct 2024 09:45:21 -0400 Subject: [PATCH 10/19] fixed job_start creating new scratch buffers --- examples/llama.vim | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/examples/llama.vim b/examples/llama.vim index d85237eeed9..deaa73fd61b 100644 --- a/examples/llama.vim +++ b/examples/llama.vim @@ -484,8 +484,7 @@ function! llama#fim(is_auto) abort elseif s:vim_ghost_text let s:current_job = job_start(l:curl_command, { \ 'out_cb': function('s:fim_on_stdout', [s:pos_x, s:pos_y, a:is_auto]), - \ 'close_cb': function('s:vim_fim_on_exit'), - \ 'out_io': 'buffer' + \ 'close_cb': function('s:vim_fim_on_exit') \ }) endif From b81721b523149b7c9c6a9f4afccfb0aad86554a4 Mon Sep 17 00:00:00 2001 From: Michael Coppola Date: Tue, 22 Oct 2024 10:35:17 -0400 Subject: [PATCH 11/19] fixed ghost text indenting when expandtab is on --- examples/llama.vim | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/examples/llama.vim b/examples/llama.vim index deaa73fd61b..2fbe43e0294 100644 --- a/examples/llama.vim +++ b/examples/llama.vim @@ -112,9 +112,7 @@ function! s:get_indent(str) let l:count = 0 for i in range(len(a:str)) if a:str[i] == "\t" - let l:count += &shiftwidth - 1 - elseif a:str[i] == " " - let l:count += 1 + let l:count += &tabstop - 1 else break endif From 39c3cd41d5300d0601425233e283e4d2ad4ddc94 Mon Sep 17 00:00:00 2001 From: Michael Coppola Date: Tue, 22 Oct 2024 11:28:09 -0400 Subject: [PATCH 12/19] removed unused code --- examples/llama.vim | 26 +++----------------------- 1 file changed, 3 insertions(+), 23 deletions(-) diff --git a/examples/llama.vim b/examples/llama.vim index 2fbe43e0294..53222ea1278 100644 --- a/examples/llama.vim +++ b/examples/llama.vim @@ -742,37 +742,17 @@ function! s:fim_on_stdout(pos_x, pos_y, is_auto, job_id, data, event = 0) \ 'virt_text_win_col': virtcol('.') \ }) elseif s:vim_ghost_text - " adapted from: - " https://github.com/github/copilot.vim/blob/release/autoload/copilot.vim let l:new_suffix = s:content[0] - let l:current_suffix = getline('.')[col('.') - 1 :] - let l:inset = '' - while !empty(l:new_suffix) - let last_char = matchstr(l:new_suffix, '.$') - let l:new_suffix = matchstr(l:new_suffix, '^.\{-\}\ze.$') - if last_char ==# matchstr(l:current_suffix, '.$') - if !empty(l:inset) - call prop_add(line('.'), col('.') + len(l:current_suffix), { - \ 'type': s:hlgroup, - \ 'text': l:inset - \ }) - let l:inset = '' - endif - let l:current_suffix = matchstr(l:current_suffix, '^.\{-\}\ze.$') - else - let l:inset = last_char . l:inset - endif - endwhile - if !empty(l:new_suffix . l:inset) + if !empty(l:new_suffix) call prop_add(line('.'), col('.'), { \ 'type': s:hint_hlgroup, - \ 'text': l:new_suffix . l:inset + \ 'text': l:new_suffix \ }) endif for line in s:content[1:] call prop_add(line('.'), 0, { \ 'type': s:hint_hlgroup, - \ 'text': l:new_suffix . line, + \ 'text': line, \ 'text_padding_left': s:get_indent(line), \ 'text_align': 'below' \ }) From b39e65fa680f18fb345aa77f99de52956c969e6d Mon Sep 17 00:00:00 2001 From: Michael Coppola Date: Tue, 22 Oct 2024 11:32:48 -0400 Subject: [PATCH 13/19] minor --- examples/llama.vim | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/llama.vim b/examples/llama.vim index 53222ea1278..50353f22ecc 100644 --- a/examples/llama.vim +++ b/examples/llama.vim @@ -91,6 +91,8 @@ let s:default_config = { \ 'ring_update_ms': 1000, \ } +let g:llama_config = get(g:, 'llama_config', s:default_config) + let s:nvim_ghost_text = exists('*nvim_buf_get_mark') let s:vim_ghost_text = has('textprop') @@ -106,8 +108,6 @@ if s:vim_ghost_text endif endif -let g:llama_config = get(g:, 'llama_config', s:default_config) - function! s:get_indent(str) let l:count = 0 for i in range(len(a:str)) From bf9c4ccdc5ac3aacf8b90e5e4c3c2242fe14a596 Mon Sep 17 00:00:00 2001 From: Michael Coppola Date: Tue, 22 Oct 2024 11:45:24 -0400 Subject: [PATCH 14/19] unified fim_on_exit --- examples/llama.vim | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/examples/llama.vim b/examples/llama.vim index 50353f22ecc..c18a9622cdf 100644 --- a/examples/llama.vim +++ b/examples/llama.vim @@ -476,13 +476,13 @@ function! llama#fim(is_auto) abort if s:nvim_ghost_text let s:current_job = jobstart(l:curl_command, { \ 'on_stdout': function('s:fim_on_stdout', [s:pos_x, s:pos_y, a:is_auto]), - \ 'on_exit': function('s:nvim_fim_on_exit'), + \ 'on_exit': function('s:fim_on_exit'), \ 'stdout_buffered': v:true \ }) elseif s:vim_ghost_text let s:current_job = job_start(l:curl_command, { \ 'out_cb': function('s:fim_on_stdout', [s:pos_x, s:pos_y, a:is_auto]), - \ 'close_cb': function('s:vim_fim_on_exit') + \ 'exit_cb': function('s:fim_on_exit') \ }) endif @@ -774,14 +774,10 @@ function! s:fim_on_stdout(pos_x, pos_y, is_auto, job_id, data, event = 0) let s:hint_shown = v:true endfunction -function! s:nvim_fim_on_exit(job_id, exit_code, event) dict +function! s:fim_on_exit(job_id, exit_code, event = v:null) if a:exit_code != 0 echom "Job failed with exit code: " . a:exit_code endif let s:current_job = v:null endfunction - -function! s:vim_fim_on_exit(job_id) - let s:current_job = v:null -endfunction From cc961965e1df1d0ce46e6f6fefdb944e83a3afeb Mon Sep 17 00:00:00 2001 From: Michael Coppola Date: Tue, 22 Oct 2024 11:48:01 -0400 Subject: [PATCH 15/19] minor --- examples/llama.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/llama.vim b/examples/llama.vim index c18a9622cdf..315d6ffb07a 100644 --- a/examples/llama.vim +++ b/examples/llama.vim @@ -552,7 +552,7 @@ function! s:on_move() endfunction " callback that processes the FIM result from the server and displays the suggestion -function! s:fim_on_stdout(pos_x, pos_y, is_auto, job_id, data, event = 0) +function! s:fim_on_stdout(pos_x, pos_y, is_auto, job_id, data, event = v:null) if s:nvim_ghost_text let l:raw = join(a:data, "\n") elseif s:vim_ghost_text From 5292d45f99b45bb9add781592ba3354f6229318c Mon Sep 17 00:00:00 2001 From: Michael Coppola Date: Tue, 22 Oct 2024 11:59:16 -0400 Subject: [PATCH 16/19] vim ghost text rendering now uses pos_x and pos_y parameters --- examples/llama.vim | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/llama.vim b/examples/llama.vim index 315d6ffb07a..d5c9916a8e3 100644 --- a/examples/llama.vim +++ b/examples/llama.vim @@ -744,13 +744,13 @@ function! s:fim_on_stdout(pos_x, pos_y, is_auto, job_id, data, event = v:null) elseif s:vim_ghost_text let l:new_suffix = s:content[0] if !empty(l:new_suffix) - call prop_add(line('.'), col('.'), { + call prop_add(s:pos_y, s:pos_x + 1, { \ 'type': s:hint_hlgroup, \ 'text': l:new_suffix \ }) endif for line in s:content[1:] - call prop_add(line('.'), 0, { + call prop_add(s:pos_y, 0, { \ 'type': s:hint_hlgroup, \ 'text': line, \ 'text_padding_left': s:get_indent(line), @@ -758,7 +758,7 @@ function! s:fim_on_stdout(pos_x, pos_y, is_auto, job_id, data, event = v:null) \ }) endfor if !empty(l:info) - call prop_add(line('.'), 0, { + call prop_add(s:pos_y, 0, { \ 'type': s:info_hlgroup, \ 'text': ' ' . l:info, \ 'text_padding_left': col('$'), From f946cbc42adcc771959c71db75a5d27f144e3fdc Mon Sep 17 00:00:00 2001 From: Michael Coppola Date: Tue, 22 Oct 2024 12:02:24 -0400 Subject: [PATCH 17/19] renamed *_hlgroup to hlgroup_* --- examples/llama.vim | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/examples/llama.vim b/examples/llama.vim index d5c9916a8e3..38c905428c2 100644 --- a/examples/llama.vim +++ b/examples/llama.vim @@ -97,14 +97,14 @@ let s:nvim_ghost_text = exists('*nvim_buf_get_mark') let s:vim_ghost_text = has('textprop') if s:vim_ghost_text - let s:hint_hlgroup = 'llama_hl_hint' - let s:info_hlgroup = 'llama_hl_info' + let s:hlgroup_hint = 'llama_hl_hint' + let s:hlgroup_info = 'llama_hl_info' - if empty(prop_type_get(s:hint_hlgroup)) - call prop_type_add(s:hint_hlgroup, {'highlight': s:hint_hlgroup}) + if empty(prop_type_get(s:hlgroup_hint)) + call prop_type_add(s:hlgroup_hint, {'highlight': s:hlgroup_hint}) endif - if empty(prop_type_get(s:info_hlgroup)) - call prop_type_add(s:info_hlgroup, {'highlight': s:info_hlgroup}) + if empty(prop_type_get(s:hlgroup_info)) + call prop_type_add(s:hlgroup_info, {'highlight': s:hlgroup_info}) endif endif @@ -535,8 +535,8 @@ function! llama#fim_cancel() let l:id_vt_fim = nvim_create_namespace('vt_fim') call nvim_buf_clear_namespace(l:bufnr, l:id_vt_fim, 0, -1) elseif s:vim_ghost_text - call prop_remove({'type': s:hint_hlgroup, 'all': v:true}) - call prop_remove({'type': s:info_hlgroup, 'all': v:true}) + call prop_remove({'type': s:hlgroup_hint, 'all': v:true}) + call prop_remove({'type': s:hlgroup_info, 'all': v:true}) endif " remove the mappings @@ -745,13 +745,13 @@ function! s:fim_on_stdout(pos_x, pos_y, is_auto, job_id, data, event = v:null) let l:new_suffix = s:content[0] if !empty(l:new_suffix) call prop_add(s:pos_y, s:pos_x + 1, { - \ 'type': s:hint_hlgroup, + \ 'type': s:hlgroup_hint, \ 'text': l:new_suffix \ }) endif for line in s:content[1:] call prop_add(s:pos_y, 0, { - \ 'type': s:hint_hlgroup, + \ 'type': s:hlgroup_hint, \ 'text': line, \ 'text_padding_left': s:get_indent(line), \ 'text_align': 'below' @@ -759,7 +759,7 @@ function! s:fim_on_stdout(pos_x, pos_y, is_auto, job_id, data, event = v:null) endfor if !empty(l:info) call prop_add(s:pos_y, 0, { - \ 'type': s:info_hlgroup, + \ 'type': s:hlgroup_info, \ 'text': ' ' . l:info, \ 'text_padding_left': col('$'), \ 'text_wrap': 'truncate' From 85cea66dbbc6f7a8d39f984fb5320e277b0249a0 Mon Sep 17 00:00:00 2001 From: Michael Coppola Date: Tue, 22 Oct 2024 12:10:35 -0400 Subject: [PATCH 18/19] renamed *_ghost_text to ghost_text_*, moved nvim/vim detection to llama#init() --- examples/llama.vim | 56 +++++++++++++++++++++++----------------------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/examples/llama.vim b/examples/llama.vim index 38c905428c2..db1edc0fda2 100644 --- a/examples/llama.vim +++ b/examples/llama.vim @@ -93,21 +93,6 @@ let s:default_config = { let g:llama_config = get(g:, 'llama_config', s:default_config) -let s:nvim_ghost_text = exists('*nvim_buf_get_mark') -let s:vim_ghost_text = has('textprop') - -if s:vim_ghost_text - let s:hlgroup_hint = 'llama_hl_hint' - let s:hlgroup_info = 'llama_hl_info' - - if empty(prop_type_get(s:hlgroup_hint)) - call prop_type_add(s:hlgroup_hint, {'highlight': s:hlgroup_hint}) - endif - if empty(prop_type_get(s:hlgroup_info)) - call prop_type_add(s:hlgroup_info, {'highlight': s:hlgroup_info}) - endif -endif - function! s:get_indent(str) let l:count = 0 for i in range(len(a:str)) @@ -156,6 +141,21 @@ function! llama#init() let s:current_job = v:null + let s:ghost_text_nvim = exists('*nvim_buf_get_mark') + let s:ghost_text_vim = has('textprop') + + if s:ghost_text_vim + let s:hlgroup_hint = 'llama_hl_hint' + let s:hlgroup_info = 'llama_hl_info' + + if empty(prop_type_get(s:hlgroup_hint)) + call prop_type_add(s:hlgroup_hint, {'highlight': s:hlgroup_hint}) + endif + if empty(prop_type_get(s:hlgroup_info)) + call prop_type_add(s:hlgroup_info, {'highlight': s:hlgroup_info}) + endif + endif + augroup llama autocmd! autocmd InsertEnter * inoremap llama#fim_inline(v:false) @@ -355,9 +355,9 @@ function! s:ring_update() \ ] " no callbacks because we don't need to process the response - if s:nvim_ghost_text + if s:ghost_text_nvim call jobstart(l:curl_command, {}) - elseif s:vim_ghost_text + elseif s:ghost_text_vim call job_start(l:curl_command, {}) endif endfunction @@ -465,21 +465,21 @@ function! llama#fim(is_auto) abort \ ] if s:current_job != v:null - if s:nvim_ghost_text + if s:ghost_text_nvim call jobstop(s:current_job) - elseif s:vim_ghost_text + elseif s:ghost_text_vim call job_stop(s:current_job) endif endif " send the request asynchronously - if s:nvim_ghost_text + if s:ghost_text_nvim let s:current_job = jobstart(l:curl_command, { \ 'on_stdout': function('s:fim_on_stdout', [s:pos_x, s:pos_y, a:is_auto]), \ 'on_exit': function('s:fim_on_exit'), \ 'stdout_buffered': v:true \ }) - elseif s:vim_ghost_text + elseif s:ghost_text_vim let s:current_job = job_start(l:curl_command, { \ 'out_cb': function('s:fim_on_stdout', [s:pos_x, s:pos_y, a:is_auto]), \ 'exit_cb': function('s:fim_on_exit') @@ -531,10 +531,10 @@ function! llama#fim_cancel() " clear the virtual text let l:bufnr = bufnr('%') - if s:nvim_ghost_text + if s:ghost_text_nvim let l:id_vt_fim = nvim_create_namespace('vt_fim') call nvim_buf_clear_namespace(l:bufnr, l:id_vt_fim, 0, -1) - elseif s:vim_ghost_text + elseif s:ghost_text_vim call prop_remove({'type': s:hlgroup_hint, 'all': v:true}) call prop_remove({'type': s:hlgroup_info, 'all': v:true}) endif @@ -553,9 +553,9 @@ endfunction " callback that processes the FIM result from the server and displays the suggestion function! s:fim_on_stdout(pos_x, pos_y, is_auto, job_id, data, event = v:null) - if s:nvim_ghost_text + if s:ghost_text_nvim let l:raw = join(a:data, "\n") - elseif s:vim_ghost_text + elseif s:ghost_text_vim let l:raw = a:data endif @@ -700,7 +700,7 @@ function! s:fim_on_stdout(pos_x, pos_y, is_auto, job_id, data, event = v:null) " display virtual text with the suggestion let l:bufnr = bufnr('%') - if s:nvim_ghost_text + if s:ghost_text_nvim let l:id_vt_fim = nvim_create_namespace('vt_fim') endif @@ -731,7 +731,7 @@ function! s:fim_on_stdout(pos_x, pos_y, is_auto, job_id, data, event = v:null) endif " display the suggestion and append the info to the end of the first line - if s:nvim_ghost_text + if s:ghost_text_nvim call nvim_buf_set_extmark(l:bufnr, l:id_vt_fim, s:pos_y - 1, s:pos_x - 1, { \ 'virt_text': [[s:content[0], 'llama_hl_hint'], [l:info, 'llama_hl_info']], \ 'virt_text_win_col': virtcol('.') - 1 @@ -741,7 +741,7 @@ function! s:fim_on_stdout(pos_x, pos_y, is_auto, job_id, data, event = v:null) \ 'virt_lines': map(s:content[1:], {idx, val -> [[val, 'llama_hl_hint']]}), \ 'virt_text_win_col': virtcol('.') \ }) - elseif s:vim_ghost_text + elseif s:ghost_text_vim let l:new_suffix = s:content[0] if !empty(l:new_suffix) call prop_add(s:pos_y, s:pos_x + 1, { From 7d6ef40c19fd39813e5a83ebe9960bda7d5ddddf Mon Sep 17 00:00:00 2001 From: Michael Coppola Date: Tue, 22 Oct 2024 12:23:35 -0400 Subject: [PATCH 19/19] minor --- examples/llama.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/llama.vim b/examples/llama.vim index db1edc0fda2..4bc26d4e936 100644 --- a/examples/llama.vim +++ b/examples/llama.vim @@ -760,7 +760,7 @@ function! s:fim_on_stdout(pos_x, pos_y, is_auto, job_id, data, event = v:null) if !empty(l:info) call prop_add(s:pos_y, 0, { \ 'type': s:hlgroup_info, - \ 'text': ' ' . l:info, + \ 'text': l:info, \ 'text_padding_left': col('$'), \ 'text_wrap': 'truncate' \ })