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 -- Start quarto session
local startsession = function(file, args) 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" 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 if args then
file = args[0] file = args[0]
end end

View file

@ -1,41 +1,32 @@
local util = require("lspconfig/util") local util = require("lspconfig.util")
local path = util.path local path = util.path
local T = {} local T = {}
local exepath = vim.fn.exepath 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 -- from https://github.com/ray-x/navigator.lua/issues/247#issue-1465308677
T.get_path = function(workspace) T.get_path = function(workspace)
-- Use activated virtualenv. -- Use activated virtualenv.
if vim.env.VIRTUAL_ENV then 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 end
-- Find and use virtualenv in workspace directory. -- Find and use virtualenv in workspace directory.
for _, pattern in ipairs({ "*", ".*" }) do for _, pattern in ipairs({ "*", ".*" }) do
local match = vim.fn.glob(path.join(workspace, pattern, "pyvenv.cfg")) local match = vim.fn.glob(path.join(workspace, pattern, "pyvenv.cfg"))
local py = "bin" .. path_sep() .. "python"
if match ~= "" then if match ~= "" then
local py = path.join("bin", "python")
match = string.gsub(match, "pyvenv.cfg", py) match = string.gsub(match, "pyvenv.cfg", py)
return match, string.format("venv base folder: %s", match) return match
end end
match = vim.fn.glob(path.join(workspace, pattern, "poetry.lock")) match = vim.fn.glob(path.join(workspace, pattern, "poetry.lock"))
if match ~= "" then if match ~= "" then
local venv_base_folder = vim.fn.trim(vim.fn.system("poetry env info -p")) 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
end end
-- Fallback to system Python. -- 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 end
return T return T