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: `register` method fails when `<leader>` and `<localleader>` share part of the same sequence. #450

Closed danilevy1212 closed 3 months ago

danilevy1212 commented 1 year ago

Did you check docs and existing issues?

Neovim version (nvim -v)

NVIM v0.9.0

Operating system/version

NixOS 23.05 x86_64 Linux

Describe the bug

register method fails when <leader> and <localleader> share part of the same sequence. I get the following error:

E5108: Error executing lua .../pack/packer/start/which-key.nvim/lua/which-key/util.lua:128: {
  internal = { " ", "m", "a" },
  keystr = "<localleader>a",
  notation = { "<leader>", "a" }
}
stack traceback:
        [C]: in function 'error'
        .../pack/packer/start/which-key.nvim/lua/which-key/util.lua:128: in function 'parse_keys'
        ...k/packer/start/which-key.nvim/lua/which-key/mappings.lua:183: in function <...k/packer/start/which-key.nvim/lua/which-key/mappings.lua:171>
        vim/shared.lua: in function 'parse'
        .../pack/packer/start/which-key.nvim/lua/which-key/keys.lua:203: in function 'register'
        .../pack/packer/start/which-key.nvim/lua/which-key/init.lua:73: in function 'register'
        [string ":source (no file)"]:1: in main chunk

Seems like [https://github.com/folke/which-key.nvim/blob/4acffc92953a90a790603bfdab7c92319ab167b1/lua/which-key/util.lua#L127-L129](this line) is what is failing.

Steps To Reproduce

  1. Setup leader and localleader
vim.g.mapleader = ' '
vim.g.maplocalleader = ' m'
  1. Try to register a keybinding with localleader
require('which-key').register({
    a = {
        function()
            vim.cmd('echo "hi"')
        end,
        "Hello"
    },
}, { prefix = '<localleader>' })
  1. Get the following error:
E5108: Error executing lua .../pack/packer/start/which-key.nvim/lua/which-key/util.lua:128: {
  internal = { " ", "m", "a" },
  keystr = "<localleader>a",
  notation = { "<leader>", "a" }
}
stack traceback:
        [C]: in function 'error'
        .../pack/packer/start/which-key.nvim/lua/which-key/util.lua:128: in function 'parse_keys'
        ...k/packer/start/which-key.nvim/lua/which-key/mappings.lua:183: in function <...k/packer/start/which-key.nvim/lua/which-key/mappings.lua:171>
        vim/shared.lua: in function 'parse'
        .../pack/packer/start/which-key.nvim/lua/which-key/keys.lua:203: in function 'register'
        .../pack/packer/start/which-key.nvim/lua/which-key/init.lua:73: in function 'register'
        [string ":source (no file)"]:1: in main chunk

Expected Behavior

The mapping <leader>ma is set. Equivalent to:

vim.keymap.set('n', '<localleader>a', function vim.cmd('echo "hi"') end)

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",
  -- add any other plugins here
}

require("lazy").setup(plugins, {
  root = root .. "/plugins",
})

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

--- Set leader keys
vim.g.mapleader = ' '
vim.g.maplocalleader = ' m'

--- Set the keys
require('which-key').register({
    a = {
        function()
            vim.cmd('echo "hi"')
        end,
        "Hello"
    },
}, { prefix = '<localleader>' })
github-actions[bot] commented 4 months ago

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 7 days.