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.43k stars 177 forks source link

bug: Mapping is not hidden when `desc: fun():string` returns `which_key_ignore` #887

Open maomao9-0 opened 2 days ago

maomao9-0 commented 2 days ago

Did you check docs and existing issues?

Neovim version (nvim -v)

0.10.2

Operating system/version

Fedora release 41

Describe the bug

The desc parameter in wk.Spec accepts either a string or fun():string. However, the behavior to hide mapping when desc = which_key_ignore only works when desc is of type string.

When desc is of type fun():string, the mapping still shows and the label becomes "which_key_ignore".

I think this is related to #880

Steps To Reproduce

In my .config/nvim/lua/config.autocmds.lua, I have the following code to set up compilation of cpp file

vim.api.nvim_create_autocmd("FileType", {
  pattern = "cpp",
  callback = function()
    vim.keymap.set("n", "<leader>rr", function()
      -- some code to compile cpp file ...
    end, { buffer = true })
  end,
})

In my .config/nvim/lua/plugins/which-key.lua file, I have the following code

local function mapcheck(keymap)
  return vim.fn.maparg(keymap, "n") ~= ""
end
local function conditional_desc(keymap, desc)
  if mapcheck(keymap) then
    return desc
  else
    return "which_key_ignore"
  end
end
return {
  "folke/which-key.nvim",
  opts = {
    spec = {
      {
        "<leader>rr",
        desc = function()
          return conditional_desc("<leader>rr", "Run code")
        end,
      },
    }
  }
}

Then, open any file that is not a .cpp file. Then click "" followed by "r". Instead of hiding the mapping, it shows "which_key_ignore" instead.

image

Expected Behavior

The mapping should be dynamically hidden / shown based on whether the desc function returns "which_key_ignore" / some other string.

Health

No response

Log

No response

Repro

No response