diff --git a/README.md b/README.md index 95f0f59..32cfd42 100644 --- a/README.md +++ b/README.md @@ -222,6 +222,7 @@ provider. below (`dir='d'`). - `run_all_cells(repl_args)`: Run all the file. - `run_cells_below(repl_args)`: Run the current cell and all of the ones below. +- `run_cells_above(repl_args)`: Run the all cells above the current one. - `comment_cell`: Comment the code inside the current cell. - `add_cell_below`: Add a cell marker below the current cell. - `add_cell_after`: Same as above (deprecated). diff --git a/lua/notebook-navigator/core.lua b/lua/notebook-navigator/core.lua index 65e9925..f46f13d 100644 --- a/lua/notebook-navigator/core.lua +++ b/lua/notebook-navigator/core.lua @@ -137,6 +137,18 @@ M.run_all_cells = function(repl_provider, repl_args) return repl(1, buf_length, repl_args) end +M.run_cells_above = function(cell_marker, repl_provider, repl_args) + local start_line = 1 + local cell_object = miniai_spec("i", cell_marker) + + local repl = get_repl(repl_provider) + + if cell_object.from.line > 1 then + local previous_cell_end = cell_object.from.line - 1 + repl(start_line, previous_cell_end, repl_args) + end +end + M.run_cells_below = function(cell_marker, repl_provider, repl_args) local buf_length = vim.api.nvim_buf_line_count(0) local cell_object = miniai_spec("i", cell_marker) diff --git a/lua/notebook-navigator/init.lua b/lua/notebook-navigator/init.lua index 1f8637e..7adb9e1 100644 --- a/lua/notebook-navigator/init.lua +++ b/lua/notebook-navigator/init.lua @@ -110,6 +110,13 @@ M.run_all_cells = function(repl_args) core.run_all_cells(M.config.repl_provider, repl_args) end +--- Run all cells above the current cell +--- +---@param repl_args table|nil Optional config for the repl. +M.run_cells_above = function(repl_args) + core.run_cells_above(cell_marker(), M.config.repl_provider, repl_args) +end + --- Run all cells below (including current cell) --- ---@param repl_args table|nil Optional config for the repl.