npackages.nvim
Report Bug / Request Feature Β· Ask Question
Supercharge your Node experience in Neovim!
A heavily modified fork of crates.nvim
π Quick Links
π Prerequisites
Required
neovim >= 0.9curl- nvim-nio
Optional
vim.uiimplementation such as dressing.nvim$/progressdisplay such as noice.nvim
π₯ Installation
Plug 'diegofigs/npackages.nvim'use { 'diegofigs/npackages.nvim', requires = { "nvim-neotest/nvim-nio" } }{
'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
toggletoggles diagnostics on/off
:Npackages togglevim.cmd.Npackages('toggle')Refresh
refreshdiagnostics by fetchingpackage.jsondependencies whose cache time has expired
:Npackages refreshvim.cmd.Npackages('refresh')Reload
reloadrefreshes diagnostics and force fetchespackage.jsondependencies
:Npackages reloadvim.cmd.Npackages('reload')Install
installrunsnpm|yarn|pnpm install
:Npackages installvim.cmd.Npackages('install')Add
addprompts user for dependency type, package name, version
and runsnpm|yarn|pnpm add [-D] <package>@<version>
:Npackages addvim.cmd.Npackages('add')Update
updateruns for package under cursornpm|yarn|pnpm install <package>@latest
:Npackages updatevim.cmd.Npackages('update')Delete
deleteruns for package under cursornpm|yarn|pnpm remove <package>
:Npackages deletevim.cmd.Npackages('delete')Change Version
change_versionprompts user for new version for package under cursor
and runsnpm|yarn|pnpm install <package>@<version>
:Npackages change_versionvim.cmd.Npackages('change_version')Related projects
- Saecki/crates.nvim: base plugin structure
and lsp functionality - mrcjkb/rustaceanvim: huge inspiration
for readme, plugin structure and testing methodology - vuki656/package-info.nvim:
similar solution, provides commands but no diagnostics