From 1bbfb606b96ae2d7e3c4a89ef08b928a9dae6c51 Mon Sep 17 00:00:00 2001 From: Konfekt Date: Mon, 11 Mar 2024 14:50:43 +0100 Subject: Implement smarter AI chat window detection to reuse existing AI chat windows. First search for AI chat windows within the same tab and then within other tabs if none are found in the current tab. It now prioritizes the reusing of an existing chat window that matches the '.aichat' filetype before considering opening a new one. If there are no existing AI chat windows, the plugin will open a new chat window as a last resort. diff --git a/autoload/vim_ai.vim b/autoload/vim_ai.vim: - " reuse chat in active window or tab + " TODO: look for first active chat buffer. If .aichat file is used, + " then reuse chat in active window - " allow .aichat files windows to be switched to, preferably on same tab - let buffer_list_tab = tabpagebuflist(tabpagenr()) - let buffer_list_tab = filter(buffer_list_tab, 'getbufvar(v:val, "&filetype") ==# "aichat"') - - let buffer_list = [] - for i in range(tabpagenr('$')) - call extend(buffer_list, tabpagebuflist(i + 1)) - endfor - let buffer_list = filter(buffer_list, 'getbufvar(v:val, "&filetype") ==# "aichat"') - - if len(buffer_list_tab) > 0 - call win_gotoid(win_findbuf(buffer_list_tab[0])[0]) - elseif len(buffer_list) > 0 - call win_gotoid(win_findbuf(buffer_list[0])[0]) - else - " open new chat window - let l:open_conf = l:config['ui']['open_chat_command'] - call s:OpenChatWindow(l:open_conf) - endif + " open new chat window + let l:open_conf = l:config['ui']['open_chat_command'] + call s:OpenChatWindow(l:open_conf) --- autoload/vim_ai.vim | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) (limited to 'autoload/vim_ai.vim') diff --git a/autoload/vim_ai.vim b/autoload/vim_ai.vim index a9bac80..3be3caf 100644 --- a/autoload/vim_ai.vim +++ b/autoload/vim_ai.vim @@ -207,13 +207,30 @@ function! vim_ai#AIChatRun(uses_range, config, ...) range if &filetype != 'aichat' let l:chat_win_ids = win_findbuf(bufnr(s:scratch_buffer_name)) if !empty(l:chat_win_ids) - " TODO: look for first active chat buffer. If .aichat file is used, - " then reuse chat in active window + " reuse chat in active window or tab call win_gotoid(l:chat_win_ids[0]) else - " open new chat window - let l:open_conf = l:config['ui']['open_chat_command'] - call s:OpenChatWindow(l:open_conf) + " allow .aichat files windows to be switched to, preferably on same tab + let buffer_list_tab = tabpagebuflist(tabpagenr()) + let buffer_list_tab = filter(buffer_list_tab, 'getbufvar(v:val, "&filetype") ==# "aichat"') + + if len(buffer_list_tab) > 0 + call win_gotoid(win_findbuf(buffer_list_tab[0])[0]) + else + let buffer_list = [] + for i in range(tabpagenr('$')) + call extend(buffer_list, tabpagebuflist(i + 1)) + endfor + let buffer_list = filter(buffer_list, 'getbufvar(v:val, "&filetype") ==# "aichat"') + + if len(buffer_list) > 0 + call win_gotoid(win_findbuf(buffer_list[0])[0]) + else + " open new chat window + let l:open_conf = l:config['ui']['open_chat_command'] + call s:OpenChatWindow(l:open_conf) + endif + endif endif endif -- cgit v1.2.3