diff options
| author | Martin Bielik <mx.bielik@gmail.com> | 2023-04-20 19:36:08 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-04-20 19:36:08 +0200 |
| commit | 940fd18db7dec5e00abff48b84e0711cd513996d (patch) | |
| tree | cafc591f65bc3ba7f9092b56e9ad1758188f5222 /autoload | |
| parent | cdba3579d66956d356c424945a346a0f044facea (diff) | |
| parent | e4c3dad037a1950de5eee153c45c2eddfe0f8f7a (diff) | |
| download | vim-ai-940fd18db7dec5e00abff48b84e0711cd513996d.tar.gz | |
Merge pull request #22 from madox2/empty-response-elimination
Empty response elimination, closes #20
Diffstat (limited to '')
| -rw-r--r-- | autoload/vim_ai.vim | 41 | ||||
| -rw-r--r-- | autoload/vim_ai_config.vim | 3 |
2 files changed, 33 insertions, 11 deletions
diff --git a/autoload/vim_ai.vim b/autoload/vim_ai.vim index d1c27a9..69a0f95 100644 --- a/autoload/vim_ai.vim +++ b/autoload/vim_ai.vim @@ -44,25 +44,42 @@ function! vim_ai#MakeScratchWindow() endif endfunction -function! s:MakePrompt(is_selection, lines, instruction) +function! s:MakeSelectionPrompt(is_selection, lines, instruction, options) + let l:selection = "" + if a:instruction == "" + let l:selection = a:lines + elseif a:is_selection + let l:boundary = a:options['selection_boundary'] + if l:boundary != "" && match(a:lines, l:boundary) == -1 + " NOTE: surround selection with boundary (e.g. #####) in order to eliminate empty responses + let l:selection = l:boundary . "\n" . a:lines . "\n" . l:boundary + else + let l:selection = a:lines + endif + endif + return l:selection +endfunction + +function! s:MakePrompt(is_selection, lines, instruction, options) let l:lines = trim(join(a:lines, "\n")) let l:instruction = trim(a:instruction) let l:delimiter = l:instruction != "" && a:is_selection ? ":\n" : "" - let l:selection = a:is_selection || l:instruction == "" ? l:lines : "" + let l:selection = s:MakeSelectionPrompt(a:is_selection, l:lines, l:instruction, a:options) return join([l:instruction, l:delimiter, l:selection], "") endfunction function! vim_ai#AIRun(is_selection, ...) range + let l:engine = g:vim_ai_complete['engine'] + let l:options = g:vim_ai_complete['options'] + let l:instruction = a:0 ? a:1 : "" let l:lines = getline(a:firstline, a:lastline) - let l:prompt = s:MakePrompt(a:is_selection, l:lines, l:instruction) + let l:prompt = s:MakePrompt(a:is_selection, l:lines, l:instruction, l:options) let s:last_command = "complete" let s:last_instruction = l:instruction let s:last_is_selection = a:is_selection - let l:engine = g:vim_ai_complete['engine'] - let l:options = g:vim_ai_complete['options'] let l:cursor_on_empty_line = trim(join(l:lines, "\n")) == "" set paste if l:cursor_on_empty_line @@ -76,15 +93,16 @@ function! vim_ai#AIRun(is_selection, ...) range endfunction function! vim_ai#AIEditRun(is_selection, ...) range + let l:engine = g:vim_ai_edit['engine'] + let l:options = g:vim_ai_edit['options'] + let l:instruction = a:0 ? a:1 : "" - let l:prompt = s:MakePrompt(a:is_selection, getline(a:firstline, a:lastline), l:instruction) + let l:prompt = s:MakePrompt(a:is_selection, getline(a:firstline, a:lastline), l:instruction, l:options) let s:last_command = "edit" let s:last_instruction = l:instruction let s:last_is_selection = a:is_selection - let l:engine = g:vim_ai_edit['engine'] - let l:options = g:vim_ai_edit['options'] set paste execute "normal! " . a:firstline . "GV" . a:lastline . "Gc" execute "py3file " . s:complete_py @@ -92,6 +110,9 @@ function! vim_ai#AIEditRun(is_selection, ...) range endfunction function! vim_ai#AIChatRun(is_selection, ...) range + let l:options = g:vim_ai_chat['options'] + let l:ui = g:vim_ai_chat['ui'] + let l:instruction = "" let l:lines = getline(a:firstline, a:lastline) set paste @@ -101,7 +122,7 @@ function! vim_ai#AIChatRun(is_selection, ...) range let l:prompt = "" if a:0 || a:is_selection let l:instruction = a:0 ? a:1 : "" - let l:prompt = s:MakePrompt(a:is_selection, l:lines, l:instruction) + let l:prompt = s:MakePrompt(a:is_selection, l:lines, l:instruction, l:options) endif execute "normal! Gi" . l:prompt endif @@ -110,8 +131,6 @@ function! vim_ai#AIChatRun(is_selection, ...) range let s:last_instruction = l:instruction let s:last_is_selection = a:is_selection - let l:options = g:vim_ai_chat['options'] - let l:ui = g:vim_ai_chat['ui'] execute "py3file " . s:chat_py set nopaste endfunction diff --git a/autoload/vim_ai_config.vim b/autoload/vim_ai_config.vim index 92d791a..409070b 100644 --- a/autoload/vim_ai_config.vim +++ b/autoload/vim_ai_config.vim @@ -5,6 +5,7 @@ let g:vim_ai_complete_default = { \ "max_tokens": 1000, \ "temperature": 0.1, \ "request_timeout": 20, +\ "selection_boundary": "#####", \ }, \} let g:vim_ai_edit_default = { @@ -14,6 +15,7 @@ let g:vim_ai_edit_default = { \ "max_tokens": 1000, \ "temperature": 0.1, \ "request_timeout": 20, +\ "selection_boundary": "#####", \ }, \} @@ -29,6 +31,7 @@ let g:vim_ai_chat_default = { \ "max_tokens": 1000, \ "temperature": 1, \ "request_timeout": 20, +\ "selection_boundary": "", \ "initial_prompt": s:initial_chat_prompt, \ }, \ "ui": { |