nvim: Refactor py environment detection function

This commit is contained in:
Marty Oehme 2023-08-29 22:37:38 +02:00
parent c96a1feec4
commit 0033116a73
Signed by: Marty
GPG Key ID: EDBF2ED917B2EF6A
2 changed files with 11 additions and 17 deletions

View File

@ -1,8 +1,11 @@
-- Start quarto session
local startsession = function(file, args)
local path, _ = require("util").get_python_venv()
vim.g["python3_host_prog"] = path
file = file or "/tmp/jupyter-magma-session.json"
local path = require("util").get_python_venv()
vim.g["python3_host_prog"] = path
if vim.fn.executable('jupyter-console') ~= 1 then return end
if args then
file = args[0]
end

View File

@ -1,41 +1,32 @@
local util = require("lspconfig/util")
local util = require("lspconfig.util")
local path = util.path
local T = {}
local exepath = vim.fn.exepath
local path_sep = function()
local is_win = vim.loop.os_uname().sysname:find("Windows")
if is_win then
return "\\"
else
return "/"
end
end
-- from https://github.com/ray-x/navigator.lua/issues/247#issue-1465308677
T.get_path = function(workspace)
-- Use activated virtualenv.
if vim.env.VIRTUAL_ENV then
return path.join(vim.env.VIRTUAL_ENV, "bin", "python"), "virtual env"
return path.join(vim.env.VIRTUAL_ENV, "bin", "python")
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 py = "bin" .. path_sep() .. "python"
if match ~= "" then
local py = path.join("bin", "python")
match = string.gsub(match, "pyvenv.cfg", py)
return match, string.format("venv base folder: %s", match)
return match
end
match = vim.fn.glob(path.join(workspace, pattern, "poetry.lock"))
if match ~= "" then
local venv_base_folder = vim.fn.trim(vim.fn.system("poetry env info -p"))
return path.join(venv_base_folder, "bin", "python"), string.format("venv base folder: %s", venv_base_folder)
return path.join(venv_base_folder, "bin", "python")
end
end
-- Fallback to system Python.
return exepath("python3") or exepath("python") or "python", "fallback to system python path"
return exepath("python3") or exepath("python") or "python"
end
return T