GitHunt
DI

diegofigs/npackages.nvim

neovim plugin for managing node's package.json

npackages.nvim

Report Bug / Request Feature Β· Ask Question

Supercharge your Node experience in Neovim!
A heavily modified fork of crates.nvim

Neovim
Lua
npm

MIT License
Issues
CI Status
Lint Status
LuaRocks

πŸ“ Prerequisites

Required

Optional

πŸ“₯ Installation

vim-plug

Plug 'diegofigs/npackages.nvim'

packer.nvim

use { 'diegofigs/npackages.nvim', requires = { "nvim-neotest/nvim-nio" } }

lazy.nvim

{
  'diegofigs/npackages.nvim',
  dependencies = { "nvim-neotest/nvim-nio" }
  lazy = false, -- This plugin is already lazy
}

⚑ Quick Setup

This plugin automatically configures an LSP client
that will provide diagnostics for your package.json dependencies.
See the Usage section for more info.

This is a file type plugin that works out of the box,
so there is no need to call a setup function or configure anything
to get this plugin working.

You will most likely want to add some keymaps.
Most keymaps are only useful in package.json files,
so I suggest you define them in vim.g.npackages.on_attach

Example:

vim.g.npackages = {
  on_attach = function(bufnr)
    vim.api.nvim_buf_set_keymap(bufnr, "n", "<leader>nt", "<cmd>Npackages toggle<cr>", {
      desc = "Toggle Package Versions",
    })
    vim.api.nvim_buf_set_keymap(bufnr, "n", "<leader>na", "<cmd>Npackages add<cr>", {
      desc = "Add Package",
    })
    vim.api.nvim_buf_set_keymap(bufnr, "n", "<leader>nd", "<cmd>Npackages delete<cr>", {
      desc = "Delete Package",
    })
    vim.api.nvim_buf_set_keymap(bufnr, "n", "<leader>nu", "<cmd>Npackages update<cr>", {
      desc = "Update Package",
    })
    vim.api.nvim_buf_set_keymap(bufnr, "n", "<leader>nc", "<cmd>Npackages change_version<cr>", {
      desc = "Change Version",
    })
    vim.api.nvim_buf_set_keymap(bufnr, "n", "<leader>ni", "<cmd>Npackages install<cr>", {
      desc = "Install Package Dependencies",
    })
    vim.api.nvim_buf_set_keymap(bufnr, "n", "<leader>nr", "<cmd>Npackages refresh<cr>", {
      desc = "Refresh Packages",
    })
    vim.api.nvim_buf_set_keymap(bufnr, "n", "<leader>nR", "<cmd>Npackages reload<cr>", {
      desc = "Reload Packages",
    })
  end,
}

πŸ“š Usage

npackages.nvim supercharges your package.json experience in Neovim by providing a rich set of LSP features. These features help you manage your dependencies with ease, offering functionality like diagnostics, completion, code actions, code lens, document symbols, semantic tokens, and go-to-definition. Here's a breakdown of the main features:

Diagnostics

npackages.nvim provides real-time diagnostics for your package.json files. It identifies issues such as missing or incorrect dependency versions, helping you ensure your dependencies are always up-to-date and correctly specified.

How it works: Diagnostics are triggered whenever you open, change, or save a package.json file. Issues like missing dependencies or mismatched versions will be highlighted directly in the editor.
Completion (Package Names and Versions)
npackages.nvim offers intelligent completion suggestions for package names and versions as you edit your package.json file.

  • Package Name Completion: As you start typing a package name, the LSP client will suggest possible packages from npm, helping you quickly find and add the correct dependencies.

  • Version Completion: When specifying a version for a dependency, the plugin suggests the latest versions available, ensuring that you can easily select the correct one.

  • How it works: Completion suggestions are triggered as you type, providing inline options for package names and versions based on npm registry data.

Code Actions

npackages.nvim offers useful code actions to help you fix issues in your package.json files and maintain clean, well-formatted code.

  • Diagnostics Fixes: Automatically fix issues identified by diagnostics, such as updating a dependency to the latest version or correcting a version format.

  • JSON Formatting: Quickly format your package.json for consistency and readability.

  • How it works: Code actions can be triggered via the LSP command palette or through key mappings, providing quick fixes and formatting options.

Code Lens (Run Scripts)

npackages.nvim adds code lenses above each script defined in your package.json file, allowing you to run scripts directly from the editor.

  • How it works: Code lenses appear as actionable text above each script definition. Simply click on the lens to run the associated script using your preferred package manager (npm, yarn, pnpm).
    Document Symbols and Semantic Tokens
    npackages.nvim enhances your editing experience by providing document symbols and semantic tokens, making navigation and understanding your package.json structure easier.

  • Document Symbols: These allow you to quickly navigate through different sections of your package.json, such as dependencies, devDependencies, and scripts.

  • Semantic Tokens: Provides syntax highlighting for different elements in package.json, differentiating between keys, values, and script names for better readability.

  • How it works: Document symbols and semantic tokens are automatically enabled when you open a package.json file, with symbols accessible via your LSP client’s symbol navigation.

Go to Definition

npackages.nvim enables quick navigation to a dependency’s package.json file within your node_modules directory.

  • How it works: Place your cursor on a dependency name in your package.json, and trigger the "Go to Definition" command to jump directly to the package.json of that package in node_modules.

Commands

Toggle
  • toggle toggles diagnostics on/off
:Npackages toggle
vim.cmd.Npackages('toggle')
Refresh
  • refresh diagnostics by fetching package.json dependencies whose cache time has expired
:Npackages refresh
vim.cmd.Npackages('refresh')
Reload
  • reload refreshes diagnostics and force fetches package.json dependencies
:Npackages reload
vim.cmd.Npackages('reload')
Install
  • install runs npm|yarn|pnpm install
:Npackages install
vim.cmd.Npackages('install')
Add
  • add prompts user for dependency type, package name, version
    and runs npm|yarn|pnpm add [-D] <package>@<version>
:Npackages add
vim.cmd.Npackages('add')
Update
  • update runs for package under cursor npm|yarn|pnpm install <package>@latest
:Npackages update
vim.cmd.Npackages('update')
Delete
  • delete runs for package under cursor npm|yarn|pnpm remove <package>
:Npackages delete
vim.cmd.Npackages('delete')
Change Version
  • change_version prompts user for new version for package under cursor
    and runs npm|yarn|pnpm install <package>@<version>
:Npackages change_version
vim.cmd.Npackages('change_version')