diff --git a/nvim/.config/nvim/after/ftplugin/quarto.lua b/nvim/.config/nvim/after/ftplugin/quarto.lua index 170805a..213108e 100644 --- a/nvim/.config/nvim/after/ftplugin/quarto.lua +++ b/nvim/.config/nvim/after/ftplugin/quarto.lua @@ -9,9 +9,22 @@ local default_buffer_session = function() return temp_path end +-- TODO: Puths kernel into local file dir currently, +-- could take an argument to put it into temporary dir instead. +local kernel_filename = function() + local buf = vim.api.nvim_buf_get_name(0) + if not buf or buf:match("^$") then + vim.fn.tempname() + end + local path, name = buf:match("(.-)([^\\/]-%.?([^%.\\/]*))$") + + return path .. "." .. name .. ".kernel.json" +end + -- Start quarto session -local startsession = function(file, args) - file = file or default_buffer_session() +local startsession = function(opts) + local args = opts.fargs + local kernel_filen = args[1] or kernel_filename() local path = require("core.util").get_python_venv_bin() if not path then @@ -19,39 +32,55 @@ local startsession = function(file, args) end vim.g["python3_host_prog"] = path - if vim.fn.executable("jupyter-console") ~= 1 then + -- simply attach to existing if exists + if vim.fn.filereadable(kernel_filen) == 1 then + print(kernel_filen) + vim.cmd("MoltenInit " .. kernel_filen) return end - if args then - file = args[0] + -- look for session maker + local exec_name = "jupyter-kernel" + local exec_path = (require("core.util").get_python_venv_basefolder() or "") .. "/bin/" .. exec_name + if vim.fn.filereadable(exec_path) ~= 1 then + exec_path = exec_name + if vim.fn.executable(exec_path) ~= 1 then + return + end end + + -- make our own session local once = false - vim.fn.jobstart({ "jupyter", "console", "-f", file }, { - on_stdout = function(_) + vim.fn.jobstart({ exec_path, "--KernelManager.connection_file", kernel_filen }, { + on_stderr = function(_, data) if not once then - vim.cmd("MoltenInit " .. file) + for _, v in pairs(data) do + if v:find("connect a client") then + vim.cmd("MoltenInit " .. kernel_filen) + once = true + end + end end - once = true end, on_exit = function(_) - vim.notify(string.format("jupyter kernel stopped: %s", file), vim.log.levels.INFO) + vim.notify(string.format("jupyter kernel stopped: %s", kernel_filen), vim.log.levels.INFO) end, stdin = nil, }) end -vim.api.nvim_create_user_command("JupyterStart", function() - startsession(vim.b["sessionfile"] or default_buffer_session()) -end, {}) + +vim.api.nvim_create_user_command("JupyterStart", function(opts) + startsession(opts) +end, { nargs = "?" }) if vim.g.quarto_auto_init_molten_session then - vim.api.nvim_create_autocmd({ "InsertEnter", "BufEnter" }, { + vim.api.nvim_create_autocmd({ "BufEnter" }, { callback = function() if vim.b["sessionfile"] == nil then - local path = default_buffer_session() + local path = kernel_filename() vim.b["sessionfile"] = path - vim.schedule_wrap(function() - startsession(path) + vim.schedule(function() + startsession({ fargs = { path } }) end) end end,