Skip to content

Commit c1444e0

Browse files
committed
llama.vim : fix edge cases
1 parent 012f880 commit c1444e0

File tree

1 file changed

+29
-15
lines changed

1 file changed

+29
-15
lines changed

examples/llama.vim

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,9 @@ function! llama#fim(is_auto) abort
9898
\ 'infill_p_eog': 0.001,
9999
\ 'stream': v:false,
100100
\ 'samplers': ["top_k", "infill"],
101+
"\ 'cache_prompt': v:true,
101102
\ 't_max_prompt_ms': g:llama_config.t_max_prompt_ms,
102-
\ 't_max_predict_ms': g:llama_config.t_max_predict_ms,
103-
\ 'cache_prompt': v:true
103+
\ 't_max_predict_ms': g:llama_config.t_max_predict_ms
104104
\ })
105105

106106
let l:curl_command = printf(
@@ -111,10 +111,21 @@ function! llama#fim(is_auto) abort
111111
" send the request asynchronously
112112
let s:current_job = jobstart(l:curl_command, {
113113
\ 'on_stdout': function('s:fim_on_stdout'),
114-
\ 'on_exit': function('s:fim_on_exit'),
114+
\ 'on_exit': function('s:fim_on_exit'),
115115
\ 'stdout_buffered': v:true,
116116
\ 'is_auto': a:is_auto
117117
\ })
118+
119+
" this trick is needed to avoid the cursor shifting upon C-O when at the end of the line
120+
if !a:is_auto
121+
augroup llama_insert
122+
autocmd!
123+
augroup END
124+
125+
if g:llama_config.auto_fim
126+
call timer_start(0, {-> s:fim_auto_enable()})
127+
endif
128+
endif
118129
endfunction
119130

120131
function! llama#fim_accept()
@@ -151,9 +162,16 @@ function! llama#fim_cancel()
151162

152163
augroup llama_insert
153164
autocmd!
154-
if g:llama_config.auto_fim
155-
autocmd CursorMovedI * call s:fim_auto()
156-
endif
165+
augroup END
166+
167+
if g:llama_config.auto_fim
168+
call s:fim_auto_enable()
169+
endif
170+
endfunction
171+
172+
function! s:fim_auto_enable()
173+
augroup llama_insert
174+
autocmd CursorMovedI * call s:fim_auto()
157175
augroup END
158176
endfunction
159177

@@ -176,6 +194,9 @@ endfunction
176194

177195
function! s:fim_on_stdout(job_id, data, event) dict
178196
let l:raw = join(a:data, "\n")
197+
if len(l:raw) == 0
198+
return
199+
endif
179200

180201
let s:can_accept = v:true
181202
let l:has_info = v:false
@@ -195,13 +216,6 @@ function! s:fim_on_stdout(job_id, data, event) dict
195216
let s:can_accept = v:false
196217
endif
197218

198-
if s:can_accept && l:raw == ""
199-
if !self.is_auto
200-
call add(s:content, "<| empty response: is the server on? |>")
201-
endif
202-
let s:can_accept = v:false
203-
endif
204-
205219
" get the generated suggestion
206220
if s:can_accept
207221
let l:response = json_decode(l:raw)
@@ -232,7 +246,7 @@ function! s:fim_on_stdout(job_id, data, event) dict
232246

233247
if len(s:content) == 0
234248
if !self.is_auto
235-
call add(s:content, "<| nothing to suggest |>")
249+
call add(s:content, "<| EOT |>")
236250
endif
237251
let s:can_accept = v:false
238252
endif
@@ -272,7 +286,7 @@ function! s:fim_on_stdout(job_id, data, event) dict
272286

273287
call nvim_buf_set_extmark(l:bufnr, l:id_vt_fim, s:pos_y - 1, s:pos_x - 1, {
274288
\ 'virt_text': [[s:content[0], 'llama_hl_hint']],
275-
\ 'virt_text_win_col': s:pos_x == len(s:line_cur) ? virtcol('.') : virtcol('.') - 1
289+
\ 'virt_text_win_col': virtcol('.') - 1
276290
\ })
277291

278292
call nvim_buf_set_extmark(l:bufnr, l:id_vt_fim, s:pos_y - 1, 0, {

0 commit comments

Comments
 (0)