sillybun / vim-repl

Best REPL environment for Vim
GNU General Public License v2.0
446 stars 39 forks source link

enhancement request: add support to send code by block defined by #%% or ### #127

Closed echaya closed 3 years ago

echaya commented 3 years ago

hi,

I've been playing around with the plug-in for a couple of days, and try to adopt it in my daily workflow. I see this plugin is really promising as a go-to REPL solution for many ppl. I'd like to request a :SendToRepl command so that it can be used in my .vimrc.

The problem with current approach with let g:sendtorepl_invoke_key = "<F9>" is that the key defined here can't be used in normal key-binding (correct me if im wrong plz). Following won't work based on my test as F5 wont fire the code to the terminal.

let g:sendtorepl_invoke_key = "<F9>" 
noremap <F5> <F9>

My current workflow involves executing code in blocks defined by ###.

###
arr = np.random.randint(low=0,high=100,size=(5,6))
for i in arr:
    print(i)
###

I came up with a short keymapping noremap <s-cr> %?###<cr>vN<F9> hoping to select the cell block and send the block for REPL. However it doesn't work (where F9 was defined per above). I've also tested noremap <s-cr> %?###<cr>vN<F5> which doesn't work either.

I guess the way let g:sendtorepl_invoke_key = "<F9>" assign keystroke for sending code to REPL is diff (im not familiar with VIMScript so didn't look into the detailed code) and can't be used for mappings.

So if we could expose a command, e.g., :SendToRepl then I could use it in my noremap without typing out the extra keystroke when sending the code.

Thanks for your consideration.

echaya commented 3 years ago

Otherwise, it would be great if the plug-in could support #%% or ### natively. Believe this is a more intuitive approach than # BEGIN and # END implemented currently.

https://code.visualstudio.com/docs/python/jupyter-support-py for reference.

echaya commented 3 years ago

I actually found :SendLineToREPL<Cr> when digging into the code. Guess we just need a better documentation than the current one :D

echaya commented 3 years ago

I also see the following but not too sure how to execute it.

if !exists('g:repl_code_block_fences')
    let g:repl_code_block_fences = {'python': '# %%', 'zsh': '# %%', 'markdown': '```'}
endif
sillybun commented 3 years ago

For your expectation, just add

    let g:repl_code_block_fences = {'python': '###', 'zsh': '# %%', 'markdown': '```'}

to .vimrc

and :REPLSendSession will send whole block to REPL environment.

sillybun commented 3 years ago

code between g:repl_code_block_fences['python'] are seen as code block.

sillybun commented 3 years ago

You are right. Currently the document is pretty bad. I always came up with an idea and implement some function and forget to write in the document.

echaya commented 3 years ago

code between g:repl_code_block_fences['python'] are seen as code block.

thanks much for your prompt reply. it works beautifully!

sillybun commented 3 years ago

Actually code block are code between g:repl_code_block_fences['python'] and g:repl_code_block_fences_end['python'].
But by default the latter is the same as the former.