diff options
| author | Martin Bielik <mx.bielik@gmail.com> | 2024-03-24 12:39:29 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-03-24 12:39:29 +0100 |
| commit | 4e7fe9bcbfe6e4c97cd70a531438c8e961a20664 (patch) | |
| tree | c49d6ba116db99885e01b725cbbc7279b5c7aa33 | |
| parent | b75b5be5dcf762b76d832eae4f7814824a479e88 (diff) | |
| parent | 6b9a0ef3771a4c873021e5cfa59a98be4030d0ee (diff) | |
| download | vim-ai-4e7fe9bcbfe6e4c97cd70a531438c8e961a20664.tar.gz | |
Merge pull request #88 from Konfekt/tabwin
detect chat window in other tabs as well
| -rw-r--r-- | autoload/vim_ai.vim | 48 |
1 files changed, 36 insertions, 12 deletions
diff --git a/autoload/vim_ai.vim b/autoload/vim_ai.vim index a37ddad..8d5b3bc 100644 --- a/autoload/vim_ai.vim +++ b/autoload/vim_ai.vim @@ -184,6 +184,40 @@ function! vim_ai#AIEditRun(config, ...) range execute "py3file " . s:complete_py endfunction +function! s:ReuseOrCreateChatWindow(config) + if &filetype != 'aichat' + " reuse chat in active window or tab + let l:chat_win_ids = win_findbuf(bufnr(s:scratch_buffer_name)) + if !empty(l:chat_win_ids) + call win_gotoid(l:chat_win_ids[0]) + return + endif + + " reuse .aichat file on the 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]) + return + endif + + " reuse any .aichat buffer in the session + 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]) + return + endif + + " 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) + endif +endfunction + " Start and answer the chat " - uses_range - true if range passed " - config - function scoped vim_ai_chat config @@ -201,18 +235,8 @@ function! vim_ai#AIChatRun(uses_range, config, ...) range let l:selection = '' endif call s:set_paste(l:config) - if &filetype != 'aichat' - let l:chat_win_id = bufwinid(s:scratch_buffer_name) - if l:chat_win_id != -1 - " TODO: look for first active chat buffer, in case .aichat file is used - " reuse chat in active window - call win_gotoid(l:chat_win_id) - else - " open new chat window - let l:open_conf = l:config['ui']['open_chat_command'] - call s:OpenChatWindow(l:open_conf) - endif - endif + + call s:ReuseOrCreateChatWindow(l:config) let l:prompt = "" if a:0 > 0 || a:uses_range |