Skip to content

Commit

Permalink
Add a NeuralViewPrompt command
Browse files Browse the repository at this point in the history
Add a command which prints the exact prompt that will be sent to virtual
assitants by Neural so you can more easily debug what Neural is going
to do.
  • Loading branch information
w0rp committed Jun 2, 2024
1 parent b1ea6ef commit db8b774
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 0 deletions.
20 changes: 20 additions & 0 deletions autoload/neural.vim
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,26 @@ function! neural#Prompt(prompt) abort
call neural#Run(a:prompt, {})
endfunction

" Print the prompt that Neural will use in full.
function! neural#ViewPrompt(...) abort
" Take the first argument or nothing.
let l:prompt = get(a:000, 0, '')
let l:buffer = bufnr('')
let l:source = s:LoadDataSource()
let l:input = s:GetSourceInput(l:buffer, l:source, l:prompt)

" no-custom-checks
echohl Question
" no-custom-checks
echo 'The following prompt will be sent.'
" no-custom-checks
echohl None
" no-custom-checks
echo "\n"
" no-custom-checks
echo l:input.prompt
endfunction

function! neural#Run(prompt, options) abort
let l:buffer = bufnr('')

Expand Down
12 changes: 12 additions & 0 deletions doc/neural.txt
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,18 @@ appropriate value. OpenAI is the default data source.
|g:neural.set_default_keybinds| to any falsy value.


`:NeuralViewPrompt` *NeuralViewPrompt*

View the complete prompt that will be sent. e.g. `:NeuralViewPrompt say hello`

Neural will automatically alter prompts sent to virtual assistants before
they are sent depending on the filetype of the current file and the
surrounding context. This command allows you to see what that prompt will be
before it is sent.

A plug mapping `<Plug>(neural_view_prompt)` is defined for this.


===============================================================================
4. Options *neural-options*
*g:neural*
Expand Down
3 changes: 3 additions & 0 deletions plugin/neural.vim
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,15 @@ command! -nargs=0 NeuralStop :call neural#Stop()
command! -nargs=? NeuralBuffer :call neural#buffer#CreateBuffer(<q-args>)
" Have Neural explain the visually selected lines.
command! -range NeuralExplain :call neural#explain#SelectedLines()
" Have Neural print the prompt that will be sent.
command! -nargs=? NeuralViewPrompt :call neural#ViewPrompt(<f-args>)

" <Plug> mappings for commands
nnoremap <silent> <Plug>(neural_prompt) :call neural#OpenPrompt()<Return>
nnoremap <silent> <Plug>(neural_stop) :call neural#Stop()<Return>
nnoremap <silent> <Plug>(neural_buffer) :call neural#buffer#CreateBuffer({})<Return>
vnoremap <silent> <Plug>(neural_explain) :NeuralExplain<Return>
nnoremap <silent> <Plug>(neural_view_prompt) :call neural#ViewPrompt()<Return>
" Set default keybinds for Neural unless we're told not to. We should almost
" never define keybinds by default in a plugin, but we can add only a few to
Expand Down
27 changes: 27 additions & 0 deletions test/vim/test_view_prompt.vader
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
After:
unlet! g:output

Given markdown(An empty Markdown file):
Execute(An empty markdown prompt should be explained correctly):
redir => g:output
silent NeuralViewPrompt
redir END

AssertEqual
\ [
\ 'The following prompt will be sent.',
\ 'Write text in a markdown file. '
\ ],
\ sort(filter(split(g:output, "\n"), '!empty(v:val)'))

Execute(A markdown prompt with some text should be explained correctly):
redir => g:output
silent NeuralViewPrompt here is some text
redir END

AssertEqual
\ [
\ 'The following prompt will be sent.',
\ 'Write text in a markdown file. here is some text'
\ ],
\ sort(filter(split(g:output, "\n"), '!empty(v:val)'))

0 comments on commit db8b774

Please sign in to comment.