Skip to content

Latest commit

 

History

History
110 lines (86 loc) · 3.56 KB

File metadata and controls

110 lines (86 loc) · 3.56 KB

py-requirements.nvim

Neovim plugin that helps manage python requirements.

demo.mp4

Features

  • Integrated with lsp and provides completions
  • Uses treesitter parser to read requirements.txt, more robust than ad-hoc parsing
  • Displays diagnostics in normal mode with warnings for not using latest version
  • Cache pypi responses within a session to improve performance
  • Auto upgrade dependencies when keymaps are configured
  • Display package description from PyPI in a floating window with syntax highlighting
  • Supports custom index-url and extra-index-url for finding packages

Dependencies

  • neovim >= 0.10.0
  • treesitter parser:
    • requirements: For requirements.txt and parsing versions in other languages
    • toml: For pyproject.toml
  • System dependencies:
    • curl: Used to call pypi API

Install

vim.pack

vim.pack.add({
    'https://github.com/nvim-treesitter/nvim-treesitter',
    'https://github.com/MeanderingProgrammer/py-requirements.nvim',
})
require('py-requirements').setup({})

lazy.nvim

{
    'MeanderingProgrammer/py-requirements.nvim',
    dependencies = { 'nvim-treesitter/nvim-treesitter' },
    config = function()
        require('py-requirements').setup({})
    end,
}

Setup

Configure

Below is the default configuration, any part of it can be modified.

require('py-requirements').setup({
    -- Endpoint used for getting package versions
    index_url = 'https://pypi.org/simple/',
    -- Fallback endpoint in case 'index_url' fails to find a package
    extra_index_url = nil,
    -- Specify which file patterns plugin is active on
    -- For info on patterns, see :h pattern
    file_patterns = { '.*requirements.*.txt', '.*pyproject.*.toml' },
    -- Options for how diagnostics are displayed
    diagnostic_opts = { padding = 5 },
    -- For available options, see :h vim.lsp.util.open_floating_preview
    float_opts = { border = 'rounded' },
    filter = {
        -- Pull only final release versions, this will ignore alpha, beta,
        -- release candidate, post release, and developmental release versions
        final_release = false,
        -- Ignore yanked package versions
        yanked = true,
    },
    -- Enabled by default if you want to disable lsp completions set to false
    enable_lsp = true,
})

Keymaps

local py = require('py-requirements')
py.setup({...})
vim.keymap.set('n', '<leader>ru', py.upgrade, {})
vim.keymap.set('n', '<leader>rU', py.upgrade_all, {})
vim.keymap.set('n', '<leader>rK', py.show_description, {})

Completions

in-process lsp

On by default and the recommended way of getting version completions from this plugin. Requires no additional configuration, assuming you have general LSP completions. Works with any completion engine out of the box!

Limitations

  • Does not read or otherwise interact with pip.conf file

Acknowledgments / Related Projects

  • crates.nvim: Many ideas were taken from this project and translated to work with Python dependencies rather than Rust crates. I also used the in-process lsp implementation as an awesome reference lsp.lua.
  • cmp-pypi: Found this one rather late, similar idea but built to work with pyproject.toml files