From ca0e08fab51b27cf95a932abb67ba8220b376256 Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Mon, 12 Aug 2024 21:19:26 +0200 Subject: [PATCH] nvim: Fix python venv utility function --- nvim/.config/nvim/after/ftplugin/quarto.lua | 5 ++- nvim/.config/nvim/lua/core/util.lua | 41 +++++++++++-------- .../nvim/lua/plugins/data_analysis.lua | 4 +- nvim/.config/nvim/lua/plugins/testing.lua | 2 +- 4 files changed, 29 insertions(+), 23 deletions(-) diff --git a/nvim/.config/nvim/after/ftplugin/quarto.lua b/nvim/.config/nvim/after/ftplugin/quarto.lua index 565bf04..170805a 100644 --- a/nvim/.config/nvim/after/ftplugin/quarto.lua +++ b/nvim/.config/nvim/after/ftplugin/quarto.lua @@ -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, }) diff --git a/nvim/.config/nvim/lua/core/util.lua b/nvim/.config/nvim/lua/core/util.lua index 93278d4..043136a 100644 --- a/nvim/.config/nvim/lua/core/util.lua +++ b/nvim/.config/nvim/lua/core/util.lua @@ -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")) - if match ~= "" then - match = string.gsub(match, "pyvenv.cfg", "") - venv_path = match - return venv_path - 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")) - venv_path = venv_base_folder - return venv_path + 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_cached = match + return venv_path_cached + end + -- Look upwards for file, it is only ever in proj root dir + match = vim.fn.findfile("poetry.lock", (workspace or ".") .. ";") + if match ~= "" then + 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 diff --git a/nvim/.config/nvim/lua/plugins/data_analysis.lua b/nvim/.config/nvim/lua/plugins/data_analysis.lua index c922aaa..6719b50 100644 --- a/nvim/.config/nvim/lua/plugins/data_analysis.lua +++ b/nvim/.config/nvim/lua/plugins/data_analysis.lua @@ -185,14 +185,14 @@ return { unmap("ci") unmap("cV") local map = vim.keymap.set - map("n", "cI", ":MoltenInit", { desc = "init molten", silent = true }) + map("n", "cJ", ":JupyterStart", { desc = "start jupyter", silent = true }) end, }) end, ft = { "norg", "quarto", "python" }, keys = { { "vn", ":MoltenInfo" }, - { "ci", ":MoltenInit" }, + { "cJ", ":JupyterStart", desc = "start jupyter", silent = true }, }, cmd = { "MoltenInfo", diff --git a/nvim/.config/nvim/lua/plugins/testing.lua b/nvim/.config/nvim/lua/plugins/testing.lua index 5d24fe5..208fc6c 100644 --- a/nvim/.config/nvim/lua/plugins/testing.lua +++ b/nvim/.config/nvim/lua/plugins/testing.lua @@ -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",