iron.nvim icon indicating copy to clipboard operation
iron.nvim copied to clipboard

`send_code_block` feature compatibility with markdown dividers

Open agbocsardi opened this issue 11 months ago • 1 comments

This is a follow up from https://github.com/Vigemus/iron.nvim/pull/391#issuecomment-2720786693

The current send_code_block feature doesn't work as intended for Markdown code cells, delimited by triple backtics:

---
title: "Example QMD notebook"
---
## R code chunk
```{r}
print('hello')

for (i in seq(1, 5)) {
    print(i)
}
```

## Python code chunk
```{python}
print('hello')

for i in range(5):
    print(i)
```

If you define the dividers to be "```", so it would recognize either language, the issue is that in the case of both languages, the send_code_block function also sends the first line, with the language indicator attached, breaking the REPL:

In [1]: ```{python}
   ...: print('hello')
   ...: 
   ...: for i in range(5):
   ...:     print(i)
   ...: 
  Cell In[1], line 1
    ```{python}
    ^
SyntaxError: invalid syntax

The suggested solution by @frere-jacques (thanks for the sppedy reply <3) involves incrementing the start of the block by one, with some consideration for edge cases such as empty blocks, and start/end of buffer.

agbocsardi avatar Mar 13 '25 12:03 agbocsardi

Current workaround I use is based on treesitter-textobjects, since the @block query works for markdown code cells:

Inside my treesitter opts:

textobjects = {
        select = {
          enable = true,
          lookahead = false,

          keymaps = {
            ['ac'] = '@block.outer',
            ['ic'] = '@block.inner',
          },
        },
      }

This way I can visually select the whole code cell, then send it over to iron.

agbocsardi avatar Mar 13 '25 13:03 agbocsardi