Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Poetry support #35

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open

Conversation

palandovalex
Copy link

No description provided.

@@ -139,6 +139,24 @@ local get_micromamba_base_path = function()
end
end

local get_poetry_venvs_base_path = function()
local which_poetry = io.popen('which poetry')
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm starting to get a bit worried about performance when all these functions are called and some call out to external executables. I don't use it but some users use the auto_venv which calls this on FileType. I wonder if this should be done async. This is somewhat independent from this PR but something I thought of now.

I also wonder, the current implementation supports the venvs_path setting. Would it be an option to support that being a function and instead supply this as a optional utility function which a user can use instead of the default venvs_path?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whell, i will replace sys calls this with parametrs in plugin config.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I realized that poetry support does not require any code mutation. I can just use venvs_path.
But I'm still thinking. which is useful to replace venvs_path with venvs_paths, with one, two or many directories.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also i can set use "has_conda" 'has_pixi', 'has_micromama' param. If it set to false - functions will not be called.
Like this:

  if settings.spec_envs == nil then
    return venvs
  end
  if 'conda' in setting.spec_envs then
    vim.list_extend(venvs, get_venvs_for(get_conda_base_path(), 'conda'))
    vim.list_extend(venvs, get_conda_base_env())
  end

This will speed up the plugin a bit, but... at the cost of backward compatibility.
It may be worth making a separate branch for such a change. For example, 'nightly_speedup'.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the meantime, I agree - everything except the usual venus should be handled asynchronously.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But I'm still thinking. which is useful to replace venvs_path with venvs_paths, with one, two or many directories.

Yeah I think that would be useful, maybe just support the setting to be string, table or function returning string or table? This could then be backwards compatible.

Also i can set use "has_conda" 'has_pixi', 'has_micromama' param. If it set to false - functions will not be called.
Like this:

I think that's a good idea, maybe it can default to true for now to be backwards compatible and then at least it can be disabled if speed is an issue?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I opened #36 for the async part

@aemonge
Copy link
Contributor

aemonge commented May 29, 2024

Wouldn't it be easier to change https://github.com/AckslD/swenv.nvim/blob/main/lua/swenv/api.lua#L200 :
](https://github.com/AckslD/swenv.nvim/blob/main/lua/swenv/api.lua#L188 :

from:

M.auto_venv = function()
  -- the function tries to activate in-project venvs, if present. Otherwise it tries to activate a venv in venvs folder
  -- which best matches the project name.
  local loaded, project_nvim = pcall(require, 'project_nvim.project')
  local venvs = settings.get_venvs(settings.venvs_path)
  if not loaded then
    print('Error: failed to load the project_nvim.project module')
    return
  end

  local project_dir, _ = project_nvim.get_project_root()
  if project_dir then -- project_nvim.get_project_root might not always return a project path
    local venv_name = read_venv_name_in_project(project_dir)
    if venv_name then
      local venv = { path = get_local_venv_path(project_dir), name = venv_name }
      set_venv(venv)
      return
    end
    venv_name = read_venv_name_common_dir(project_dir)
    if venv_name then
      local venv = best_match(venvs, venv_name)
      if venv then
        set_venv(venv)
        return
      end
    end
  end
end

to:

M.auto_venv = function()
  -- Check if the $VIRTUAL_ENV environment variable exists
  local virtual_env = os.getenv("VIRTUAL_ENV")
  if virtual_env then
    -- Extract the last part of the $VIRTUAL_ENV path
    local venv_name = virtual_env:match("([^/]+)$")
    if venv_name then
      local venv = { path = virtual_env, name = venv_name }
      set_venv(venv)
      return
    end
  end

  -- The function tries to activate in-project venvs, if present. Otherwise it tries to activate a venv in venvs folder
  -- which best matches the project name.
  local loaded, project_nvim = pcall(require, 'project_nvim.project')
  local venvs = settings.get_venvs(settings.venvs_path)
  if not loaded then
    print('Error: failed to load the project_nvim.project module')
    return
  end

  local project_dir, _ = project_nvim.get_project_root()
  if project_dir then -- project_nvim.get_project_root might not always return a project path
    local venv_name = read_venv_name_in_project(project_dir)
    if venv_name then
      local venv = { path = get_local_venv_path(project_dir), name = venv_name }
      set_venv(venv)
      return
    end
    venv_name = read_venv_name_common_dir(project_dir)
    if venv_name then
      local venv = best_match(venvs, venv_name)
      if venv then
        set_venv(venv)
        return
      end
    end
  end
end

@aemonge
Copy link
Contributor

aemonge commented May 29, 2024

Basically using $VIRTUAL_ENV

  local virtual_env = os.getenv("VIRTUAL_ENV")
  if virtual_env then
    -- Extract the last part of the $VIRTUAL_ENV path
    local venv_name = virtual_env:match("([^/]+)$")
    if venv_name then
      local venv = { path = virtual_env, name = venv_name }
      set_venv(venv)
      return
    end
  end

@AckslD
Copy link
Owner

AckslD commented May 29, 2024

@aemonge hmm, but wouldn't that only work if you already had the venv activated? I don't actually use the auto_venv feature myself but if I understand it correctly it's intention is to activate a venv automatically. So that env var wouldn't exist?

@aemonge
Copy link
Contributor

aemonge commented May 31, 2024

You're right !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants