Vigemus / iron.nvim

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

How to send a block of code? #342

Closed Bubobubobubobubo closed 1 year ago

Bubobubobubobubo commented 1 year ago

Hi, I am using Iron to live code music using Python. One of the gestures I need for that is the capacity to send code blocks in the Python REPL:

@swim
def test_iron():
    print('I need to send that whole block of code')
    again(test_iron)

I was previously using slime that makes it easy to select a zone and send it to the REPL for eval. Am I missing something in the documentation?

hkupty commented 1 year ago

In iron you can send multiple lines together as a chunk of text using the the mapping for send motion, just like in the example config:

iron.setup {
  config = {
  -- ...
  },
  -- Iron doesn't set keymaps by default anymore.
  -- You can set them here or manually add keymaps to the functions in iron.core
  keymaps = {
    send_motion = "<space>sc",
    -- ...
  }
}

In this case, if you run <space>scap you'll send the whole paragraph. If you use treesitter, you can use nvim-treesitter-textobjects' af to send a function: <space>scaf.

I hope this helps.

Best regards, Henry

Bubobubobubobubo commented 1 year ago

Hi, <space>scap is perfect. This is exactly what I needed. I also found the following technique but I don't really know why it works. It is less immediate compared to the command you were suggesting though. Sardine is the name of a custom Python interpreter I am running (see my repo):

python = {
    command = { "sardine" },
    format = require("iron.fts.common").bracketed_paste,
},

How would you remap <space>scap on something like <space><CR>?

hkupty commented 1 year ago

ap is a vim text object that would read as around paragraph, just like iw would read `inner word.

You should be able to use regular nmap <space><CR> <space>scap, but bear in mind that it is usually preferred to use text objects and motions as each case is different. If, for example, you have a function with an empty line, say, for better readability, you'd probably want to use treesitter's af instead, or at least <space>sc2ap, for sending two whole paragraphs instead.

I hope this solves your issue, but feel free to reopen if you have any further questions!

hkupty commented 1 year ago

For reference, here is the link to treesitter's text-object: https://github.com/nvim-treesitter/nvim-treesitter-textobjects

henrikmunch commented 1 year ago

I am facing a similar problem regarding the ap key binding. I have send_motion = "<c-s>" , and indeed I can send a paragraph of text by typing <c-s>ap.

However, I am unable to abbreviate this sequence of keys to <c-z>. I attempted the latter by writing vim.keymap.set("n", "<c-z>", "<c-s>ap"). Which mistake am I making?

Thanks in advance!