diff options
Diffstat (limited to '')
| -rw-r--r-- | README.md | 35 | ||||
| -rw-r--r-- | autoload/vim_ai.vim | 12 | ||||
| -rw-r--r-- | doc/vim-ai.txt | 33 |
3 files changed, 80 insertions, 0 deletions
@@ -289,6 +289,41 @@ let g:vim_ai_complete = chat_engine_config let g:vim_ai_edit = chat_engine_config ``` +## Custom commands + +To create a custom command, you can call `AIRun`, `AIEditRun` and `AIChatRun` functions. See examples below: + +```vim +" custom command suggesting git commit message, takes no arguments +function! AIPromptCommitMessageFn() + let l:diff = system('git diff --staged') + let l:prompt = "generate a short commit message from the diff below:\n" . l:diff + let l:range = 0 + let l:config = { + \ "engine": "chat", + \ "options": { + \ "model": "gpt-3.5-turbo", + \ "initial_prompt": ">>> system\nyou are a code assistant", + \ "temperature": 1, + \ }, + \} + call vim_ai#AIRun(l:range, l:config, l:prompt) +endfunction +command! AIPromptCommitMessage call AIPromptCommitMessageFn() + +" custom command that provides a code review for selected code block +function! AIPromptCodeReviewFn(range) range + let l:prompt = "programming syntax is " . &filetype . ", review the code below" + let l:config = { + \ "options": { + \ "initial_prompt": ">>> system\nyou are a clean code expert", + \ }, + \} + '<,'>call vim_ai#AIChatRun(a:range, l:config, l:prompt) +endfunction +command! -range AIPromptCodeReview <line1>,<line2>call AIPromptCodeReviewFn(<range>) +``` + ## Important Disclaimer **Accuracy**: GPT is good at producing text and code that looks correct at first glance, but may be completely wrong. Be sure to thoroughly review, read and test all output generated by this plugin! diff --git a/autoload/vim_ai.vim b/autoload/vim_ai.vim index b058b54..7206431 100644 --- a/autoload/vim_ai.vim +++ b/autoload/vim_ai.vim @@ -69,6 +69,10 @@ function! s:MakePrompt(is_selection, lines, instruction, config) return join([l:instruction, l:delimiter, l:selection], "") endfunction +" Complete prompt +" - is_selection - <range> parameter +" - config - function scoped vim_ai_complete config +" - a:1 - optional instruction prompt function! vim_ai#AIRun(is_selection, config, ...) range let l:config = vim_ai_config#ExtendDeep(g:vim_ai_complete, a:config) @@ -93,6 +97,10 @@ function! vim_ai#AIRun(is_selection, config, ...) range set nopaste endfunction +" Edit prompt +" - is_selection - <range> parameter +" - config - function scoped vim_ai_edit config +" - a:1 - optional instruction prompt function! vim_ai#AIEditRun(is_selection, config, ...) range let l:config = vim_ai_config#ExtendDeep(g:vim_ai_edit, a:config) @@ -110,6 +118,10 @@ function! vim_ai#AIEditRun(is_selection, config, ...) range set nopaste endfunction +" Start and answer the chat +" - is_selection - <range> parameter +" - config - function scoped vim_ai_chat config +" - a:1 - optional instruction prompt function! vim_ai#AIChatRun(is_selection, config, ...) range let l:config = vim_ai_config#ExtendDeep(g:vim_ai_chat, a:config) diff --git a/doc/vim-ai.txt b/doc/vim-ai.txt index 5cc0718..396fb95 100644 --- a/doc/vim-ai.txt +++ b/doc/vim-ai.txt @@ -143,6 +143,39 @@ Examples how configure key bindings and customize commands: > " redo last AI command nnoremap <leader>r :AIRedo<CR> +CUSTOM COMMANDS + +To create custom commands, call `AIRun`, `AIEditRun` and `AIChatRun` functions: > + + " custom command suggesting git commit message, takes no arguments + function! AIPromptCommitMessageFn() + let l:diff = system('git diff --staged') + let l:prompt = "generate a short commit message from the diff below:\n" . l:diff + let l:range = 0 + let l:config = { + \ "engine": "chat", + \ "options": { + \ "model": "gpt-3.5-turbo", + \ "initial_prompt": ">>> system\nyou are a code assistant", + \ "temperature": 1, + \ }, + \} + call vim_ai#AIRun(l:range, l:config, l:prompt) + endfunction + command! AIPromptCommitMessage call AIPromptCommitMessageFn() + + " custom command that provides a code review for selected code block + function! AIPromptCodeReviewFn(range) range + let l:prompt = "programming syntax is " . &filetype . ", review the code below" + let l:config = { + \ "options": { + \ "initial_prompt": ">>> system\nyou are a clean code expert", + \ }, + \} + '<,'>call vim_ai#AIChatRun(a:range, l:config, l:prompt) + endfunction + command! -range AIPromptCodeReview <line1>,<line2>call AIPromptCodeReviewFn(<range>) + ABOUT *vim-ai-about* Contributions are welcome on GitHub: |