diff --git a/README.md b/README.md index e8b417c..0fa06de 100644 --- a/README.md +++ b/README.md @@ -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 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 }, } ``` @@ -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 @@ -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 @@ -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: + +But remember, the best config is always your own. diff --git a/doc/quarto.txt b/doc/quarto.txt index f0c3e3e..ba775b4 100644 --- a/doc/quarto.txt +++ b/doc/quarto.txt @@ -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 @@ -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 - 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 + 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. diff --git a/lua/quarto/config.lua b/lua/quarto/config.lua index c58c70f..ba189bf 100644 --- a/lua/quarto/config.lua +++ b/lua/quarto/config.lua @@ -17,7 +17,7 @@ M.defaultConfig = { }, codeRunner = { enabled = true, - default_method = "slime", -- "molten" or "slime" + default_method = "slime", -- "molten", "slime", "iron" or 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 diff --git a/lua/quarto/runner/init.lua b/lua/quarto/runner/init.lua index 8cd838e..85a7093 100644 --- a/lua/quarto/runner/init.lua +++ b/lua/quarto/runner/init.lua @@ -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 @@ -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. diff --git a/lua/quarto/runner/iron.lua b/lua/quarto/runner/iron.lua new file mode 100644 index 0000000..fd20796 --- /dev/null +++ b/lua/quarto/runner/iron.lua @@ -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 + diff --git a/lua/quarto/runner/slime.lua b/lua/quarto/runner/slime.lua index aef4d16..01df41f 100644 --- a/lua/quarto/runner/slime.lua +++ b/lua/quarto/runner/slime.lua @@ -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, _)