which-key.nvim icon indicating copy to clipboard operation
which-key.nvim copied to clipboard

feature: Possibility to use a keybinding with different arguments each time

Open zellidev0 opened this issue 1 year ago • 0 comments

Did you check the docs?

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

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

I'm trying to register a keybinding for IncRename but this general question came up.

When the cursor is on a LSP identifier and I'm entering the command :IncRename xxx it renames the word under the cursor to xxx while highlighting it in the buffer. The problem is xxx of course is a custom name for a LSP identifier that changes when running the command for another variable, function etc. Which-key can (to my limited knowledge) only register a final command that does not get arguments of any kind.

I tried registering it in different ways but I can never enter a unique argument when running the command each time.

wk.register({
  ["<leader>r"] = { name = "refactor" },
  ["<leader>rvariant1"] = { "<Cmd>IncRename", "Rename" }, -- not working due to missing <CR>
  ["<leader>rvariant2"] = { -- not working because no argument is passed
    "<Cmd>lua vim.api.nvim_command(':IncRename ' )<CR>" , "Rename current word"
  },
})

Describe the solution you'd like

I'm looking for a way to register a command that receives an argument so I can press the keybinding and simply start typing then. Like so (naive approach with pseudo code):

wk.register({
  ["<leader>r"] = { name = "refactor" },
  ["<leader>rvariant1"] = { "<Cmd>IncRename <ARG1><CR>", "Rename" },
})

Where <ARG1> is unique in each run of the command.

Describe alternatives you've considered

My current alternative is to register the keybinding like so: And use which-key only for the documentation.

vim.keymap.set("n", "<leader>rn", function()
  return ":IncRename " .. vim.fn.expand("<cword>")
end, { expr = true })
wk.register({
  ["<leader>r"] = { name = "refactor" },
  ["<leader>rr"] = { "Rename" },
})

Additional context

None, but thanks for the help.

zellidev0 avatar Sep 18 '23 17:09 zellidev0