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.12k stars 163 forks source link

bug: Buffer local mappings descriptions don't work #629

Closed mehalter closed 1 month ago

mehalter commented 1 month ago

Did you check docs and existing issues?

Neovim version (nvim -v)

0.10.0

Operating system/version

Arch Linux

Describe the bug

Love the new v3 update so far! I stumbled across one bug so far though. I found that buffer local mapping groups set up with just a description for "menu titles" don't seem to have any effect. 2024-07-12_16:13:10_screenshot

Steps To Reproduce

  1. nvim -u repro.lua, start neovim
  2. :lua vim.g.test_function(), set the buffer local mappings to the current buffer
  3. \, press the leader key and see that there is no description "Test"
  4. Go into visual mode and see the same thing

Expected Behavior

Descriptions for buffer local keymapping groups should be displayed in which-key

Repro

vim.env.LAZY_STDPATH = ".repro"
load(vim.fn.system("curl -s https://raw.githubusercontent.com/folke/lazy.nvim/main/bootstrap.lua"))()

require("lazy.minit").repro({
  spec = {
    "folke/which-key.nvim",
  },
})

require("which-key").setup({})

vim.g.test_function = function()
  local buf = vim.api.nvim_get_current_buf()
  vim.keymap.set({ "v", "n" }, "<Leader>gg", function()
    vim.print("Hello, World!")
  end, { desc = "TEST MAPPING", buffer = buf })
  vim.keymap.set({ "v", "n" }, "<Leader>gG", function()
    vim.print("Hello, World 2!")
  end, { desc = "TEST MAPPING 2", buffer = buf })

  require("which-key").add({
    { "<Leader>g", desc = "Test", mode = "v", buffer = buf },
    { "<Leader>g", desc = "Test", mode = "n", buffer = buf },
  })
end
folke commented 1 month ago

A minit user in the wild :) I'm not even using that repro yet. Need to do that soon.

As for your question. There is no group description in the repro?

folke commented 1 month ago

oh wait. The desc shown in which-key should be the ones provided by add(). Will check

folke commented 1 month ago

A group shold be like below, but it still doesnt work:

  require("which-key").add({
    { "<Leader>g", group = "Test", mode = { "n", "v" }, buffer = buf },
  })
mehalter commented 1 month ago

For mappings without a right hand side, would just setting the desc work as well? Or it should always be group? It seems like in the global context either seem to work

mehalter commented 1 month ago

The docs indicate that desc is a required field (https://github.com/folke/which-key.nvim?tab=readme-ov-file#%EF%B8%8F-setup)

Is this the case? Also what would the meaning of specifying group and desc in a mapping? What's the reason for separating it out since only one label can ever be displayed?

mehalter commented 1 month ago

That fixed it, thanks so much! Still a bit curious if it's alright to only use desc in this way or if there is something I'm missing that could bite me down the road.

Thanks so much for such an expedient fix!!!

folke commented 1 month ago

Fixed.

Need to update the docs. Either group or desc needs to be present.

The difference matters to know which mappings should still be shown.

A single mapping without children can only be removed when it's a group mapping.

Other single mappings (like the ones LazyVim adds for mini.ai), can't be removed.

folke commented 1 month ago

to be clear: manual mappings (so no real keymaps) that don't have any children are not shown, unless they are not a group node.

folke commented 1 month ago

So for this specific use-case it's fine to use desc as well