mirror of
https://github.com/neovim/neovim.git
synced 2025-12-01 06:23:01 +00:00
feat(api): add support for lua function & description in keymap
Behavioral changes:
1. Added support for lua function in keymaps in
--------------------------------------------
- nvim_set_keymap
Can set lua function as keymap rhs like following:
```lua
vim.api.nvim_{buf_}set_keymap('n', '<leader>lr', '', {callback = vim.lsp.buf.references})
```
Note: lua function can only be set from lua . If api function being
called from viml or over rpc this option isn't available.
- nvim_{buf_}get_keymap
When called from lua, lua function is returned is `callback` key .
But in other cases callback contains number of the function ref.
- :umap, nvim_del_keymap & nvim_buf_del_keymap clears lua keymaps correctly.
- :map commands for displaing rhs .
For lua keymaps rhs is displayed as <Lua function ref_no>
Note: lua keymap cannot be set through viml command / functions.
- mapargs()
When dict is false it returns string in `<Lua function ref_no>`
format (same format as :map commands).
When dict is true it returns ref_no number in `callback` key.
- mapcheck()
returns string in `<Lua function ref_no>` format (same format as :map commands).
2. Added support for keymap description
---------------------------------------
- nvim_{buf_}set_keymap: added `desc` option in opts table .
```lua
vim.api.nvim_set_keymap('n', '<leader>w', '<cmd>w<cr>', {desc='Save current file'})
```
- nvim_{buf_}get_keymap: contains `desc` in returned list.
- commands like `:nmap <leader>w` will show description in a new line below rhs.
- `maparg()` return dict contains `desc`.
This commit is contained in:
@@ -1538,10 +1538,10 @@ Dictionary nvim_get_mode(void)
|
||||
/// @param mode Mode short-name ("n", "i", "v", ...)
|
||||
/// @returns Array of maparg()-like dictionaries describing mappings.
|
||||
/// The "buffer" key is always zero.
|
||||
ArrayOf(Dictionary) nvim_get_keymap(String mode)
|
||||
ArrayOf(Dictionary) nvim_get_keymap(uint64_t channel_id, String mode)
|
||||
FUNC_API_SINCE(3)
|
||||
{
|
||||
return keymap_array(mode, NULL);
|
||||
return keymap_array(mode, NULL, channel_id == LUA_INTERNAL_CALL);
|
||||
}
|
||||
|
||||
/// Sets a global |mapping| for the given mode.
|
||||
@@ -1566,7 +1566,10 @@ ArrayOf(Dictionary) nvim_get_keymap(String mode)
|
||||
/// @param lhs Left-hand-side |{lhs}| of the mapping.
|
||||
/// @param rhs Right-hand-side |{rhs}| of the mapping.
|
||||
/// @param opts Optional parameters map. Accepts all |:map-arguments|
|
||||
/// as keys excluding |<buffer>| but including |noremap|.
|
||||
/// as keys excluding |<buffer>| but including |noremap| and "desc".
|
||||
/// |desc| can be used to give a description to keymap.
|
||||
/// When called from Lua, also accepts a "callback" key that takes
|
||||
/// a Lua function to call when the mapping is executed.
|
||||
/// Values are Booleans. Unknown key is an error.
|
||||
/// @param[out] err Error details, if any.
|
||||
void nvim_set_keymap(String mode, String lhs, String rhs, Dict(keymap) *opts, Error *err)
|
||||
|
||||
Reference in New Issue
Block a user