HallerPatrick / py_lsp.nvim

Lsp Plugin for working with Python virtual environments
100 stars 12 forks source link

Add conda integration #18

Closed cnrrobertson closed 1 year ago

cnrrobertson commented 1 year ago

I noticed this is already on your todos, but it would be a really nice feature to see. It seems like you could integrate it pretty easily by searching the envs path at the conda installation location (usually something like ~/conda). I was able to follow the example here to create my own little change Python interpreter code:

function change_python_interpreter(path)
    vim.lsp.stop_client(vim.lsp.get_active_clients())
    configs.pyright.settings.python.pythonPath = path
    require'lspconfig'.pyright.setup(configs.pyright)
    vim.cmd('e%')
end

function get_python_interpreters(a, l, p)
    local paths = {}
    local is_home_dir = function()
        return vim.fn.getcwd(0) == vim.fn.expand("$HOME")
    end
    local commands = {'fd --glob -tl python $HOME/mambaforge', 'which -a python', is_home_dir() and '' or 'find . -name python'}
    for _, cmd in ipairs(commands) do
        local _paths = vim.fn.systemlist(cmd)
        if _paths then
            for _, path in ipairs(_paths) do
                table.insert(paths, path)
            end
        end
    end
    table.sort(paths)
    local res = {}
    for i, path in ipairs(paths) do
        if path ~= paths[i+1] then table.insert(res, path) end
    end
    return res
end

vim.api.nvim_exec([[
command! -nargs=1 -complete=customlist,PythonInterpreterComplete PythonInterpreter lua change_python_interpreter(<q-args>)

function! PythonInterpreterComplete(A,L,P) abort
  return v:lua.get_python_interpreters()
endfunction
]], false)

This provides a PythonInterpreter vim command which offers completion options of all conda environments and works well for me. However, I like what you are doing with this plugin much better.

HallerPatrick commented 1 year ago

Hey @cnrrobertson!

Glad you like my plugin! By looking at the snippet you provide, you are looking at every python binary found in $HOME, right? This would give me a list of at least 20 different python venvs to choose from. py_lsp tries to find venvs based on a strategy like poetry or venv. So we should add a new condastrategy to it.

So what we would need is a command like conda venvs list, that lists all available venvs that are created through conda. Do you know if there exists such a option with conda? Otherwise I could take a look at it?

Here you can find all existing strategies :)

Greetings Patrick

cnrrobertson commented 1 year ago

Yeah, so the local commands = {'fd --glob -tl python $HOME/mambaforge', 'which -a python', is_home_dir() and '' or 'find . -name python'} is the main search command.

It finds python binaries in my conda directory ($HOME/mambaforge), in whatever is currently activated (which -a python could find a venv, conda environment, or just the system binary), and then if we are NOT in $HOME it looks recursively in the current directory to find any environments. Not particularly efficient, I like your implementation better I think.

Conda does have a command conda env list to see all available venvs. The key difference is that all the environments are stored in a $HOME directory rather than in the project directory. It actually makes them easy to find. Common home directory names are ~/anaconda, ~/condaforge, ~/miniconda3, ~/mambaforge. But they could be named anything, so it might require an additional configuration option to get the directory..

I could write up a strategy for finding conda environments with a new source strategy conda that looks for common names in the home directory and submit a PRif that's helpful? I could also add a new configuration option to explicitly give the conda directory name if you're up for that.

HallerPatrick commented 1 year ago

Ah, haven't noticed the mambaforge directory.

Feel free to write a PR, if you are having any questions just write me!

Thanks in advance! 🚀