linux-cultist / venv-selector.nvim

Allows selection of python virtual environment from within neovim
MIT License
388 stars 41 forks source link

Automate with cwd #19

Closed GordianDziwis closed 1 year ago

GordianDziwis commented 1 year ago

When the current working dir changes, would be a good time to retirve the venv from cache.

linux-cultist commented 1 year ago

You mean you would want the plugin to check for a cached venv every time you change your cwd?

There would be a lot of unnecessary checks I think, since people use vim for a lot more than just python projects. But we could maybe add it as an option you can set.

It's also possible to write your own autocommand to execute VenvSelectCached on every cwd change, as a solution for now.

igorlfs commented 1 year ago

I'm having a similar issue. My workflow consists of

  1. Launching neovim from home directory
  2. Restoring a session in another directory with persisted
  3. Using a hook to activate the venv for the current project, i.e.,
    au({ "User" }, {
    desc = "Venv autoselect",
    pattern = { "PersistedLoadPost", "PersistedTelescopeLoadPost" },
    group = ag("PersistedHooks", {}),
    callback = function()
        local venv = vim.fn.findfile("pyproject.toml", vim.fn.getcwd() .. ";")
        if venv ~= "" then
            require("venv-selector").retrieve_from_cache()
        end
    end,
    })

The problem is that venv-select isn't aware of cwd changes, so it always thinks I'm in the home directory.

You mean you would want the plugin to check for a cached venv every time you change your cwd?

I think we could make venv-select session aware by having an option to control how often that happens. Something along the lines of

update_with_cwd = "never"    -- current behavior
               or "once"     -- update only once to update when loading a session, for instance
               or "always"   -- always updates cached venv (mention that it can be performance intensive?)
igorlfs commented 1 year ago

The latest update fixes my use case, thanks linux-cultist! By the way, nice nickname!

linux-cultist commented 1 year ago

Perfect! Very happy to hear that and thanks for liking the nickname... It was just something that came to me as fitting my open source personality.. :)

@BonaBeavis: Did the latest update fix this for you as well? The plugin now uses the latest cwd when you run VenvSelectCached and it was always intended to, but a bug slipped in there.

linux-cultist commented 1 year ago

I'm closing this but feel free to reopen if it's still an issue.

igorlfs commented 1 year ago

Hey, actually, I'm having an issue that I need to restart the LSP to trigger it "hooking" to the selected venv. It is otherwise active, but due to some race condition, I need to use :LspRestart to make Pyright recognize the venv. It looks like when I open a session, and it loads some files, the LSP immediately attaches to these files (or at least does so at the same time the venv is being activated). Could require("venv-selector").retrieve_from_cache() check if LSP is active and if so, trigger a restart?

GuillaumeOj commented 5 months ago

Hey, actually, I'm having an issue that I need to restart the LSP to trigger it "hooking" to the selected venv. It is otherwise active, but due to some race condition, I need to use :LspRestart to make Pyright recognize the venv. It looks like when I open a session, and it loads some files, the LSP immediately attaches to these files (or at least does so at the same time the venv is being activated). Could require("venv-selector").retrieve_from_cache() check if LSP is active and if so, trigger a restart?

EDIT: Actually this is a very bad idea. It has some side effects that I have not digged.

~~Hey @igorlfs, I don't know if it's the proper way to do it, but I have found a workaround for this. According to the doc, we create an autocmd like this:~~

vim.api.nvim_create_autocmd('VimEnter', {
  desc = 'Auto select virtualenv Nvim open',
  pattern = '*',
  callback = function()
    local venv = vim.fn.findfile('pyproject.toml', vim.fn.getcwd() .. ';')
    if venv ~= '' then
      require('venv-selector').retrieve_from_cache()
    end
  end,
  once = true,
})

So I have simply added:

vim.cmd([[silent! LspRestart]])

here:

    if venv ~= '' then
      require('venv-selector').retrieve_from_cache()
      vim.cmd([[silent! LspRestart]])
    end