Skip to content

Commit 42117fd

Browse files
authored
fix: python.nvim detect project changes for venv adjustments automatically (#24)
1 parent ff0656d commit 42117fd

File tree

10 files changed

+18
-12
lines changed

10 files changed

+18
-12
lines changed

doc/python.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ Default values:
7878
auto_venv_lsp_attach_patterns = { "*.py" },
7979

8080
-- Filetypes to activate commands for python.nvim
81-
command_setup_filetypes = { "python" },
81+
command_setup_buf_pattern = { "*.py" },
8282

8383
-- Load python.nvim python snippets
8484
python_lua_snippets = false,

lua/lualine/components/python.lua

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,16 @@ function M:init(options)
1111
end
1212

1313
function M:update_status()
14-
local venv = require("python.venv").current_venv()
15-
if venv then
16-
return venv.name
14+
local venv_path = vim.fn.getenv("VIRTUAL_ENV")
15+
local conda_env = vim.fn.getenv("CONDA_DEFAULT_ENV")
16+
17+
if venv_path ~= vim.NIL then
18+
if vim.fs.basename(venv_path) == ".venv" then
19+
return vim.fs.basename(vim.fs.dirname(venv_path))
20+
end
21+
return vim.fs.basename(venv_path)
22+
elseif conda_env ~= nil then
23+
return conda_env
1724
else
1825
return "no venv"
1926
end

lua/python/config.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ PythonConfig.defaults = {
2828
auto_venv_lsp_attach_patterns = { "*.py" },
2929

3030
-- Filetypes to activate commands for python.nvim
31-
command_setup_filetypes = { "python" },
31+
command_setup_buf_pattern = { "*.py" },
3232

3333
-- Load python.nvim python snippets
3434
python_lua_snippets = false,

lua/python/hatch/commands.lua

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,6 @@ end
128128

129129
function PythonHatchCommands.hatch_list_python()
130130
local versions = hatch_installed_versions()
131-
vim.print(versions)
132131
end
133132

134133
function PythonHatchCommands.hatch_delete_python()

lua/python/init.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ function Python.setup(opts)
4343
})
4444

4545
-- Load up commands for users
46-
vim.api.nvim_create_autocmd({ "FileType" }, {
47-
pattern = config.command_setup_filetypes,
46+
vim.api.nvim_create_autocmd({ "BufEnter" }, {
47+
pattern = config.command_setup_buf_pattern,
4848
desc = "python.nvim: Loading commands for python",
4949
group = id,
5050
callback = function()

lua/python/venv/create.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ function PythonVENVCreate.python_set_venv(venv_path, venv_name, venv_source)
7171
if current_venv then
7272
current_venv_name = current_venv.name
7373
end
74-
if vim.fs.basename(venv_path) ~= current_venv_name then
74+
if vim.fs.basename(venv_path) and current_venv_name ~= venv_name then
7575
if not venv_source then
7676
venv_source = "venv"
7777
end

lua/python/venv/detect.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ function DetectVEnv:found_in_cwd()
4040

4141
-- set venv if cwd is found in state before doing searches.
4242
if python_state.venvs[cwd] ~= nil and vim.fn.isdirectory(python_state.venvs[cwd].venv_path) ~= 0 then
43-
local venv_name = vim.fs.basename(python_state.venvs[cwd].venv_path)
43+
local venv_name = vim.fs.basename(vim.fs.dirname(python_state.venvs[cwd].venv_path))
4444
if not venv_name then
4545
return false
4646
end

lua/python/venv/interpreters.lua

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ function PythonVENVInterpreters.python_interpreters()
3939
if IS_WINDOWS then
4040
return { "python3" }
4141
end
42-
-- TODO for macos we probably need to look in other places other than homebrew
4342
local pythons = vim.fn.globpath("/usr/bin/", "python3.*", false, true)
4443

4544
if IS_MACOS then

scripts/minimal_init.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ require("nvim-treesitter.locals")
2121
require("nvim-treesitter").setup()
2222
require("mini.test").setup()
2323
require("mini.doc").setup()
24-
local ts_configs = require("nvim-treesitter.configs").setup({
24+
require("nvim-treesitter.configs").setup({
2525
modules = {
2626
"highlight",
2727
},

tests/test_text_actions.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ local get_lines = function()
2626
end
2727

2828
T["text_actions"] = MiniTest.new_set({
29+
n_retry = 3,
2930
hooks = {
3031
pre_case = function()
3132
child.cmd("e _not_existing_new_buffer.py")

0 commit comments

Comments
 (0)