Skip to content

Commit b90189d

Browse files
committed
add ignore_envs_groups parametr
1 parent c11eeaa commit b90189d

5 files changed

Lines changed: 55 additions & 15 deletions

File tree

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,26 @@ require('swenv.api').pick_venv()
2727
to pick an environment. Uses `vim.ui.select` so a tip is to use eg
2828
[dressing.nvim](https://github.com/stevearc/dressing.nvim).
2929

30+
### Supported envs
31+
32+
Swenv supports various env types:
33+
Conda, pixi, micromamba, pyenv... For some envs types, such as poetry, you can specify env path:
34+
35+
```lua
36+
require('swenv').setup({
37+
venvs_path = vim.fn.expand('~/.cache/pypoetry/virtualenvs'),
38+
})
39+
```
40+
41+
If you havent some envs group, you can disable they, to save startup time:
42+
43+
```lua
44+
require('swenv').setup({
45+
ignore_envs_groups = {'conda', 'pixi', 'micromamba', 'pyenv'}
46+
})
47+
```
48+
49+
3050
### Get Environment
3151

3252
To show the current venv in for example a status-line you can call

lua/swenv/api.lua

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,14 @@ M.get_current_venv = function()
8181
return current_venv
8282
end
8383

84+
8485
local get_venvs_for = function(base_path, source, opts)
8586
local venvs = {}
8687
if base_path == nil then
8788
return venvs
8889
end
8990
local paths = scan_dir(base_path, vim.tbl_extend('force', { depth = 1, only_dirs = true, silent = true }, opts or {}))
90-
for _, path in ipairs(paths) do
91+
for _, path in pairs(paths) do
9192
table.insert(venvs, {
9293
name = Path:new(path):make_relative(base_path),
9394
path = path,
@@ -117,7 +118,7 @@ local get_conda_base_path = function()
117118
end
118119
end
119120

120-
local get_conda_base_env = function ()
121+
local get_conda_base_env = function()
121122
local venvs = {}
122123
local path = os.getenv("CONDA_EXE")
123124
if path then
@@ -148,15 +149,33 @@ local get_pyenv_base_path = function()
148149
end
149150
end
150151

152+
local to_set = function(some_list)
153+
local set = {}
154+
for _, key in ipair(some_list) do
155+
set[key] = true
156+
end
157+
return set
158+
end
159+
151160
M.get_venvs = function(venvs_path)
152161
local venvs = {}
153162
vim.list_extend(venvs, get_venvs_for(venvs_path, 'venv'))
154-
vim.list_extend(venvs, get_venvs_for(get_pixi_base_path(), 'pixi'))
155-
vim.list_extend(venvs, get_venvs_for(get_conda_base_path(), 'conda'))
156-
vim.list_extend(venvs, get_conda_base_env())
157-
vim.list_extend(venvs, get_venvs_for(get_micromamba_base_path(), 'micromamba'))
158-
vim.list_extend(venvs, get_venvs_for(get_pyenv_base_path(), 'pyenv'))
159-
vim.list_extend(venvs, get_venvs_for(get_pyenv_base_path(), 'pyenv', { only_dirs = false }))
163+
164+
local ignore_envs = to_set(settings.ignore_envs_groups)
165+
if ignore_envs and not ignore_envs['conda'] then
166+
vim.list_extend(venvs, get_venvs_for(get_conda_base_path(), 'conda'))
167+
vim.list_extend(venvs, get_conda_base_env())
168+
end
169+
if ignore_envs and not ignore_envs['pixi'] then
170+
vim.list_extend(venvs, get_venvs_for(get_pixi_base_path(), 'pixi'))
171+
end
172+
if ignore_envs and not ignore_envs['micromamba'] then
173+
vim.list_extend(venvs, get_venvs_for(get_micromamba_base_path(), 'micromamba'))
174+
end
175+
if ignore_envs and not ignore_envs['pyenv'] then
176+
vim.list_extend(venvs, get_venvs_for(get_pyenv_base_path(), 'pyenv'))
177+
vim.list_extend(venvs, get_venvs_for(get_pyenv_base_path(), 'pyenv', { only_dirs = false }))
178+
end
160179
return venvs
161180
end
162181

lua/swenv/config.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ M.settings = {
1010
-- Path passed to `get_venvs`.
1111
venvs_path = vim.fn.expand('~/venvs'),
1212
-- Something to do after setting an environment
13+
ignore_envs_groups = {},
1314
post_set_venv = nil,
1415
}
1516

lua/swenv/match.lua

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ end
2121

2222
-- Function to find the best match based on Levenshtein distance
2323
M.best_match = function(items, query)
24-
local min_distance = math.huge -- Initialize minimum distance as infinity
25-
local match = nil -- Initialize match as nil to handle case if no match found
26-
for _, item in ipairs(items) do -- Iterate over items
24+
local min_distance = math.huge -- Initialize minimum distance as infinity
25+
local match = nil -- Initialize match as nil to handle case if no match found
26+
for _, item in ipairs(items) do -- Iterate over items
2727
local distance = lev(query, item.name) -- Compute Levenshtein distance to the query
28-
if distance < min_distance then -- If this item is closer to query...
29-
min_distance = distance -- Update minimum distance
30-
match = item -- Update the match
28+
if distance < min_distance then -- If this item is closer to query...
29+
min_distance = distance -- Update minimum distance
30+
match = item -- Update the match
3131
end
3232
end
3333
return match -- Return the best match

lua/swenv/project.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ M.read_venv_name = function(project_dir)
66
if not file then
77
return nil
88
end
9-
local content = file:read('*a') -- *a or *all reads the whole file
9+
local content = file:read('*a') -- *a or *all reads the whole file
1010
file:close()
1111
return content:match('^%s*(.-)%s*$') -- Trim whitespace
1212
end

0 commit comments

Comments
 (0)