nvim: Fix python venv utility function

This commit is contained in:
Marty Oehme 2024-08-12 21:19:26 +02:00
parent bfbe4c36cd
commit ca0e08fab5
Signed by: Marty
GPG key ID: EDBF2ED917B2EF6A
4 changed files with 29 additions and 23 deletions

View file

@ -1,4 +1,3 @@
local default_buffer_session = function() local default_buffer_session = function()
local buffer_path = vim.api.nvim_buf_get_name(0) or vim.fn.tempname() local buffer_path = vim.api.nvim_buf_get_name(0) or vim.fn.tempname()
local temp_path = vim.fn.stdpath("run") .. "/molten-sessions" .. buffer_path .. ".json" local temp_path = vim.fn.stdpath("run") .. "/molten-sessions" .. buffer_path .. ".json"
@ -51,7 +50,9 @@ if vim.g.quarto_auto_init_molten_session then
if vim.b["sessionfile"] == nil then if vim.b["sessionfile"] == nil then
local path = default_buffer_session() local path = default_buffer_session()
vim.b["sessionfile"] = path vim.b["sessionfile"] = path
vim.schedule_wrap(startsession(path)) vim.schedule_wrap(function()
startsession(path)
end)
end end
end, end,
}) })

View file

@ -51,32 +51,37 @@ function T.get_python_venv_bin(workspace)
return path_join(pyenv, "bin", "python") return path_join(pyenv, "bin", "python")
end end
-- cache path so we can call it multiple times -- cache path so we can call it multiple times
local venv_path = "" local venv_path_cached = ""
-- return the current python environment path -- return the current python environment path
function T.get_python_venv_basefolder(workspace) function T.get_python_venv_basefolder(workspace)
if venv_path and venv_path ~= "" then if venv_path_cached and venv_path_cached ~= "" then
return venv_path return venv_path_cached
end end
-- Use activated virtualenv. -- Use activated virtualenv.
if vim.env.VIRTUAL_ENV then if vim.env.VIRTUAL_ENV then
venv_path = vim.env.VIRTUAL_ENV venv_path_cached = vim.env.VIRTUAL_ENV
return venv_path return venv_path_cached
end end
-- Find and use virtualenv in workspace directory. local match
for _, pattern in ipairs({ "*", ".*" }) do -- Look downwards for file, can be nested. Limit to 1 depth for speed rn
local match = vim.fn.glob(path_join(workspace, pattern, "pyvenv.cfg")) -- TODO: Maybe not hardcode 1-depth but allow choice.
if match ~= "" then match = vim.fn.findfile("pyvenv.cfg", "**1")
match = string.gsub(match, "pyvenv.cfg", "") if match ~= "" then
venv_path = match match = string.gsub(match, "/pyvenv.cfg", "")
return venv_path venv_path_cached = match
end return venv_path_cached
match = vim.fn.glob(path_join(workspace, pattern, "poetry.lock")) end
if match ~= "" then -- Look upwards for file, it is only ever in proj root dir
local venv_base_folder = vim.fn.trim(vim.fn.system("poetry env info -p")) match = vim.fn.findfile("poetry.lock", (workspace or ".") .. ";")
venv_path = venv_base_folder if match ~= "" then
return venv_path local obj = vim.system({ "poetry", "env", "info", "-p" }, { text = true }):wait()
if obj.code ~= 0 then
return
end end
local venv_base_folder = obj.stdout:match("^%s*(.-)%s*$")
venv_path_cached = venv_base_folder
return venv_path_cached
end end
end end

View file

@ -185,14 +185,14 @@ return {
unmap("<localleader>ci") unmap("<localleader>ci")
unmap("<localleader>cV") unmap("<localleader>cV")
local map = vim.keymap.set local map = vim.keymap.set
map("n", "<localleader>cI", ":MoltenInit<cr>", { desc = "init molten", silent = true }) map("n", "<localleader>cJ", ":JupyterStart<cr>", { desc = "start jupyter", silent = true })
end, end,
}) })
end, end,
ft = { "norg", "quarto", "python" }, ft = { "norg", "quarto", "python" },
keys = { keys = {
{ "<leader>vn", ":MoltenInfo<cr>" }, { "<leader>vn", ":MoltenInfo<cr>" },
{ "<localleader>ci", ":MoltenInit<cr>" }, { "<localleader>cJ", ":JupyterStart<cr>", desc = "start jupyter", silent = true },
}, },
cmd = { cmd = {
"MoltenInfo", "MoltenInfo",

View file

@ -103,7 +103,7 @@ return {
}, },
}, },
}, },
-- TODO needs to pick up poetry env for python, -- TODO: needs to pick up poetry env for python,
-- currently just hard-codes running through poetry -- currently just hard-codes running through poetry
{ {
"andythigpen/nvim-coverage", "andythigpen/nvim-coverage",