From 5c06715e46576f2682e47e153ea12863533bd135 Mon Sep 17 00:00:00 2001 From: Anshuman Date: Wed, 22 May 2024 18:24:01 +0800 Subject: [PATCH 1/5] feat: config.repl_provider can be a function --- README.md | 3 ++- afile.py | 5 +++++ doc/NotebookNavigator.txt | 2 ++ lua/notebook-navigator/core.lua | 4 ++-- lua/notebook-navigator/init.lua | 22 +++++++++++++--------- lua/notebook-navigator/repls.lua | 4 +++- 6 files changed, 27 insertions(+), 13 deletions(-) create mode 100644 afile.py diff --git a/README.md b/README.md index 1068b09..f9aca7c 100644 --- a/README.md +++ b/README.md @@ -158,8 +158,9 @@ Any options that are not specified when calling `setup` will take on their defau }, -- The repl plugin with which to interface -- Current options: "iron" for iron.nvim, "toggleterm" for toggleterm.nvim, - -- "molten" for molten-nvim or "auto" which checks which of the above are + -- "molten" for molten-nvim or "auto" which checks which of the above are -- installed + -- (start_line, end_line, repl_args, cell_marker) -> boolean (success) repl_provider = "auto", -- Syntax based highlighting. If you don't want to install mini.hipattners or -- enjoy a more minimalistic look diff --git a/afile.py b/afile.py new file mode 100644 index 0000000..4886f86 --- /dev/null +++ b/afile.py @@ -0,0 +1,5 @@ +# %% +import numpy as np + +# %% +print("allo") diff --git a/doc/NotebookNavigator.txt b/doc/NotebookNavigator.txt index be37209..043a2de 100644 --- a/doc/NotebookNavigator.txt +++ b/doc/NotebookNavigator.txt @@ -184,6 +184,8 @@ Default values: -- Current options: "iron" for iron.nvim, "toggleterm" for toggleterm.nvim, -- "molten" for molten-nvim or "auto" which checks which of the above are -- installed + -- or provide a function with signature + -- (start_line, end_line, repl_args, cell_marker) -> boolean (success) repl_provider = "auto", -- Syntax based highlighting. If you don't want to install mini.hipattners or -- enjoy a more minimalistic look diff --git a/lua/notebook-navigator/core.lua b/lua/notebook-navigator/core.lua index c563c35..cb28307 100644 --- a/lua/notebook-navigator/core.lua +++ b/lua/notebook-navigator/core.lua @@ -125,7 +125,7 @@ M.run_all_cells = function(repl_provider, repl_args) local buf_length = vim.api.nvim_buf_line_count(0) local repl = get_repl(repl_provider) - repl(1, buf_length, repl_args) + return repl(1, buf_length, repl_args) end M.run_cells_below = function(cell_marker, repl_provider, repl_args) @@ -133,7 +133,7 @@ M.run_cells_below = function(cell_marker, repl_provider, repl_args) local cell_object = miniai_spec("i", cell_marker) local repl = get_repl(repl_provider) - repl(cell_object.from.line, buf_length, repl_args) + return repl(cell_object.from.line, buf_length, repl_args) end M.merge_cell = function(dir, cell_marker) diff --git a/lua/notebook-navigator/init.lua b/lua/notebook-navigator/init.lua index c1c02de..d777bb5 100644 --- a/lua/notebook-navigator/init.lua +++ b/lua/notebook-navigator/init.lua @@ -267,6 +267,8 @@ M.config = { -- The repl plugin with which to interface -- Current options: "iron" for iron.nvim, "toggleterm" for toggleterm.nvim, -- or "auto" which checks which of the above are installed + -- installed + -- (start_line, end_line, repl_args, cell_marker) -> boolean (success) repl_provider = "auto", -- Syntax based highlighting. If you don't want to install mini.hipattners or -- enjoy a more minimalistic look @@ -288,21 +290,21 @@ M.setup = function(config) vim.validate("config", config, "table", true) M.config = vim.tbl_deep_extend("force", M.config, config or {}) - vim.validate("cell_markers", M.config.cell_markers, "table") - vim.validate("activate_hydra_keys", M.config.activate_hydra_keys, "string", true) - vim.validate("show_hydra_hint", M.config.show_hydra_hint, "boolean") - vim.validate("hydra_keys", M.config.hydra_keys, "table") + vim.validate("cell_markers", M.config.cell_markers, "table") + vim.validate("activate_hydra_keys", M.config.activate_hydra_keys, "string", true) + vim.validate("show_hydra_hint", M.config.show_hydra_hint, "boolean") + vim.validate("hydra_keys", M.config.hydra_keys, "table") vim.validate("config.hydra_keys.comment", M.config.hydra_keys.comment, "string") vim.validate("config.hydra_keys.run", M.config.hydra_keys.run, "string") vim.validate("config.hydra_keys.run_and_move", M.config.hydra_keys.run_and_move, "string") vim.validate("config.hydra_keys.move_up", M.config.hydra_keys.move_up, "string") - vim.validate("config.hydra_keys.move_down", M.config.hydra_keys.move_down, "string") - vim.validate("config.hydra_keys.add_cell_before", M.config.hydra_keys.add_cell_before, "string") - vim.validate("config.hydra_keys.add_cell_after", M.config.hydra_keys.add_cell_after, "string") + vim.validate("config.hydra_keys.move_down", M.config.hydra_keys.move_down, "string") + vim.validate("config.hydra_keys.add_cell_before", M.config.hydra_keys.add_cell_before, "string") + vim.validate("config.hydra_keys.add_cell_after", M.config.hydra_keys.add_cell_after, "string") for ft, marker in pairs(M.config.cell_markers) do - vim.validate("config.cell_markers." .. ft, marker, "string") + vim.validate("config.cell_markers." .. ft, marker, "string") end if (not got_hydra) and (M.config.activate_hydra_keys ~= nil) then @@ -310,7 +312,9 @@ M.setup = function(config) end if - M.config.repl_provider ~= "auto" and not utils.has_value(utils.available_repls, M.config.repl_provider) + M.config.repl_provider ~= "auto" + and type(M.config.repl_provider) == "string" + and not utils.has_value(utils.available_repls, M.config.repl_provider) then vim.notify("[NotebookNavigator] The requested repl (" .. M.config.repl_provider .. ") is not available.") end diff --git a/lua/notebook-navigator/repls.lua b/lua/notebook-navigator/repls.lua index ed9d52d..e3212d2 100644 --- a/lua/notebook-navigator/repls.lua +++ b/lua/notebook-navigator/repls.lua @@ -88,8 +88,10 @@ local get_repl = function(repl_provider) chosen_repl = repls[r] break end - else + elseif type(repl_provider) == "string" then chosen_repl = repls[repl_provider] + elseif type(repl_provider) == "function" then + chosen_repl = repl_provider end -- Check if we actuall got out a supported repl From 73cc5e812294593e3d43d131714f08e952a9d57f Mon Sep 17 00:00:00 2001 From: Anshuman Date: Wed, 22 May 2024 18:25:04 +0800 Subject: [PATCH 2/5] feat: passing config options to `activate_hydra` `config.buffer` for buffer local hydra `config.hydra` for everything else --- lua/notebook-navigator/init.lua | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lua/notebook-navigator/init.lua b/lua/notebook-navigator/init.lua index d777bb5..1f979a9 100644 --- a/lua/notebook-navigator/init.lua +++ b/lua/notebook-navigator/init.lua @@ -221,11 +221,15 @@ local function activate_hydra(config) local hydra_config = { name = "NotebookNavigator", mode = { "n" }, - config = { + config = vim.tbl_extend("force", { invoke_on_body = true, color = "pink", hint = { float_opts = { border = "rounded" } }, - }, + -- TODO: Make sure these are clearly specified in README and config + -- TODO: Where is config.hydra defined? + buffer = config.buffer, + desc = config.desc, + }, config.hydra), body = config.activate_hydra_keys, heads = active_hydra_heads, } From 15efffa3952f6c66500f1a45d14eae2be57486a5 Mon Sep 17 00:00:00 2001 From: Anshuman Date: Fri, 24 May 2024 23:13:13 +0800 Subject: [PATCH 3/5] feat: more options for move_cell --- README.md | 2 ++ lua/notebook-navigator/core.lua | 11 ++++++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index f9aca7c..95f0f59 100644 --- a/README.md +++ b/README.md @@ -211,6 +211,8 @@ keymaps on the plugin configuration or even map them on they Hydra mode as long as you take heed of the [advice given above](#current-limitations). - `move_cell(dir)`: Move up or done a cell in the `u`p or `d`own direction. + - `D` to move to the first non-empty line of the next cell, likewise `U` + - `e` to move to the last non-empty line of the next cell, `E` for previous cell - `run_cell(repl_args)`: Run the current cell. You may optionally pass a table of `repl_args` that will be forwarded to the REPL. For the details of what is forwarded exactly and how it is used check `repls.lua` and look for your REPL diff --git a/lua/notebook-navigator/core.lua b/lua/notebook-navigator/core.lua index cb28307..65e9925 100644 --- a/lua/notebook-navigator/core.lua +++ b/lua/notebook-navigator/core.lua @@ -7,17 +7,26 @@ local M = {} M.move_cell = function(dir, cell_marker) local search_res local result + local mod = dir:sub(2, 2) - if dir == "d" then + if dir == "d" or dir == "D" or dir == "e" then search_res = vim.fn.search("^" .. cell_marker, "W") if search_res == 0 then result = "last" + elseif dir == "D" then + vim.fn.search("^\\w", "W") + elseif dir == "e" then + vim.fn.search("^\\w", "bW") end else search_res = vim.fn.search("^" .. cell_marker, "bW") if search_res == 0 then result = "first" vim.api.nvim_win_set_cursor(0, { 1, 0 }) + elseif dir == "U" then + vim.fn.search("^\\w", "W") + elseif dir == "E" then + vim.fn.search("^\\w", "bW") end end From ed2cce4178b5b172eb969194160a8d83cead48ec Mon Sep 17 00:00:00 2001 From: Thomas Vandal Date: Tue, 23 Dec 2025 07:04:58 -0500 Subject: [PATCH 4/5] Only trigger supported repl warning if it is not a function --- lua/notebook-navigator/repls.lua | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lua/notebook-navigator/repls.lua b/lua/notebook-navigator/repls.lua index e3212d2..4001c75 100644 --- a/lua/notebook-navigator/repls.lua +++ b/lua/notebook-navigator/repls.lua @@ -78,10 +78,11 @@ repls.no_repl = function(_) end local get_repl = function(repl_provider) local available_repls = utils.available_repls - if #available_repls == 0 then + if type(repl_provider) ~= "function" and #available_repls == 0 then vim.notify("[NotebookNavigator] No supported REPLs available.\nMost functionality will error out.") return nil end + local chosen_repl = nil if repl_provider == "auto" then for _, r in ipairs(available_repls) do @@ -94,7 +95,7 @@ local get_repl = function(repl_provider) chosen_repl = repl_provider end - -- Check if we actuall got out a supported repl + -- Check if we actually got out a supported repl if chosen_repl == nil then vim.notify("[NotebookNavigator] The provided repl, " .. repl_provider .. ", is not supported.") chosen_repl = repls["no_repl"] From 2c145bb46218e3b7b48004f14204051200160ac0 Mon Sep 17 00:00:00 2001 From: Thomas Vandal Date: Tue, 23 Dec 2025 07:44:50 -0500 Subject: [PATCH 5/5] Use a single hydra_config entry instead of buffer, desc, etc. --- lua/notebook-navigator/init.lua | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/lua/notebook-navigator/init.lua b/lua/notebook-navigator/init.lua index 1f979a9..1f8637e 100644 --- a/lua/notebook-navigator/init.lua +++ b/lua/notebook-navigator/init.lua @@ -225,13 +225,9 @@ local function activate_hydra(config) invoke_on_body = true, color = "pink", hint = { float_opts = { border = "rounded" } }, - -- TODO: Make sure these are clearly specified in README and config - -- TODO: Where is config.hydra defined? - buffer = config.buffer, - desc = config.desc, - }, config.hydra), - body = config.activate_hydra_keys, + }, config.hydra_config or {}), heads = active_hydra_heads, + body = config.activate_hydra_keys, } if config.show_hydra_hint then hydra_config.hint = hydra_hint @@ -255,6 +251,8 @@ M.config = { -- If not `nil` the keymap defined in the string will activate the hydra head activate_hydra_keys = nil, + -- Optional `config` table for the Hydra head + hydra_config = nil, -- If `true` a hint panel will be shown when the hydra head is active show_hydra_hint = true, -- Mappings while the hydra head is active. @@ -296,6 +294,7 @@ M.setup = function(config) vim.validate("cell_markers", M.config.cell_markers, "table") vim.validate("activate_hydra_keys", M.config.activate_hydra_keys, "string", true) + vim.validate("hydra_config", M.config.hydra_config, { "table" }, true) vim.validate("show_hydra_hint", M.config.show_hydra_hint, "boolean") vim.validate("hydra_keys", M.config.hydra_keys, "table")