@@ -667,86 +667,6 @@ function! llama#fim(pos_x, pos_y, is_auto, prev, use_cache) abort
667667 endif
668668endfunction
669669
670- " if accept_type == 'full', accept entire response
671- " if accept_type == 'line', accept only the first line of the response
672- " if accept_type == 'word', accept only the first word of the response
673- function ! llama#fim_accept (accept_type)
674- let l: pos_x = s: fim_data [' pos_x' ]
675- let l: pos_y = s: fim_data [' pos_y' ]
676-
677- let l: line_cur = s: fim_data [' line_cur' ]
678-
679- let l: can_accept = s: fim_data [' can_accept' ]
680- let l: content = s: fim_data [' content' ]
681-
682- if l: can_accept && len (l: content ) > 0
683- " insert suggestion on current line
684- if a: accept_type != ' word'
685- " insert first line of suggestion
686- call setline (l: pos_y , l: line_cur [:(l: pos_x - 1 )] . l: content [0 ])
687- else
688- " insert first word of suggestion
689- let l: suffix = l: line_cur [(l: pos_x ):]
690- let l: word = matchstr (l: content [0 ][:- (len (l: suffix ) + 1 )], ' ^\s*\S\+' )
691- call setline (l: pos_y , l: line_cur [:(l: pos_x - 1 )] . l: word . l: suffix )
692- endif
693-
694- " insert rest of suggestion
695- if len (l: content ) > 1 && a: accept_type == ' full'
696- call append (l: pos_y , l: content [1 :-1 ])
697- endif
698-
699- " move cusor
700- if a: accept_type == ' word'
701- " move cursor to end of word
702- call cursor (l: pos_y , l: pos_x + len (l: word ) + 1 )
703- elseif a: accept_type == ' line' || len (l: content ) == 1
704- " move cursor for 1-line suggestion
705- call cursor (l: pos_y , l: pos_x + len (l: content [0 ]) + 1 )
706- if len (l: content ) > 2
707- " simulate pressing Enter to move to next line
708- call feedkeys (" \<CR> " )
709- endif
710- else
711- " move cursor for multi-line suggestion
712- call cursor (l: pos_y + len (l: content ) - 1 , len (l: content [-1 ]) + 1 )
713- endif
714- endif
715-
716- call llama#fim_hide ()
717- endfunction
718-
719- function ! llama#fim_hide ()
720- let s: hint_shown = v: false
721-
722- " clear the virtual text
723- let l: bufnr = bufnr (' %' )
724-
725- if s: ghost_text_nvim
726- let l: id_vt_fim = nvim_create_namespace (' vt_fim' )
727- call nvim_buf_clear_namespace (l: bufnr , l: id_vt_fim , 0 , -1 )
728- elseif s: ghost_text_vim
729- call prop_remove ({' type' : s: hlgroup_hint , ' all' : v: true })
730- call prop_remove ({' type' : s: hlgroup_info , ' all' : v: true })
731- endif
732-
733- " remove the mappings
734- exe ' silent! iunmap <buffer> ' . g: llama_config .keymap_accept_full
735- exe ' silent! iunmap <buffer> ' . g: llama_config .keymap_accept_line
736- exe ' silent! iunmap <buffer> ' . g: llama_config .keymap_accept_word
737- endfunction
738-
739- function ! s: on_move ()
740- let s: t_last_move = reltime ()
741-
742- call llama#fim_hide ()
743-
744- let l: pos_x = col (' .' ) - 1
745- let l: pos_y = line (' .' )
746-
747- call s: fim_try_hint (l: pos_x , l: pos_y )
748- endfunction
749-
750670" callback that processes the FIM result from the server
751671function ! s: fim_on_response (hashes, job_id, data, event = v: null )
752672 if s: ghost_text_nvim
@@ -774,6 +694,25 @@ function! s:fim_on_response(hashes, job_id, data, event = v:null)
774694 endif
775695endfunction
776696
697+ function ! s: fim_on_exit (job_id, exit_code, event = v: null )
698+ if a: exit_code != 0
699+ echom " Job failed with exit code: " . a: exit_code
700+ endif
701+
702+ let s: current_job = v: null
703+ endfunction
704+
705+ function ! s: on_move ()
706+ let s: t_last_move = reltime ()
707+
708+ call llama#fim_hide ()
709+
710+ let l: pos_x = col (' .' ) - 1
711+ let l: pos_y = line (' .' )
712+
713+ call s: fim_try_hint (l: pos_x , l: pos_y )
714+ endfunction
715+
777716" try to generate a suggestion using the data in the cache
778717function ! s: fim_try_hint (pos_x, pos_y)
779718 " show the suggestion only in insert mode
@@ -1065,10 +1004,71 @@ function! s:fim_render(pos_x, pos_y, data)
10651004 let s: fim_data [' content' ] = l: content
10661005endfunction
10671006
1068- function ! s: fim_on_exit (job_id, exit_code, event = v: null )
1069- if a: exit_code != 0
1070- echom " Job failed with exit code: " . a: exit_code
1007+ " if accept_type == 'full', accept entire response
1008+ " if accept_type == 'line', accept only the first line of the response
1009+ " if accept_type == 'word', accept only the first word of the response
1010+ function ! llama#fim_accept (accept_type)
1011+ let l: pos_x = s: fim_data [' pos_x' ]
1012+ let l: pos_y = s: fim_data [' pos_y' ]
1013+
1014+ let l: line_cur = s: fim_data [' line_cur' ]
1015+
1016+ let l: can_accept = s: fim_data [' can_accept' ]
1017+ let l: content = s: fim_data [' content' ]
1018+
1019+ if l: can_accept && len (l: content ) > 0
1020+ " insert suggestion on current line
1021+ if a: accept_type != ' word'
1022+ " insert first line of suggestion
1023+ call setline (l: pos_y , l: line_cur [:(l: pos_x - 1 )] . l: content [0 ])
1024+ else
1025+ " insert first word of suggestion
1026+ let l: suffix = l: line_cur [(l: pos_x ):]
1027+ let l: word = matchstr (l: content [0 ][:- (len (l: suffix ) + 1 )], ' ^\s*\S\+' )
1028+ call setline (l: pos_y , l: line_cur [:(l: pos_x - 1 )] . l: word . l: suffix )
1029+ endif
1030+
1031+ " insert rest of suggestion
1032+ if len (l: content ) > 1 && a: accept_type == ' full'
1033+ call append (l: pos_y , l: content [1 :-1 ])
1034+ endif
1035+
1036+ " move cusor
1037+ if a: accept_type == ' word'
1038+ " move cursor to end of word
1039+ call cursor (l: pos_y , l: pos_x + len (l: word ) + 1 )
1040+ elseif a: accept_type == ' line' || len (l: content ) == 1
1041+ " move cursor for 1-line suggestion
1042+ call cursor (l: pos_y , l: pos_x + len (l: content [0 ]) + 1 )
1043+ if len (l: content ) > 2
1044+ " simulate pressing Enter to move to next line
1045+ call feedkeys (" \<CR> " )
1046+ endif
1047+ else
1048+ " move cursor for multi-line suggestion
1049+ call cursor (l: pos_y + len (l: content ) - 1 , len (l: content [-1 ]) + 1 )
1050+ endif
10711051 endif
10721052
1073- let s: current_job = v: null
1053+ call llama#fim_hide ()
1054+ endfunction
1055+
1056+ function ! llama#fim_hide ()
1057+ let s: hint_shown = v: false
1058+
1059+ " clear the virtual text
1060+ let l: bufnr = bufnr (' %' )
1061+
1062+ if s: ghost_text_nvim
1063+ let l: id_vt_fim = nvim_create_namespace (' vt_fim' )
1064+ call nvim_buf_clear_namespace (l: bufnr , l: id_vt_fim , 0 , -1 )
1065+ elseif s: ghost_text_vim
1066+ call prop_remove ({' type' : s: hlgroup_hint , ' all' : v: true })
1067+ call prop_remove ({' type' : s: hlgroup_info , ' all' : v: true })
1068+ endif
1069+
1070+ " remove the mappings
1071+ exe ' silent! iunmap <buffer> ' . g: llama_config .keymap_accept_full
1072+ exe ' silent! iunmap <buffer> ' . g: llama_config .keymap_accept_line
1073+ exe ' silent! iunmap <buffer> ' . g: llama_config .keymap_accept_word
10741074endfunction
0 commit comments