folke / which-key.nvim

💥 Create key bindings that stick. WhichKey helps you remember your Neovim keymaps, by showing available keybindings in a popup as you type.
Apache License 2.0
5.38k stars 177 forks source link

bug: Lazy-loaded plugins not properly loaded when triggered #470

Closed TroySigX closed 1 year ago

TroySigX commented 1 year ago

Did you check docs and existing issues?

Neovim version (nvim -v)

0.9.1

Operating system/version

6.3.9-arch1-1

Describe the bug

Setting some keymaps using which-key for some lazy-loaded plugins causes some problems: When the plugin is triggered to load, if the mapped command is VimScript, the command cannot be executed.

Steps To Reproduce

  1. Load the repro config below
  2. press <F1>
  3. Nvim will show the error like this: Not an editor command: TroubleToggle

Expected Behavior

should be able to execute the command

Repro

-- bootstrap lazy
local lazypath = root .. "/plugins/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
  vim.fn.system({ "git", "clone", "--filter=blob:none", "https://github.com/folke/lazy.nvim.git", lazypath, })
end
vim.opt.runtimepath:prepend(lazypath)

-- install plugins
local plugins = {
  {
    'folke/trouble.nvim',
    dependencies = 'nvim-tree/nvim-web-devicons',
    keys = {
      { '<F1>', mode = { 'n', 'i', 'v' } },
    },
    config = function()
      require('trouble').setup {}
    end,
  },
  {
    'folke/which-key.nvim',
    event = 'VeryLazy',
    config = function()
        require('which-key').register({
          ['<F1>'] = { '<Esc>:TroubleToggle<CR>', 'Toggle Trouble List' }
        }, { mode = { 'n', 'i', 'v' } })
    end,
  },
}
require("lazy").setup(plugins, {
  root = root .. "/plugins",
})

vim.cmd.colorscheme("tokyonight")
-- add anything else here
TroySigX commented 1 year ago

It looks like for plugins like trouble, instead of triggering loading plugin using the keys property in lazy, I need to trigger by cmd

folke commented 1 year ago

You're defining the keymap with which-key, and are then also trying to lazy-load on the same keymap. That can't work.

You can just define the keymap fully with lazy.

Or alternatively, define it with which-key, remove the lazy key handler and lazy load on cmd instead.

You can't do both