Skip to content

An additional source for nvim-cmp to autocomplete packages and its versions

License

Notifications You must be signed in to change notification settings

whengely/cmp-npm

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

38 Commits
 
 
 
 
 
 

Repository files navigation

cmp-npm

This is an additional source for nvim-cmp, it allows you to autocomplete npm packages and its versions. The source is only active if you're in a package.json file.

demo-cmp-npm

Requirements

It needs the Neovim plugin nvim-cmp and the npm command line tool.

Installation

For vim-plug:

Plug 'nvim-lua/plenary.nvim'
Plug 'David-Kunz/cmp-npm'

For packer:

use {
  'David-Kunz/cmp-npm',
  requires = {
    'nvim-lua/plenary.nvim'
  }
}

For lazy.nvim:

{
  "David-Kunz/cmp-npm",
  dependencies = { 'nvim-lua/plenary.nvim' },
  ft = "json",
  config = function()
    require('cmp-npm').setup({})
  end
}

Run the setup function and add the source

require('cmp-npm').setup({})
cmp.setup({
  ...,
  sources = {
    { name = 'npm', keyword_length = 4 },
    ...
  }
})

(in Vimscript, make sure to add lua << EOF before and EOF after the lua code)

The setup function accepts an options table which defaults to:

{
  ignore = {},
  only_semantic_versions = false,
  only_latest_version = false
}
  • ignore (table): Allows you to filter out all versions which match one of its entries, e.g. ignore = { 'beta', 'rc' }.
  • only_semantic_versions (Boolean): If true, will filter out all versions which don't follow the major.minor.patch schema.
  • only_latest_version (Boolean): If true, will only show latest release version.

Highlighting & Icon

Npm's cmp source creates highlight group CmpItemKindNpm. To add an icon for lspkind, add the icon to your lspkind symbol map.

Option 1

-- lspkind.lua
local lspkind = require("lspkind")
lspkind.init({
  symbol_map = {
    Npm = "",
  },
})

vim.api.nvim_set_hl(0, "CmpItemKindNpm", {fg ="#BD93F9"})

Option 2

-- cmp.lua
cmp.setup {
  ...
  formatting = {
    format = lspkind.cmp_format({
      mode = "symbol",
      symbol_map = { Npm = "" }
    })
  }
  ...
}

Limitations

The versions are not correctly sorted (depends on nvim-cmp's sorting algorithm).

About

An additional source for nvim-cmp to autocomplete packages and its versions

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Lua 100.0%