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
4.75k stars 154 forks source link

bug: Crash on VimEnter when using `mapleader = "<C-Space>"` and setting a mapping with `<C-Space>` instead of `<Leader>` (or defining a group) #587

Closed bew closed 4 months ago

bew commented 4 months ago

Did you check docs and existing issues?

Neovim version (nvim -v)

0.9.5

Operating system/version

Linux (NixOS)

Describe the bug

I recently changed my main <Leader> to <C-Space> and opening neovim now shows an error message. And when I disable enough things to avoid this error message, the which-key popup is not displayed on <C-Space>..

Steps To Reproduce

  1. Run nvim -u repro.lua 😬

  2. See error:

    Error detected while processing VimEnter Autocommands for "*":
    E5108: Error executing lua ...wip/.repro/plugins/which-key.nvim/lua/which-key/util.lua:128: {
      internal = { "<", "C", "-", "S", "p", "a", "c", "e", ">", "f", "o", "o" },
      keystr = "<Leader>foo",
      notation = { "<", "L", "e", "a", "d", "e", "r", ">", "f", "o", "o" }
    }
    stack traceback:
            [C]: in function 'error'
            ...wip/.repro/plugins/which-key.nvim/lua/which-key/util.lua:128: in function 'parse_keys'
            ....repro/plugins/which-key.nvim/lua/which-key/mappings.lua:183: in function <....repro/plugins/which-key.nvim/lua/which-key/mappings.lua:171>
            vim/shared.lua: in function 'parse'
            ...wip/.repro/plugins/which-key.nvim/lua/which-key/keys.lua:208: in function 'register'
            ...wip/.repro/plugins/which-key.nvim/lua/which-key/init.lua:94: in function 'load'
            [string ":lua"]:1: in main chunk
  3. Replace (in the repro) the which-key config with config = true to remove the group (to continue with another similar bug)

  4. Re-open nvim -u repro.lua

  5. See the same error again, this time for the mapping:

    Error detected while processing VimEnter Autocommands for "*":
    E5108: Error executing lua ...wip/.repro/plugins/which-key.nvim/lua/which-key/util.lua:128: {
      internal = { "<", "C", "-", "S", "p", "a", "c", "e", ">" },
      keystr = "<leader>",
      notation = { "<", "l", "e", "a", "d", "e", "r", ">" }
    }
    stack traceback:
            [C]: in function 'error'
            ...wip/.repro/plugins/which-key.nvim/lua/which-key/util.lua:128: in function 'parse_keys'
            ...wip/.repro/plugins/which-key.nvim/lua/which-key/keys.lua:342: in function 'add_hooks'
            ...wip/.repro/plugins/which-key.nvim/lua/which-key/keys.lua:350: in function 'add_hooks'
            ...wip/.repro/plugins/which-key.nvim/lua/which-key/keys.lua:334: in function 'update'
            ...wip/.repro/plugins/which-key.nvim/lua/which-key/init.lua:96: in function 'load'
            [string ":lua"]:1: in main chunk
  6. Replace <C-Space> with <Leader> in the keybind at the bottom of the repo (to continue with other similar bugs that might be related)

  7. No more error messages on VimEnter

  8. But there seems to be other issues that might be related: a. I can't use the <C-Space>i keybind at all in this repro (I don't see the msg being echo-ed) b. Typing <C-Space> and waiting for &timeoutlen does not show the which-key popup 🤔

Expected Behavior

No error on start, and working normally

Repro

-- DO NOT change the paths and don't remove the colorscheme
local root = vim.fn.fnamemodify("./.repro", ":p")

-- set stdpaths to use .repro
for _, name in ipairs({ "config", "data", "state", "cache" }) do
  vim.env[("XDG_%s_HOME"):format(name:upper())] = root .. "/" .. name
end

-- 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/tokyonight.nvim",
  {
    "folke/which-key.nvim",
    config = function()
      require"which-key".register { ["<Leader>foo"] = "+some-group" }
      -- CRASH because of this config
    end,
  },
  -- add any other plugins here
}
require("lazy").setup(plugins, {
  root = root .. "/plugins",
})

vim.cmd.colorscheme("tokyonight")
-- add anything else here

vim.g.mapleader = "<C-Space>"
vim.keymap.set("n", "<C-Space>i", "<cmd>echo 'hello from leader'<cr>")
-- CRASH because of this mapping

-- NOTE: does not error if I replace "<C-Space>i" with "<Leader>i"
bew commented 4 months ago

It was user error ><

https://github.com/neovim/neovim/issues/27826#issuecomment-1990134643

(Well ideally the plugin shouldn't crash, I'll let your re-open if you want to fix it)