From 0033116a73da504dd14c64a256be70dc2ae8715c Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Tue, 29 Aug 2023 22:37:38 +0200 Subject: [PATCH] nvim: Refactor py environment detection function --- nvim/.config/nvim/after/ftplugin/quarto.lua | 7 +++++-- nvim/.config/nvim/lua/util/pyenv.lua | 21 ++++++--------------- 2 files changed, 11 insertions(+), 17 deletions(-) diff --git a/nvim/.config/nvim/after/ftplugin/quarto.lua b/nvim/.config/nvim/after/ftplugin/quarto.lua index 94fd38f..edd5a82 100644 --- a/nvim/.config/nvim/after/ftplugin/quarto.lua +++ b/nvim/.config/nvim/after/ftplugin/quarto.lua @@ -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 diff --git a/nvim/.config/nvim/lua/util/pyenv.lua b/nvim/.config/nvim/lua/util/pyenv.lua index 8e250b5..b36e474 100644 --- a/nvim/.config/nvim/lua/util/pyenv.lua +++ b/nvim/.config/nvim/lua/util/pyenv.lua @@ -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