Vigemus / iron.nvim

Interactive Repl Over Neovim
BSD 3-Clause "New" or "Revised" License
978 stars 81 forks source link

Send multiline code chunk #341

Closed sdavidsson90 closed 1 year ago

sdavidsson90 commented 1 year ago

Thanks for a great plugin!

In the beginning of the asciinema featured in the readme, 'hkupty' writes a two-lined python-function and sends it to the REPL for execution. He does this while standing on the second line and without visually selecting the desired lines first.

How do you send a multiline code chunk in one motion?

hkupty commented 1 year ago

Hi! I'm glad you like the plugin, thank you very much!

In the sample configuration, there's a line for mapping send_motion, which is the motion I used in that asciinema. you can also use the mark_motion to mark a codeblock to be sent later with send_mark.

I hope this is helpful. Please don't hesitate to ask questions if you are in need!

Best regards,

sdavidsson90 commented 1 year ago

Thank you for your swift reply. This approach works, but also requires me to count the number of lines to be sent. Some editors (eg. RStudio) can recognise when the cursor is on a line that is part of a chunk, and execute the whole chunk. Is Iron capable of this?

hkupty commented 1 year ago

if you have treesitter installed, you can use send_motion with a semantic block, for example, if your send_motion is mapped to sm, you could use smaf to send the chunk surrounding a function.

given the way neovim works with text objects, I believe it is out of scope for iron to implement this feature, as it would be conflicting with more specialized plugins like treesitter or lsp.

sdavidsson90 commented 1 year ago

Thank you for your help! This works very well for Python and Bash - but not for R unfortunately. I believe this is not an issue for the Iron community, but for Treesitter. If anyone is interested in the details: It only seems to accept functions, and they seem to get stuck in the buffer as they are printed even when other operations are executed.

In testing this, I also noticed unexpected behaviour when sending mutiple commands at at once. In R and IPython, the standard inputs seem to get sent to the REPL in a single continuous stream before any of the standard outputs are printed.

Sending these lines:

def add_10(x):
    result = x + 10
    return result

add_10(100)

def add_20(x):
    result = x + 20
    return result

add_20(100)

Returns: Out[1]: 120 - and not the other result. The same R code will correctly return the output of both functions, but after the entirety of the standard input. In Bash the output of each line is printed as the lines are fed into the REPL as expected.

This is obviously not the end of the world, but could perhaps be given some consideration in a possible future release of Iron. Again, I am very grateful for your work.