Skip to content

Commit

Permalink
feat: support Iron.nvim and user functions for sending code to REPL (#…
Browse files Browse the repository at this point in the history
…109)



---------

Co-authored-by: Jannik Buhr <[email protected]>
  • Loading branch information
mikedecr and jmbuhr authored Dec 20, 2024
1 parent 5512a57 commit eb98585
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 19 deletions.
31 changes: 19 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,11 @@ require('quarto').setup{
},
},
codeRunner = {
enabled = false,
default_method = nil, -- 'molten' or 'slime'
enabled = true,
default_method = "slime", -- "molten", "slime", "iron" or <function>
ft_runners = {}, -- filetype to runner, ie. `{ python = "molten" }`.
-- Takes precedence over `default_method`
never_run = { "yaml" }, -- filetypes which are never sent to a code runner
-- Takes precedence over `default_method`
never_run = { 'yaml' }, -- filetypes which are never sent to a code runner
},
}
```
Expand Down Expand Up @@ -150,22 +150,28 @@ Or, what might ultimately be the cleaner way of documenting language specific is
## Running Code

Quarto-nvim doesn't run code for you, instead, it will interface with existing code running
plugins and tell them what to run. There are currently two such code running plugins that quarto
plugins and tell them what to run. There are currently three such code running plugins that quarto
will work with:

1. [molten-nvim](https://github.com/benlubas/molten-nvim) - a code runner that supports the jupyter
kernel, renders output below each code cell, and optionally renders images in the terminal.
2. [vim-slime](https://github.com/jpalardy/vim-slime) - a general purpose code runner with support
for sending code to integrated nvim terminals, tmux panes, and many others.
3. [iron.nvim](https://github.com/Vigemus/iron.nvim) - general purpose code runner and library for
within-neovim REPL interaction in splits or floating windows.

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

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

Quarto exposes code running functions through to runner module: `require('quarto.runner')`. Those
Quarto exposes code running functions through to runner module: `require('quarto.runner')`.
Those
functions are:

- `run_cell()` - runs the current cell
- `run_above(multi_lang)` - runs all the cells above the current one, **and** the current one, in order
- `run_below(multi_lang)` - runs all the cells below the current one, **and** the current one, in order
Expand All @@ -178,7 +184,6 @@ called with the value `true`, and will only run cells that match the language of
otherwise. As a result, just calling `run_all()` will run all cells that match the language of the
current cell.


Here are some example run mappings:

```lua
Expand Down Expand Up @@ -212,6 +217,8 @@ QuartoSendLine
## Recommended Plugins

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

<https://github.com/jmbuhr/quarto-nvim-kickstarter>

But remember, the best config is always your own.
6 changes: 4 additions & 2 deletions doc/quarto.txt
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ or with an empty table.
},
codeRunner = {
enabled = false,
default_method = nil, -- 'molten' or 'slime'
default_method = nil, -- 'molten', 'slime', or 'iron'
ft_runners = {}, -- filetype to runner, ie. `{ python = "molten" }`.
-- Takes precedence over `default_method`
never_run = { "yaml" }, -- filetypes which are never sent to a code runner
Expand Down Expand Up @@ -198,7 +198,9 @@ two such code running plugins that quarto will work with: 1. molten-nvim
jupyter kernel, renders output below each code cell, and optionally renders
images in the terminal. 2. vim-slime <https://github.com/jpalardy/vim-slime> -
a general purpose code runner with support for sending code to integrated nvim
terminals, tmux panes, and many others.
terminals, tmux panes, and many others. 3. Iron.nvim
<https://github.com/Vigemus/iron.nvim> general purpose code runner and library
for within-neovim REPL interaction in splits or floating windows.

I recommend picking a code runner, setting it up based on its README, and then
coming back to this point to learn how Quarto will augment that code runner.
Expand Down
2 changes: 1 addition & 1 deletion lua/quarto/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ M.defaultConfig = {
},
codeRunner = {
enabled = true,
default_method = "slime", -- "molten" or "slime"
default_method = "slime", -- "molten", "slime", "iron" or <function>
ft_runners = {}, -- filetype to runner, ie. `{ python = "molten" }`.
-- Takes precedence over `default_method`
never_run = { 'yaml' }, -- filetypes which are never sent to a code runner
Expand Down
11 changes: 8 additions & 3 deletions lua/quarto/runner/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,13 @@ local function send(cell, opts)
runner = ft_runners[cell.lang]
end

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

-- NOTE: This function will not work the same with molten as it does with slime. Generally, code
-- NOTE: This function will not work the same with molten as it does with slime/iron. Generally, code
-- runners which run code based on the CodeCell range field, will not work when the user selects
-- code across cells. But it will work if a selection is entirely within a cell.
-- Also: This function cannot run multiple languages at once.
Expand Down
21 changes: 21 additions & 0 deletions lua/quarto/runner/iron.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
local concat = require('quarto.tools').concat
local iron_send = require("iron.core").send

---Run the code cell with iron
---@param cell CodeCell
---@param _ boolean
local function run(cell, _)
local text_lines = concat(cell.text)
local lang = cell.lang or "quarto"
-- forward to iron.send
-- first arg is filetype. if not supplied, iron.core.send would infer "quarto".
-- Iron lets the user map a filetype to a repl binary, e.g. {"python" = "ipython", "r" = "radian"}
-- so we can pass the cell.lang to get the same feel from a .qmd file.
iron_send(lang, text_lines)
end

---@class CodeRunner
local M = { run = run }

return M

2 changes: 1 addition & 1 deletion lua/quarto/runner/slime.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
local concat = require('quarto.tools').concat

---Run the code cell with molten
---Run the code cell with slime
---@param cell CodeCell
---@param _ boolean
local function run(cell, _)
Expand Down

0 comments on commit eb98585

Please sign in to comment.