From 90a7677d2ef8af867ebd1fb2db4a81d83583be51 Mon Sep 17 00:00:00 2001 From: Joshua Cold Date: Sat, 30 Aug 2025 19:05:13 -0600 Subject: [PATCH] fix: python.nvim detect project changes for venv adjustments automatically --- doc/python.txt | 2 +- lua/lualine/components/python.lua | 13 ++++++++++--- lua/python/config.lua | 2 +- lua/python/hatch/commands.lua | 1 - lua/python/init.lua | 4 ++-- lua/python/venv/create.lua | 2 +- lua/python/venv/detect.lua | 2 +- lua/python/venv/interpreters.lua | 1 - scripts/minimal_init.lua | 2 +- tests/test_text_actions.lua | 1 + 10 files changed, 18 insertions(+), 12 deletions(-) diff --git a/doc/python.txt b/doc/python.txt index b3f46c1..64d786f 100644 --- a/doc/python.txt +++ b/doc/python.txt @@ -78,7 +78,7 @@ Default values: auto_venv_lsp_attach_patterns = { "*.py" }, -- Filetypes to activate commands for python.nvim - command_setup_filetypes = { "python" }, + command_setup_buf_pattern = { "*.py" }, -- Load python.nvim python snippets python_lua_snippets = false, diff --git a/lua/lualine/components/python.lua b/lua/lualine/components/python.lua index 5b40b5c..d3ae735 100644 --- a/lua/lualine/components/python.lua +++ b/lua/lualine/components/python.lua @@ -11,9 +11,16 @@ function M:init(options) end function M:update_status() - local venv = require("python.venv").current_venv() - if venv then - return venv.name + local venv_path = vim.fn.getenv("VIRTUAL_ENV") + local conda_env = vim.fn.getenv("CONDA_DEFAULT_ENV") + + if venv_path ~= vim.NIL then + if vim.fs.basename(venv_path) == ".venv" then + return vim.fs.basename(vim.fs.dirname(venv_path)) + end + return vim.fs.basename(venv_path) + elseif conda_env ~= nil then + return conda_env else return "no venv" end diff --git a/lua/python/config.lua b/lua/python/config.lua index 24a2020..ffd7c68 100644 --- a/lua/python/config.lua +++ b/lua/python/config.lua @@ -28,7 +28,7 @@ PythonConfig.defaults = { auto_venv_lsp_attach_patterns = { "*.py" }, -- Filetypes to activate commands for python.nvim - command_setup_filetypes = { "python" }, + command_setup_buf_pattern = { "*.py" }, -- Load python.nvim python snippets python_lua_snippets = false, diff --git a/lua/python/hatch/commands.lua b/lua/python/hatch/commands.lua index e8fd61b..d8a2475 100644 --- a/lua/python/hatch/commands.lua +++ b/lua/python/hatch/commands.lua @@ -128,7 +128,6 @@ end function PythonHatchCommands.hatch_list_python() local versions = hatch_installed_versions() - vim.print(versions) end function PythonHatchCommands.hatch_delete_python() diff --git a/lua/python/init.lua b/lua/python/init.lua index e6c38bb..fd040b8 100644 --- a/lua/python/init.lua +++ b/lua/python/init.lua @@ -43,8 +43,8 @@ function Python.setup(opts) }) -- Load up commands for users - vim.api.nvim_create_autocmd({ "FileType" }, { - pattern = config.command_setup_filetypes, + vim.api.nvim_create_autocmd({ "BufEnter" }, { + pattern = config.command_setup_buf_pattern, desc = "python.nvim: Loading commands for python", group = id, callback = function() diff --git a/lua/python/venv/create.lua b/lua/python/venv/create.lua index 66e1304..e5d6799 100644 --- a/lua/python/venv/create.lua +++ b/lua/python/venv/create.lua @@ -71,7 +71,7 @@ function PythonVENVCreate.python_set_venv(venv_path, venv_name, venv_source) if current_venv then current_venv_name = current_venv.name end - if vim.fs.basename(venv_path) ~= current_venv_name then + if vim.fs.basename(venv_path) and current_venv_name ~= venv_name then if not venv_source then venv_source = "venv" end diff --git a/lua/python/venv/detect.lua b/lua/python/venv/detect.lua index 8112365..dabe160 100644 --- a/lua/python/venv/detect.lua +++ b/lua/python/venv/detect.lua @@ -40,7 +40,7 @@ function DetectVEnv:found_in_cwd() -- set venv if cwd is found in state before doing searches. if python_state.venvs[cwd] ~= nil and vim.fn.isdirectory(python_state.venvs[cwd].venv_path) ~= 0 then - local venv_name = vim.fs.basename(python_state.venvs[cwd].venv_path) + local venv_name = vim.fs.basename(vim.fs.dirname(python_state.venvs[cwd].venv_path)) if not venv_name then return false end diff --git a/lua/python/venv/interpreters.lua b/lua/python/venv/interpreters.lua index bd89b18..30311ce 100644 --- a/lua/python/venv/interpreters.lua +++ b/lua/python/venv/interpreters.lua @@ -39,7 +39,6 @@ function PythonVENVInterpreters.python_interpreters() if IS_WINDOWS then return { "python3" } end - -- TODO for macos we probably need to look in other places other than homebrew local pythons = vim.fn.globpath("/usr/bin/", "python3.*", false, true) if IS_MACOS then diff --git a/scripts/minimal_init.lua b/scripts/minimal_init.lua index 9f7de55..f181bc3 100644 --- a/scripts/minimal_init.lua +++ b/scripts/minimal_init.lua @@ -21,7 +21,7 @@ require("nvim-treesitter.locals") require("nvim-treesitter").setup() require("mini.test").setup() require("mini.doc").setup() -local ts_configs = require("nvim-treesitter.configs").setup({ +require("nvim-treesitter.configs").setup({ modules = { "highlight", }, diff --git a/tests/test_text_actions.lua b/tests/test_text_actions.lua index ae125ad..eb4604e 100644 --- a/tests/test_text_actions.lua +++ b/tests/test_text_actions.lua @@ -26,6 +26,7 @@ local get_lines = function() end T["text_actions"] = MiniTest.new_set({ + n_retry = 3, hooks = { pre_case = function() child.cmd("e _not_existing_new_buffer.py")