Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: API to refresh which-key mappings after plugin loading #944

Open
1 task done
daUnknownCoder opened this issue Feb 4, 2025 · 0 comments
Open
1 task done
Labels
enhancement New feature or request

Comments

@daUnknownCoder
Copy link

Did you check the docs?

  • I have read all the which-key.nvim docs

Is your feature request related to a problem? Please describe.

if i have some plugin which generates keybindings according to the conditions, which-key does not update the new ones in the prompt

Describe the solution you'd like

an API like require("which-key").refresh()?

Describe alternatives you've considered

for example, i use this plugin: https://github.com/j-morano/buffer_manager.nvim/, and for navigation between buffers i can do leader<number> to jump between buffers 1-10, it has this code

buffer-manager repo
  {
    "j-morano/buffer_manager.nvim",
    lazy = true,
    event = { "BufAdd", "BufDelete", "BufEnter" },
    keys = {
      { ",,", "<cmd>lua require('buffer_manager.ui').toggle_quick_menu()<CR>", desc = "Buffer Manager" },
    },
    config = function()
      local bm_status_ok, bm = pcall(require, "buffer_manager")
      if not bm_status_ok then
        print("Buffer Manager not found!")
      end
      bm.setup({
        select_menu_item_commands = {
          edit = { key = "<CR>", command = "edit" },
          v = { key = "<C-v>", command = "vsplit" },
          h = { key = "<C-h>", command = "split" },
        },
        short_file_names = true,
        short_term_names = true,
      })
      local keys = "1234567890"
      local bmui = require("buffer_manager.ui")
      for i = 1, #keys do
        local key = keys:sub(i, i)
        local desc = "Switch Buffer: " .. (i == 10 and "10" or key)
        vim.keymap.set({ "t", "n" }, string.format(",%s", key), function()
          bmui.nav_file(i)
        end, { desc = desc, noremap = true })
      end
      vim.keymap.set({ "t", "n" }, ",s", function()
        bmui.toggle_quick_menu()
        vim.defer_fn(function()
          vim.fn.feedkeys("/")
        end, 50)
      end, { desc = "Search Buffer", noremap = true })
      vim.api.nvim_set_hl(0, "BufferManagerModified", { fg = "#50fa7b" })
    end,
  },

to jump between buffers, but it is not dynamic (if i dont have buffers from 6-10, it throws error), so i wrote this code:

local function count_buffers()
  local count = 1
  for _, buffer in ipairs(vim.api.nvim_list_bufs()) do
    if vim.api.nvim_buf_is_loaded(buffer) then
      count = count + 1
    end
  end
  return count
end

local buffer_count = count_buffers()

for i = 1, buffer_count do
  local key = keys:sub(i, i)
  vim.keymap.set({ "t", "n" }, string.format(",%s", key), function()
    bmui.nav_file(i)
  end, { desc = "Switch Buffer: " .. key, noremap = true })
end

which dynamically makes keymaps so now it doesnt refresh the which-key window

Additional context

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant