hrsh7th / nvim-cmp

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

cmp-config.enabled return value expects table, not boolean? #1747

Closed 45mg closed 8 months ago

45mg commented 8 months ago

FAQ

Announcement

Minimal reproducible full config

let plugdir=$HOME."/.config/nvim/plugged"

call plug#begin(expand(plugdir))
    Plug 'hrsh7th/nvim-cmp'
    Plug 'hrsh7th/cmp-buffer'
call plug#end()

lua<<EOF

vim.g.cmptoggle = false
vim.keymap.set({"n", "i"}, "<C-e>", "<cmd>lua vim.g.cmptoggle = not vim.g.cmptoggle<CR>", { desc = "toggle nvim-cmp" })

local cmp = require'cmp'
cmp.setup({
    enabled = function()
    return vim.g.cmptoggle
    end,
    completion = {
       autocomplete = true,
    },
    sources = cmp.config.sources({
        name = 'buffer'
    })
})
EOF

Based on this suggestion, which was based on your suggestion.

Description

Use <C-e> to toggle whether enabled or not. When disabled, no issues. When enabled, this error on every keypress in insert-mode -

E5108: Error executing lua vim/shared.lua:0: t: expected table, got boolean
stack traceback:
        [C]: in function 'error'
        vim/shared.lua: in function 'validate'
        vim/shared.lua: in function 'tbl_contains'
        /home/user/.config/nvim/plugged/nvim-cmp/lua/cmp/core.lua:168: in function 'autoindent'
        /home/user/.config/nvim/plugged/nvim-cmp/lua/cmp/core.lua:161: in function 'on_change'
        /home/user/.config/nvim/plugged/nvim-cmp/lua/cmp/init.lua:329: in function </home/user/.config/nvim/plugged/nvim-cmp/lua/cmp/init.lua:324>
        ....config/nvim/plugged/nvim-cmp/lua/cmp/utils/feedkeys.lua:47: in function <....config/nvim/plugged/nvim-cmp/lua/cmp/utils/feedkeys.lua:45>
Press ENTER or type command to continue

Steps to reproduce

nvim -u cmp-repro.vim

Toggle with <C-e> as described above.

Expected behavior

Should begin autocompleting when plugin is enabled, and do nothing when plugin is disabled.

Actual behavior

Correctly does nothing when disabled, but gives above-mentioned error when enabled.

Additional context

Based on this suggestion, I'm guessing that enabled expects its function to return some kind of function value. Without familiarity with the codebase, I can't say for sure.

The plugin help clearly states -

                                                            cmp-config.enabled
enabled
  boolean | fun(): boolean
  Toggles the plugin on and off.

which is probably not true based on the error.

Sam-programs commented 8 months ago

the issue is in

  completion = {
       autocomplete = true,
    },

autocomplete is on by default and it should only be a trigger event array or false

completion.autocomplete 
  `cmp.TriggerEvent[] | false`
  The event to trigger autocompletion. If set to `false`, then completion is
  only invoked manually (e.g. by calling `cmp.complete`).
      if vim.tbl_contains(config.get().completion.autocomplete or {}, trigger_event) then

this if statement just puts autocomplete as trigger event if it is not (false or nil)

45mg commented 8 months ago

Ah, yes, my mistake. My config worked fine after removing that block. Thank you!