hrsh7th / nvim-cmp

A completion plugin for neovim coded in Lua.
MIT License
7.9k stars 394 forks source link

Completion (select next item) key triggers error every press in Powershell terminal on Windows 10 #1722

Closed Rimann91 closed 12 months ago

Rimann91 commented 1 year ago

FAQ

Announcement

Minimal reproducible full config

-- Setup nvim-cmp.
local cmp = require 'cmp'

local kind_icons = {
  Text = "",
  Method = "m",
  Function = "",
  Constructor = "",
  Field = "",
  Variable = "",
  Class = "",
  Interface = "",
  Module = "",
  Property = "",
  Unit = "",
  Value = "",
  Enum = "",
  Keyword = "",
  Snippet = "",
  Color = "",
  File = "",
  Reference = "",
  Folder = "",
  EnumMember = "",
  Constant = "",
  Struct = "",
  Event = "",
  Operator = "",
  TypeParameter = "",
}

cmp.setup({
  snippet = {
    -- requireD - you must specify a snippet engine
    expand = function(args)
      --vim.fn["vsnip#anonymous"](args.body) -- For `vsnip` users.
      -- require('luasnip').lsp_expand(args.body) -- For `luasnip` users.
      -- require('snippy').expand_snippet(args.body) -- For `snippy` users.
      vim.fn["UltiSnips#Anon"](args.body) -- For `ultisnips` users.
    end,
  },
  view = 'custom',
  window = {
    completion = cmp.config.window.bordered(),
  },
  mapping = {
    ['<C-b>'] = cmp.mapping(cmp.mapping.scroll_docs(-4), { 'i', 'c' }),
    ['<C-f>'] = cmp.mapping(cmp.mapping.scroll_docs(4), { 'i', 'c' }),
    ['<C-Space>'] = cmp.mapping(cmp.mapping.complete(), { 'i', 'c' }),
    ['<TAB>'] = cmp.mapping(cmp.mapping.select_next_item()),
    ['<S-TAB>'] = cmp.mapping(cmp.mapping.select_prev_item()),
    ['<C-y>'] = cmp.config.disable, -- Specify `cmp.config.disable` if you want to remove the default `<C-y>` mapping.
    ['<C-e>'] = cmp.mapping({
      i = cmp.mapping.abort(),
      c = cmp.mapping.close(),
    }),
    ['<CR>'] = cmp.mapping.confirm({ select = true }), -- Accept currently selected item. Set `select` to `false` to only confirm explicitly selected items.
  },
  formatting = {
    fields = { "kind", "abbr", "menu" },
    format = function(entry, vim_item)
      -- Kind icons
      vim_item.kind = string.format("%s", kind_icons[vim_item.kind])
      -- vim_item.kind = string.format('%s %s', kind_icons[vim_item.kind], vim_item.kind) -- This concatonates the icons with the name of the item kind
      vim_item.menu = ({
        nvim_lsp = "[LSP]",
        cmp_tabnine = "[T9]",
        nvim_lua = "[NVIM_LUA]",
        luasnip = "[Snippet]",
        buffer = "[Buffer]",
        path = "[Path]",
      })[entry.source.name]
      return vim_item
    end,
  },
  sources = cmp.config.sources({
    { name = 'nvim_lsp' },
    { name = 'cmp_tabnine' },
    { name = 'ultisnips' }, -- For ultisnips users.
    { name = 'buffer' },
    { name = 'path' },
    --{ name = 'vsnip' }, -- For vsnip users.
    -- { name = 'luasnip' }, -- For luasnip users.
    -- { name = 'snippy' }, -- For snippy users.
    -- { name = 'cmdline' },
    { name = 'orgmode' }
  }),
})

-- Use buffer source for `/` (if you enabled `native_menu`, this won't work anymore).
cmp.setup.cmdline('/', {
  mapping = cmp.mapping.preset.cmdline(),
  sources = {
    { name = 'buffer' }
  }
})

-- Use cmdline & path source for ':' (if you enabled `native_menu`, this won't work anymore).
cmp.setup.cmdline(':', {
  mapping = cmp.mapping.preset.cmdline(),
  sources = cmp.config.sources({
    { name = 'path' },
    { name = 'cmdline' }
  }, {
    {
      option = {
        ignore_cmds = { 'Dispatch' }
      }
    }
  })
})

cmp_capabilities = require('cmp_nvim_lsp').default_capabilities(vim.lsp.protocol.make_client_capabilities())

Description

I am using the exact configuration on windows 10 PowerShell terminal that I use in WSL Ubuntu. In Ubuntu there is no issue. Cmp provides the list of completions, but then when I press as I do in ubuntu I recieve the following error in messages

Error executing vim.schedule lua callback: ...\.config\nvim\autoload\plugged\nvim-cmp/lua/cmp/view.lua:270: attempt to index field 'docs' (a nil value)
stack traceback:
    ...\.config\nvim\autoload\plugged\nvim-cmp/lua/cmp/view.lua:270: in function 'callback'
    ...g\nvim\autoload\plugged\nvim-cmp/lua/cmp/utils/async.lua:138: in function 'fn'
    vim/_editor.lua:341: in function <vim/_editor.lua:340>

The error occurs in every buffer, including the command cmdline completions and completions for LSP

Steps to reproduce

  1. Open neovim
  2. enter cmdline mode or open a file with a running LSP server
  3. Begin typing valid input
  4. Once suggestions appear attempt to use bound key to cycle through them

Expected behavior

The suggestions to be cycled through

Actual behavior

neovim displays an error message before cycling to each next entry in the suggestions

Additional context

Neovim prerelease 0.10.0 (I upgraded to this to check if it resolved the issue, originally noticed the issue in 0.9.0).

all plugins are up to date.

Rimann91 commented 12 months ago

I resolved this issue, although I still do not know exactly why the problem happens in windows but not linux: In my config I have the 'view' option set to a string ('custom').

Clearly, this should cause a problem if lua code tries to access a a member varable on a string. I changed the view option to a table and set the docs object as well. The error went away. I still have a string set to 'view' in linux though. And i recieve no such error. Only other difference I can spot is luaJIT version 2.1.0 on linux and luaJIT version 2.1.16 on windows