nvim/lua/custom/plugins/jupytext.lua
Jeremie Fraeys 6c762c436a
Some checks failed
Luacheck / luacheck (push) Failing after 4s
StyLua / stylua (push) Failing after 2s
fix: endx typo in lsp-servers.lua
2026-02-08 15:08:04 -05:00

70 lines
2.1 KiB
Lua

return {
'goerz/jupytext.vim',
lazy = false,
config = function()
vim.g.jupytext_fmt = 'py:percent'
vim.g.jupytext_enable = 1
vim.g.jupytext_pairing = 1
local jupytext_bin = vim.fn.exepath('jupytext')
if jupytext_bin ~= nil and jupytext_bin ~= '' then
vim.g.jupytext_command = jupytext_bin
else
local preferred_python3 = '/opt/homebrew/bin/python3'
local python3_bin = preferred_python3
if vim.fn.executable(preferred_python3) ~= 1 then
python3_bin = vim.fn.exepath('python3')
end
if python3_bin ~= nil and python3_bin ~= '' then
vim.g.jupytext_command = python3_bin .. ' -m jupytext'
else
vim.schedule(function()
vim.notify(
'Jupytext: no `jupytext` or `python3` found in PATH. Install jupytext (e.g. `/opt/homebrew/bin/python3 -m pip install --user jupytext`) or set `vim.g.jupytext_command`.',
vim.log.levels.WARN
)
end)
end
end
local jupytext_group = vim.api.nvim_create_augroup('custom_jupytext', { clear = true })
vim.api.nvim_create_autocmd('BufWritePost', {
group = jupytext_group,
pattern = '*.py',
callback = function()
local ipynb_file = vim.fn.expand('%:r') .. '.ipynb'
if vim.fn.filereadable(ipynb_file) == 1 then
vim.cmd('silent! Jupytext --sync')
end
end,
})
vim.api.nvim_create_autocmd('BufWritePost', {
group = jupytext_group,
pattern = '*.ipynb',
callback = function()
local py_file = vim.fn.expand('%:r') .. '.py'
if vim.fn.filereadable(py_file) == 1 then
vim.cmd('silent! Jupytext --sync')
end
end,
})
vim.api.nvim_create_autocmd({ 'BufReadPost', 'BufNewFile' }, {
group = jupytext_group,
pattern = '*.ipynb',
callback = function()
local py_file = vim.fn.expand('%:r') .. '.py'
if vim.fn.filereadable(py_file) == 0 then
vim.cmd('silent! Jupytext --to py:percent')
end
if vim.fn.filereadable(py_file) == 1 then
vim.cmd('silent! edit ' .. vim.fn.fnameescape(py_file))
end
end,
})
end,
}