Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions nvim/lua/helper-ipython.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
local M = {}

-- Detects the start and end lines of the current IPython cell.
function M.detect_cell_range()
local cur = vim.fn.line(".")
local last = vim.fn.line("$")

-- Find the start of the cell by searching upwards for # %%
local start = cur
for l = cur, 1, -1 do
local line = vim.fn.getline(l)
if line:match("^%s*# %%%%") and l ~= cur then
start = l + 1
Comment on lines +10 to +13

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Treat current cell marker as a boundary

When the cursor is on a # %% marker line, the l ~= cur guard prevents the current marker from being recognized as the start of the cell. In that situation, start is taken from the previous marker and finish from the next marker, so the range includes the marker line and can send the previous cell instead of the intended cell below the marker. This only happens when the user triggers the mapping while their cursor is on the marker line, but it will send the wrong cell contents in that common workflow.

Useful? React with 👍 / 👎.

break
elseif l == 1 then
start = 1
end
end

-- Find the end of the cell by searching downwards for # %%
local finish = cur
for l = cur + 1, last do
local line = vim.fn.getline(l)
if line:match("^%s*# %%%%") then
finish = l - 1
break
elseif l == last then
finish = last
end
end

return start, finish
end

return M
19 changes: 19 additions & 0 deletions nvim/lua/plugins.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,25 @@ return {
end,
},

-- Sends buffer content to console REPL.
{
"jpalardy/vim-slime",
config = function()
vim.g.slime_target = "tmux"
vim.g.slime_python_ipython = 1
vim.g.slime_default_config = {
socket_name = "default",
target_pane = "{last}",
}
vim.g.slime_dont_ask_default = 1

vim.keymap.set("n", "<Leader>ip", function()
local s, e = require("helper-ipython").detect_cell_range()
vim.cmd(string.format("%d,%dSlimeSend", s, e))
end)
end,
},

-- Lua library for nvim.
{ "nvim-lua/plenary.nvim" },

Expand Down