Giter VIP home page Giter VIP logo

whichpy.nvim's Introduction

whichpy.nvim

Yet another python interpreter selector plugin for neovim. Make LSPs (pyright, pylsp, basedpyright) and dap-python work with specific python.

whichpydemo.mov

Features

  • Only nvim-lspconfig required. dap-python is optional.
  • Use vim.ui.select. Enable dressing.nvim to get powerful UI.
  • Support Pylsp, Pyright, BasedPyright LSP servers by default. Other LSP server can be supported with simple config.
  • Switch between python interpreters without restart LSPs. (Except WhichPy restore on Pyright)
  • Search on common directories, currently support:
    • workspace (relative path of vim.fn.getcwd())
    • global (vim.env.Path and common posix paths)
    • global virtual environment
    • pyenv
    • poetry
    • pdm
    • conda

Installation

  • Using lazy.nvim:
return {
  "neolooong/whichpy.nvim",
  opts = {},
}

Configuration

whichpy.nvim comes with these defaults:
{
  cache_dir = vim.fn.stdpath("cache") .. "/whichpy.nvim",
  locator = {
    -- you can disable locator like this
    -- locator = { enable = false },
    workspace = {
      search_pattern = ".*env.*", -- `:help lua-patterns`
      depth = 2,
    },
    global = {},
    global_virtual_environment = {
      dirs = {
        -- accept following structure
        -- path
        -- { path, vim.uv.os_uname().sysname }
        "~/envs",
        "~/.direnv",
        "~/.venvs",
        "~/.virtualenvs",
        "~/.local/share/virtualenvs",
        { "~/Envs", "Linux" },  -- only search on linux
        vim.env.WORKON_HOME,
      }
    },
    pyenv = {},
    poetry = {},
    pdm = {},
    conda = {},
  },
  lsp = {
    -- lsp_name = { path_getter() , path_setter() }
    pylsp = {
      require("whichpy.lsp").pylsp.python_path_getter,
      require("whichpy.lsp").pylsp.python_path_setter,
    },
    pyright = {
      require("whichpy.lsp").pyright.python_path_getter,
      require("whichpy.lsp").pyright.python_path_setter,
    },
    basedpyright = {
      require("whichpy.lsp").pyright.python_path_getter,
      require("whichpy.lsp").pyright.python_path_setter,
    },
  },
}

Commands

This plugin provide these commands:

  • :WhichPy select [path?]

    If path provided, LSPs and dap-python would be configured. Otherwise, selector prompt (through vim.ui.select) would show up.

  • :WhichPy restore

    Restore LSPs and dap-python configuration, and clear the cache.

  • :WhichPy retrieve

    Retrieve the interpreter path from cache, then configure lsp and dap-python.

  • :WhichPy rescan

    Search python interpreter again.

Common questions

How this plugin activate environment?

This plugin DOES NOT activate environment (source env/bin/activate or conda activate). The purpose of the plugin is to make LSPs and dap-python work with the specified python.

When path selected, this plugin do these things:

  1. Save the environment variables: VIRTUAL_ENV and CONDA_PREFIX.
  2. Unset VIRTUAL_ENV and CONDA_PREFIX then set the resolve_python() of dap-python.
  3. Iterate lsp clients, save the python path that current used (if any), before update the configuration.
How to activate environment automatically?
  • Activate environment before open neovim.

  • Set the python path when lsp initalize.

    -- pyright
    require("lspconfig").pyright.setup({
      on_init = function(client)
        -- 
        client.settings.python.pythonPath = require("whichpy.lsp").find_python_path(client.config.root_dir)
      end
    })
    
    -- pylsp
    require("lspconfig").pylsp.setup({
      on_init = function(client)
        client.settings = vim.tbl_deep_extend("force", client.settings, {
          pylsp = {
            plugins = {
              jedi = {
                environment = require("whichpy.lsp").find_python_path(client.config.root_dir)
              }
            }
          }
        })
      end
    })

whichpy.nvim's People

Contributors

neolooong avatar

Stargazers

Yuta Katayama avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.