From 9d43ef6c4966705376af2cd16fb012d020ce673d Mon Sep 17 00:00:00 2001 From: Martin Bielik Date: Sun, 8 Dec 2024 23:15:33 +0100 Subject: don't include not selected line, refactor ranges, fixes #112 --- README.md | 8 ++++---- autoload/vim_ai.vim | 43 ++++++++++++++----------------------------- doc/vim-ai.txt | 1 - plugin/vim-ai.vim | 21 +++++---------------- 4 files changed, 23 insertions(+), 50 deletions(-) diff --git a/README.md b/README.md index abe74b6..e8ca86a 100644 --- a/README.md +++ b/README.md @@ -107,8 +107,6 @@ In the documentation below, `` denotes a visual selection or any oth ### `:AI` -`:AI` - complete the text on the current line - `:AI {prompt}` - complete the prompt ` :AI` - complete the selection @@ -481,6 +479,7 @@ To create a custom command, you can call `AIRun`, `AIEditRun` and `AIChatRun` fu ```vim " custom command suggesting git commit message, takes no arguments function! GitCommitMessageFn() + let l:range = 0 let l:diff = system('git --no-pager diff --staged') let l:prompt = "generate a short commit message from the diff below:\n" . l:diff let l:config = { @@ -491,10 +490,11 @@ function! GitCommitMessageFn() \ "temperature": 1, \ }, \} - call vim_ai#AIRun(l:config, l:prompt) + call vim_ai#AIRun(l:range, l:config, l:prompt) endfunction command! GitCommitMessage call GitCommitMessageFn() + " custom command that provides a code review for selected code block function! CodeReviewFn(range) range let l:prompt = "programming syntax is " . &filetype . ", review the code below" @@ -505,7 +505,7 @@ function! CodeReviewFn(range) range \} exe a:firstline.",".a:lastline . "call vim_ai#AIChatRun(a:range, l:config, l:prompt)" endfunction -command! -range=0 CodeReview ,call CodeReviewFn() +command! -range -nargs=? AICodeReview ,call CodeReviewFn() ``` ## Contributing diff --git a/autoload/vim_ai.vim b/autoload/vim_ai.vim index a7d2bf9..3effe73 100644 --- a/autoload/vim_ai.vim +++ b/autoload/vim_ai.vim @@ -118,8 +118,10 @@ endfunction function! s:GetSelectionOrRange(is_selection, ...) if a:is_selection return s:GetVisualSelection() - else + elseif a:1 != a:2 return trim(join(getline(a:1, a:2), "\n")) + else + return "" endif endfunction @@ -145,20 +147,14 @@ function! s:GetVisualSelection() endfunction " Complete prompt +" - uses_range - truty if range passed " - config - function scoped vim_ai_complete config " - a:1 - optional instruction prompt -" - a:2 - optional selection pending (to override g:vim_ai_is_selection_pending) -function! vim_ai#AIRun(config, ...) range abort +function! vim_ai#AIRun(uses_range, config, ...) range abort let l:config = vim_ai_config#ExtendDeep(g:vim_ai_complete, a:config) let l:instruction = a:0 > 0 ? a:1 : "" " l:is_selection used in Python script - if a:0 > 1 - let l:is_selection = a:2 - else - let l:is_selection = g:vim_ai_is_selection_pending && - \ a:firstline == line("'<") && a:lastline == line("'>") - endif - + let l:is_selection = a:uses_range && a:firstline == line("'<") && a:lastline == line("'>") let l:selection = s:GetSelectionOrRange(l:is_selection, a:firstline, a:lastline) let l:prompt = s:MakePrompt(l:selection, l:instruction, l:config) @@ -185,19 +181,14 @@ function! vim_ai#AIRun(config, ...) range abort endfunction " Edit prompt +" - uses_range - truty if range passed " - config - function scoped vim_ai_edit config " - a:1 - optional instruction prompt -" - a:2 - optional selection pending (to override g:vim_ai_is_selection_pending) -function! vim_ai#AIEditRun(config, ...) range abort +function! vim_ai#AIEditRun(uses_range, config, ...) range abort let l:config = vim_ai_config#ExtendDeep(g:vim_ai_edit, a:config) let l:instruction = a:0 > 0 ? a:1 : "" " l:is_selection used in Python script - if a:0 > 1 - let l:is_selection = a:2 - else - let l:is_selection = g:vim_ai_is_selection_pending && - \ a:firstline == line("'>") && a:lastline == line("'>") - endif + let l:is_selection = a:uses_range && a:firstline == line("'<") && a:lastline == line("'>") let l:selection = s:GetSelectionOrRange(l:is_selection, a:firstline, a:lastline) let l:prompt = s:MakePrompt(l:selection, l:instruction, l:config) @@ -253,21 +244,15 @@ function! s:ReuseOrCreateChatWindow(config) endfunction " Start and answer the chat -" - uses_range - true if range passed +" - uses_range - truty if range passed " - config - function scoped vim_ai_chat config " - a:1 - optional instruction prompt function! vim_ai#AIChatRun(uses_range, config, ...) range abort let l:config = vim_ai_config#ExtendDeep(g:vim_ai_chat, a:config) let l:instruction = "" " l:is_selection used in Python script - if a:uses_range - let l:is_selection = g:vim_ai_is_selection_pending && - \ a:firstline == line("'<") && a:lastline == line("'>") - let l:selection = s:GetSelectionOrRange(l:is_selection, a:firstline, a:lastline) - else - let l:is_selection = 0 - let l:selection = '' - endif + let l:is_selection = a:uses_range && a:firstline == line("'<") && a:lastline == line("'>") + let l:selection = s:GetSelectionOrRange(l:is_selection, a:firstline, a:lastline) try call s:set_paste(l:config) @@ -300,9 +285,9 @@ endfunction function! vim_ai#AIRedoRun() abort undo if s:last_command ==# "complete" - exe s:last_firstline.",".s:last_lastline . "call vim_ai#AIRun(s:last_config, s:last_instruction, s:last_is_selection)" + exe s:last_firstline.",".s:last_lastline . "call vim_ai#AIRun(s:last_is_selection, s:last_config, s:last_instruction)" elseif s:last_command ==# "edit" - exe s:last_firstline.",".s:last_lastline . "call vim_ai#AIEditRun(s:last_config, s:last_instruction, s:last_is_selection)" + exe s:last_firstline.",".s:last_lastline . "call vim_ai#AIEditRun(s:last_is_selection, s:last_config, s:last_instruction)" elseif s:last_command ==# "chat" " chat does not need prompt, all information are in the buffer already call vim_ai#AIChatRun(0, s:last_config) diff --git a/doc/vim-ai.txt b/doc/vim-ai.txt index ff080f8..0d9a410 100644 --- a/doc/vim-ai.txt +++ b/doc/vim-ai.txt @@ -15,7 +15,6 @@ https://github.com/madox2/vim-ai *:AI* -:AI complete the text on the current line :AI {prompt} complete the prompt :AI complete the selection :AI {instruction} complete the selection using the instruction diff --git a/plugin/vim-ai.vim b/plugin/vim-ai.vim index 1ed5326..d27f312 100644 --- a/plugin/vim-ai.vim +++ b/plugin/vim-ai.vim @@ -4,19 +4,8 @@ if !has('python3') finish endif -" detect if a visual selection is pending: https://stackoverflow.com/a/20133772 -let g:vim_ai_is_selection_pending = 0 -augroup vim_ai - autocmd! - autocmd CursorMoved * - \ let g:vim_ai_is_selection_pending = mode() =~# "^[vV\]" -augroup END - -command! -range -nargs=? -complete=customlist,vim_ai#RoleCompletion AI ,call vim_ai#AIRun({}, ) -command! -range -nargs=? -complete=customlist,vim_ai#RoleCompletion AIEdit ,call vim_ai#AIEditRun({}, ) -" Whereas AI and AIEdit default to passing the current line as range -" AIChat defaults to passing nothing which is achieved by -range=0 and passing -" as described at https://stackoverflow.com/a/20133772 -command! -range=0 -nargs=? -complete=customlist,vim_ai#RoleCompletion AIChat ,call vim_ai#AIChatRun(, {}, ) -command! -nargs=? AINewChat call vim_ai#AINewChatRun() -command! AIRedo call vim_ai#AIRedoRun() +command! -range -nargs=? -complete=customlist,vim_ai#RoleCompletion AI ,call vim_ai#AIRun(, {}, ) +command! -range -nargs=? -complete=customlist,vim_ai#RoleCompletion AIEdit ,call vim_ai#AIEditRun(, {}, ) +command! -range -nargs=? -complete=customlist,vim_ai#RoleCompletion AIChat ,call vim_ai#AIChatRun(, {}, ) +command! -nargs=? AINewChat call vim_ai#AINewChatRun() +command! AIRedo call vim_ai#AIRedoRun() -- cgit v1.2.3