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

Lua: improve vim.keymap.set() #28536

Open
justinmk opened this issue Apr 27, 2024 · 4 comments
Open

Lua: improve vim.keymap.set() #28536

justinmk opened this issue Apr 27, 2024 · 4 comments
Labels
enhancement feature request lua stdlib
Milestone

Comments

@justinmk
Copy link
Member

justinmk commented Apr 27, 2024

Problem

keymap.set has a suboptimal signature, it could be improved

Expected behavior

  • add an overloaded signature so that the rhs can be the last arg. function keymap.set(mode, lhs, opts, rhs)
    • Callback should always be the last arg, where possible. That is a readability enhancement and a project standard.
    • The old signature will also be supported. but discouraged
  • lhs can be a list. This reduces boilerplate when defining aliases for a mapping.

Comparison

Before:

vim.keymap.set('n', 'crr', function()
  vim.lsp.buf.code_action()
end, { desc = 'vim.lsp.buf.code_action()' })

After:

vim.keymap.set('n', 'crr', { desc = 'vim.lsp.buf.code_action()' }, function()
  vim.lsp.buf.code_action()
end)
@justinmk justinmk added enhancement feature request lua stdlib labels Apr 27, 2024
@justinmk justinmk added this to the 0.11 milestone Apr 27, 2024
@justinmk
Copy link
Member Author

What is the objection to this?

@clason
Copy link
Member

clason commented Apr 28, 2024

Personally, I don't see the claimed readability benefit, so the churn seems not worth it. And it conflicts with the (in my opinion stronger) project standard of "optional keyword-style arguments last".

@mfussenegger
Copy link
Member

mfussenegger commented Apr 28, 2024

No objection from my side, but I suspect part of the objection is that in most personal keymaps people just don't set a description, so they already have:

vim.keymap.set('n', 'crr', function()
  vim.lsp.buf.code_action()
end)

And with this change they'd have to write:

vim.keymap.set('n', 'crr', nil, function()
  vim.lsp.buf.code_action()
end)

The nudge towards adding a description might not be the worst, but imho there's no clear advantage to the proposed change for most users.
Besides, in this particular case one could just write:

vim.keymap.set('n', 'crr', vim.lsp.buf.code_action, { desc = 'vim.lsp.buf.code_action()' })

Opposed to autocmds, keymaps don't (yet?) provide a context as argument to the called function, so using the function reference is currently save.

@justinmk
Copy link
Member Author

And with this change they'd have to write:

vim.keymap.set('n', 'crr', nil, function()
  vim.lsp.buf.code_action()
end)

good point. so instead, I have updated the proposal to be an overload instead of deprecating the old form.

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

No branches or pull requests

3 participants