diff options
| author | Martin Bielik <mx.bielik@gmail.com> | 2023-02-27 17:04:33 +0100 |
|---|---|---|
| committer | Martin Bielik <mx.bielik@gmail.com> | 2023-02-27 21:26:18 +0100 |
| commit | 9fa5e42f94464a2ddb0889fbc86bf30936bda256 (patch) | |
| tree | 71245e06db0f129517aef04fce362c52dbc5460d /vim-ai.vim | |
| parent | e69475498a21df2934ff1c2e87fad6cfa942dc5a (diff) | |
| download | vim-ai-9fa5e42f94464a2ddb0889fbc86bf30936bda256.tar.gz | |
accept additional arguments
Diffstat (limited to '')
| -rw-r--r-- | vim-ai.vim | 29 |
1 files changed, 20 insertions, 9 deletions
@@ -1,19 +1,30 @@ let g:openaiToken = system("cat ~/.config/openai.token") -function! AIRun() range - execute a:firstline . ',' . a:lastline . 'd' +function! AIRun(...) range + let selection = getline(a:firstline, a:lastline) + if a:0 + let instruction = a:1 . ":" + call insert(selection, instruction, 0) + endif + + let buff_lastline = line('$') - let selection = @* - call writefile(split(selection, "\n"), "/tmp/vim-ai.temp") + call writefile(selection, "/tmp/vim-ai.temp") - echo "Working..." + echo "Completing..." let output = system("cat /tmp/vim-ai.temp | openai complete - -t " . g:openaiToken) + let output = trim(output) + + execute a:firstline . ',' . a:lastline . 'd' + + if a:lastline == buff_lastline + execute "normal! o" . output . "\<Esc>" + else + execute "normal! O" . output . "\<Esc>" + endif - call feedkeys("i") - call feedkeys(output) - call feedkeys("\<Esc>") endfunction -command! -range AI <line1>,<line2>call AIRun() +command! -range -nargs=? AI <line1>,<line2>call AIRun(<f-args>) nnoremap <leader>o :call AIRun()<CR> vnoremap <leader>o :call AIRun()<CR> |