From 6356c7457f08ce52f1d89b6728d34eeda3b704a5 Mon Sep 17 00:00:00 2001 From: Martin Bielik Date: Tue, 23 Jul 2024 23:54:38 +0200 Subject: allow multiple chats in keep open mode --- autoload/vim_ai.vim | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'autoload/vim_ai.vim') diff --git a/autoload/vim_ai.vim b/autoload/vim_ai.vim index 5157bae..a9a3431 100644 --- a/autoload/vim_ai.vim +++ b/autoload/vim_ai.vim @@ -21,9 +21,9 @@ let s:scratch_buffer_name = ">>> AI chat" " - scratch_buffer_keep_open = 1 " - opens last ai-chat buffer " - keeps the buffer in the buffer list -function! vim_ai#MakeScratchWindow() abort +function! vim_ai#MakeScratchWindow(force_new) abort let l:keep_open = g:vim_ai_chat['ui']['scratch_buffer_keep_open'] - if l:keep_open && bufexists(s:scratch_buffer_name) + if l:keep_open && bufexists(s:scratch_buffer_name) && !a:force_new " reuse chat buffer execute "buffer " . s:scratch_buffer_name return @@ -71,11 +71,12 @@ function! s:MakePrompt(selection, instruction, config) return join([l:instruction, l:delimiter, l:selection], "") endfunction -function! s:OpenChatWindow(open_conf) +function! s:OpenChatWindow(open_conf, force_new) 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 + call vim_ai#MakeScratchWindow(a:force_new) endfunction @@ -228,7 +229,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 +273,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 -- cgit v1.2.3 From 4e0e82f75beaa31b06b08153a5243cbfaa1fbd8b Mon Sep 17 00:00:00 2001 From: Martin Bielik Date: Wed, 24 Jul 2024 22:41:34 +0200 Subject: switch to last created buffer in keep open mode --- autoload/vim_ai.vim | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) (limited to 'autoload/vim_ai.vim') diff --git a/autoload/vim_ai.vim b/autoload/vim_ai.vim index a9a3431..94a5b3f 100644 --- a/autoload/vim_ai.vim +++ b/autoload/vim_ai.vim @@ -15,6 +15,21 @@ 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 @@ -23,9 +38,10 @@ let s:scratch_buffer_name = ">>> AI chat" " - keeps the buffer in the buffer list function! vim_ai#MakeScratchWindow(force_new) abort let l:keep_open = g:vim_ai_chat['ui']['scratch_buffer_keep_open'] - if l:keep_open && bufexists(s:scratch_buffer_name) && !a:force_new + let l:last_scratch_buffer_name = s:GetLastScratchBufferName() + if l:keep_open && bufexists(l:last_scratch_buffer_name) && !a:force_new " reuse chat buffer - execute "buffer " . s:scratch_buffer_name + execute "buffer " . l:last_scratch_buffer_name return endif setlocal buftype=nofile -- cgit v1.2.3 From e18d29dac7fdf616a6a738fdb8ab017e0c3f8549 Mon Sep 17 00:00:00 2001 From: Martin Bielik Date: Thu, 25 Jul 2024 21:55:31 +0200 Subject: private MakeScratchWindow function --- autoload/vim_ai.vim | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'autoload/vim_ai.vim') diff --git a/autoload/vim_ai.vim b/autoload/vim_ai.vim index 94a5b3f..a9c3d0a 100644 --- a/autoload/vim_ai.vim +++ b/autoload/vim_ai.vim @@ -33,10 +33,11 @@ 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(force_new) abort +function! s:MakeScratchWindow(force_new) abort let l:keep_open = g:vim_ai_chat['ui']['scratch_buffer_keep_open'] let l:last_scratch_buffer_name = s:GetLastScratchBufferName() if l:keep_open && bufexists(l:last_scratch_buffer_name) && !a:force_new @@ -92,7 +93,7 @@ function! s:OpenChatWindow(open_conf, force_new) \ ? g:vim_ai_open_chat_presets[a:open_conf] \ : a:open_conf execute l:open_cmd - call vim_ai#MakeScratchWindow(a:force_new) + call s:MakeScratchWindow(a:force_new) endfunction -- cgit v1.2.3 From be5ccb9e7b79063d3a2ad44d91f7631b3272779c Mon Sep 17 00:00:00 2001 From: Martin Bielik Date: Mon, 29 Jul 2024 19:32:16 +0200 Subject: remove useless buffer in keep open mode --- autoload/vim_ai.vim | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) (limited to 'autoload/vim_ai.vim') diff --git a/autoload/vim_ai.vim b/autoload/vim_ai.vim index a9c3d0a..a7d2bf9 100644 --- a/autoload/vim_ai.vim +++ b/autoload/vim_ai.vim @@ -37,14 +37,25 @@ endfunction " - scratch_buffer_keep_open = 1 " - opens last ai-chat buffer (unless force_new = 1) " - keeps the buffer in the buffer list -function! s:MakeScratchWindow(force_new) 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'] 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 " . 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 @@ -88,15 +99,6 @@ function! s:MakePrompt(selection, instruction, config) return join([l:instruction, l:delimiter, l:selection], "") endfunction -function! s:OpenChatWindow(open_conf, force_new) - 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 - call s:MakeScratchWindow(a:force_new) -endfunction - - let s:is_handling_paste_mode = 0 function! s:set_paste(config) -- cgit v1.2.3