diff options
| author | Martin Bielik <mx.bielik@gmail.com> | 2024-09-11 21:31:44 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-09-11 21:31:44 +0200 |
| commit | ff479a02e8c7fce544f51ffc4be2542e7dd88039 (patch) | |
| tree | 4d0c70aa6a5cbd91ccfc29b3bb6afcc260aac810 /autoload/vim_ai.vim | |
| parent | f5163548a5c53cfd19e186d2214533e9ed658f03 (diff) | |
| parent | be5ccb9e7b79063d3a2ad44d91f7631b3272779c (diff) | |
| download | vim-ai-ff479a02e8c7fce544f51ffc4be2542e7dd88039.tar.gz | |
Merge pull request #113 from madox2/keep-open-mode-multiple-chats
allow multiple chats in keep open mode
Diffstat (limited to 'autoload/vim_ai.vim')
| -rw-r--r-- | autoload/vim_ai.vim | 48 |
1 files changed, 34 insertions, 14 deletions
diff --git a/autoload/vim_ai.vim b/autoload/vim_ai.vim index 5157bae..a7d2bf9 100644 --- a/autoload/vim_ai.vim +++ b/autoload/vim_ai.vim @@ -15,19 +15,47 @@ let s:last_config = {} let s:scratch_buffer_name = ">>> AI chat" +function! s:StartsWith(longer, shorter) abort + return a:longer[0:len(a:shorter)-1] ==# a:shorter +endfunction + +function! s:GetLastScratchBufferName() + let l:all_buffer_names = map(map(filter(copy(getbufinfo()), 'v:val.listed'), 'v:val.bufnr'), 'bufname(v:val)') + let l:buffer_name = -1 + for l:name in l:all_buffer_names + if s:StartsWith(l:name, s:scratch_buffer_name) + let l:buffer_name = l:name + endif + endfor + return l:buffer_name +endfunction + " Configures ai-chat scratch window. " - scratch_buffer_keep_open = 0 " - opens new ai-chat every time +" - excludes buffer from buffer list " - scratch_buffer_keep_open = 1 -" - opens last ai-chat buffer +" - opens last ai-chat buffer (unless force_new = 1) " - keeps the buffer in the buffer list -function! vim_ai#MakeScratchWindow() abort +function! s:OpenChatWindow(open_conf, force_new) abort + " open new buffer that will be used as a chat + let l:open_cmd = has_key(g:vim_ai_open_chat_presets, a:open_conf) + \ ? g:vim_ai_open_chat_presets[a:open_conf] + \ : a:open_conf + execute l:open_cmd + + " reuse chat in keep-open mode let l:keep_open = g:vim_ai_chat['ui']['scratch_buffer_keep_open'] - if l:keep_open && bufexists(s:scratch_buffer_name) + let l:last_scratch_buffer_name = s:GetLastScratchBufferName() + if l:keep_open && bufexists(l:last_scratch_buffer_name) && !a:force_new + let l:current_buffer = bufnr('%') " reuse chat buffer - execute "buffer " . s:scratch_buffer_name + execute "buffer " . l:last_scratch_buffer_name + " close new buffer that was created by l:open_cmd + execute "bd " . l:current_buffer return endif + setlocal buftype=nofile setlocal noswapfile setlocal ft=aichat @@ -71,14 +99,6 @@ function! s:MakePrompt(selection, instruction, config) return join([l:instruction, l:delimiter, l:selection], "") endfunction -function! s:OpenChatWindow(open_conf) - let l:open_cmd = has_key(g:vim_ai_open_chat_presets, a:open_conf) - \ ? g:vim_ai_open_chat_presets[a:open_conf] - \ : a:open_conf - execute l:open_cmd -endfunction - - let s:is_handling_paste_mode = 0 function! s:set_paste(config) @@ -228,7 +248,7 @@ function! s:ReuseOrCreateChatWindow(config) " open new chat window if no active buffer found let l:open_conf = a:config['ui']['open_chat_command'] - call s:OpenChatWindow(l:open_conf) + call s:OpenChatWindow(l:open_conf, 0) endif endfunction @@ -272,7 +292,7 @@ endfunction " a:1 - optional preset shorcut (below, right, tab) function! vim_ai#AINewChatRun(...) abort let l:open_conf = a:0 > 0 ? "preset_" . a:1 : g:vim_ai_chat['ui']['open_chat_command'] - call s:OpenChatWindow(l:open_conf) + call s:OpenChatWindow(l:open_conf, 1) call vim_ai#AIChatRun(0, {}) endfunction |