Skip to content

Commit eb98585

Browse files
mikedecrjmbuhr
andauthored
feat: support Iron.nvim and user functions for sending code to REPL (#109)
--------- Co-authored-by: Jannik Buhr <[email protected]>
1 parent 5512a57 commit eb98585

File tree

6 files changed

+54
-19
lines changed

6 files changed

+54
-19
lines changed

README.md

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,11 @@ require('quarto').setup{
7878
},
7979
},
8080
codeRunner = {
81-
enabled = false,
82-
default_method = nil, -- 'molten' or 'slime'
81+
enabled = true,
82+
default_method = "slime", -- "molten", "slime", "iron" or <function>
8383
ft_runners = {}, -- filetype to runner, ie. `{ python = "molten" }`.
84-
-- Takes precedence over `default_method`
85-
never_run = { "yaml" }, -- filetypes which are never sent to a code runner
84+
-- Takes precedence over `default_method`
85+
never_run = { 'yaml' }, -- filetypes which are never sent to a code runner
8686
},
8787
}
8888
```
@@ -150,22 +150,28 @@ Or, what might ultimately be the cleaner way of documenting language specific is
150150
## Running Code
151151

152152
Quarto-nvim doesn't run code for you, instead, it will interface with existing code running
153-
plugins and tell them what to run. There are currently two such code running plugins that quarto
153+
plugins and tell them what to run. There are currently three such code running plugins that quarto
154154
will work with:
155+
155156
1. [molten-nvim](https://github.com/benlubas/molten-nvim) - a code runner that supports the jupyter
156157
kernel, renders output below each code cell, and optionally renders images in the terminal.
157158
2. [vim-slime](https://github.com/jpalardy/vim-slime) - a general purpose code runner with support
158159
for sending code to integrated nvim terminals, tmux panes, and many others.
160+
3. [iron.nvim](https://github.com/Vigemus/iron.nvim) - general purpose code runner and library for
161+
within-neovim REPL interaction in splits or floating windows.
159162

160-
I recommend picking a code runner, setting it up based on its README, and then coming back
163+
We recommend picking a code runner, setting it up based on its respective README and then coming back
161164
to this point to learn how Quarto will augment that code runner.
162165

163-
This plugin enables easily sending code cells to your code runner. There are two different ways to
164-
do this: commands, covered below; and lua functions, covered right here. *By default these functions
165-
will only run cells that are the same language as the current cell.*
166+
This plugin enables easily sending code cells to your code runner.
167+
There are two different ways to do this:
168+
commands, covered below; and lua functions, covered right here.
169+
_By default these functions will only run cells that are the same language as the current cell._
166170

167-
Quarto exposes code running functions through to runner module: `require('quarto.runner')`. Those
171+
Quarto exposes code running functions through to runner module: `require('quarto.runner')`.
172+
Those
168173
functions are:
174+
169175
- `run_cell()` - runs the current cell
170176
- `run_above(multi_lang)` - runs all the cells above the current one, **and** the current one, in order
171177
- `run_below(multi_lang)` - runs all the cells below the current one, **and** the current one, in order
@@ -178,7 +184,6 @@ called with the value `true`, and will only run cells that match the language of
178184
otherwise. As a result, just calling `run_all()` will run all cells that match the language of the
179185
current cell.
180186

181-
182187
Here are some example run mappings:
183188

184189
```lua
@@ -212,6 +217,8 @@ QuartoSendLine
212217
## Recommended Plugins
213218

214219
Quarto works great with a number of plugins in the neovim ecosystem.
215-
You can find my personal (and thus up-to-date) configuration for use with Quarto, R and python here:
220+
You can find my (@jmbuhr) personal (and thus up-to-date) configuration for use with Quarto, R and python here:
216221

217222
<https://github.com/jmbuhr/quarto-nvim-kickstarter>
223+
224+
But remember, the best config is always your own.

