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 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"
@ -51,7 +50,9 @@ if vim.g.quarto_auto_init_molten_session then
if vim.b["sessionfile"] == nil then
local path = default_buffer_session()
vim.b["sessionfile"] = path
vim.schedule_wrap(startsession(path))
vim.schedule_wrap(function()
startsession(path)
end)
end
end,
})

View file

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

View file

@ -185,14 +185,14 @@ return {
unmap("<localleader>ci")
unmap("<localleader>cV")
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,
ft = { "norg", "quarto", "python" },
keys = {
{ "<leader>vn", ":MoltenInfo<cr>" },
{ "<localleader>ci", ":MoltenInit<cr>" },
{ "<localleader>cJ", ":JupyterStart<cr>", desc = "start jupyter", silent = true },
},
cmd = {
"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
{
"andythigpen/nvim-coverage",