doc/quarto.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ or with an empty table.
105105
},
106106
codeRunner = {
107107
enabled = false,
108-
default_method = nil, -- 'molten' or 'slime'
108+
default_method = nil, -- 'molten', 'slime', or 'iron'
109109
ft_runners = {}, -- filetype to runner, ie. `{ python = "molten" }`.
110110
-- Takes precedence over `default_method`
111111
never_run = { "yaml" }, -- filetypes which are never sent to a code runner
@@ -198,7 +198,9 @@ two such code running plugins that quarto will work with: 1. molten-nvim
198198
jupyter kernel, renders output below each code cell, and optionally renders
199199
images in the terminal. 2. vim-slime <https://github.com/jpalardy/vim-slime> -
200200
a general purpose code runner with support for sending code to integrated nvim
201-
terminals, tmux panes, and many others.
201+
terminals, tmux panes, and many others. 3. Iron.nvim
202+
<https://github.com/Vigemus/iron.nvim> general purpose code runner and library
203+
for within-neovim REPL interaction in splits or floating windows.
202204

203205
I recommend picking a code runner, setting it up based on its README, and then
204206
coming back to this point to learn how Quarto will augment that code runner.

lua/quarto/config.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ M.defaultConfig = {
1717
},
1818
codeRunner = {
1919
enabled = true,
20-
default_method = "slime", -- "molten" or "slime"
20+
default_method = "slime", -- "molten", "slime", "iron" or <function>
2121
ft_runners = {}, -- filetype to runner, ie. `{ python = "molten" }`.
2222
-- Takes precedence over `default_method`
2323
never_run = { 'yaml' }, -- filetypes which are never sent to a code runner

lua/quarto/runner/init.lua

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,13 @@ local function send(cell, opts)
6666
runner = ft_runners[cell.lang]
6767
end
6868

69-
if runner ~= nil then
70-
require('quarto.runner.' .. runner).run(cell, opts.ignore_cols)
69+
-- if user passes a fn to config.codeRunner.default_method, we use that.
70+
-- (this also means fns are allowed as values in ft_runners)
71+
-- otherwise we lookup a string for pre-packaged runner function, e.g. "molten"
72+
if type(runner) == "function" then
73+
runner(cell, opts.ignore_cols)
74+
elseif type(runner) == "string" then
75+
require("quarto.runner." .. runner).run(cell, opts.ignore_cols)
7176
else
7277
vim.notify("[Quarto] couldn't find appropriate code runner for language: " .. cell.lang, vim.log.levels.ERROR)
7378
end
@@ -149,7 +154,7 @@ Runner.run_line = function()
149154
send(cell, { ignore_cols = true })
150155
end
151156

152-
-- NOTE: This function will not work the same with molten as it does with slime. Generally, code
157+
-- NOTE: This function will not work the same with molten as it does with slime/iron. Generally, code
153158
-- runners which run code based on the CodeCell range field, will not work when the user selects
154159
-- code across cells. But it will work if a selection is entirely within a cell.
155160
-- Also: This function cannot run multiple languages at once.

lua/quarto/runner/iron.lua

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
local concat = require('quarto.tools').concat
2+
local iron_send = require("iron.core").send
3+
4+
---Run the code cell with iron
5+
---@param cell CodeCell
6+
---@param _ boolean
7+
local function run(cell, _)
8+
local text_lines = concat(cell.text)
9+
local lang = cell.lang or "quarto"
10+
-- forward to iron.send
11+
-- first arg is filetype. if not supplied, iron.core.send would infer "quarto".
12+
-- Iron lets the user map a filetype to a repl binary, e.g. {"python" = "ipython", "r" = "radian"}
13+
-- so we can pass the cell.lang to get the same feel from a .qmd file.
14+
iron_send(lang, text_lines)
15+
end
16+
17+
---@class CodeRunner
18+
local M = { run = run }
19+
20+
return M
21+

lua/quarto/runner/slime.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
local concat = require('quarto.tools').concat
22

3-
---Run the code cell with molten
3+
---Run the code cell with slime
44
---@param cell CodeCell
55
---@param _ boolean
66
local function run(cell, _)

0 commit comments

Comments
 (0)