stevearc / dressing.nvim

Neovim plugin to improve the default vim.ui interfaces
MIT License
1.7k stars 32 forks source link

[Question] Creating a simple :Maps function #87

Closed phcerdan closed 7 months ago

phcerdan commented 1 year ago

Hi, lua noob here. I want to emulate the :Maps command from fzf.vim, which allows you to pick a nmap via fzf fuzzy finder ui.

How do I pass the content from :verbose nmap to a vim.fn.input, so I can enjoy dressing.nvim?

Thanks!

stevearc commented 1 year ago

Hi! Since you described yourself as a lua noob, I'll give you some background and advice that doesn't directly answer your question, but I think may be more useful for what you're trying to do.

There are a bunch of different "fuzzy selector" implementations out there (fzf.vim, fzf-lua, telescope), all with different APIs. When writing a plugin for general consumption, sometimes it's useful to prompt the user to choose from a list of items. The trouble for the plugin author is: I don't know which of these selection plugins the user has installed, so I would need to write one implementation for each one they could possibly have.

This is where vim.ui.select comes in. This core API was released in Neovim 0.6. By default it would use vim.fn.inputlist(), which is functional but not pretty. The key is that it provides a single API in Neovim core that plugin authors can call when they want the user to select something. Then, if the user wants a prettier selector, they can overwrite the implementation of vim.ui.select to use fzf, or telescope, or anything else.

dressing.nvim is a tool that provides that last step: the shim layer that translates the vim.ui.select API into the API required by fzf, telescope, or other implementations.

All of that said, unless you are writing a plugin, there is no need to use vim.ui.select. You can use it if it's the best API for the job, but there's no reason to not call :Maps directly. After all, it's your system and you know that you have fzf installed, so why not just use it?

But if you read all of this and decided that actually you do still want to select a mapping using vim.ui.select, then you should look at :help nvim_get_keymap() to get the list of all keymaps and :help vim.ui.select to see the API of that function. From there it's just a matter of translating the keymaps into the format expected by select. Bonus tip: you can run lua commands directly in vim with :lua vim.ui.select({'first', 'second'}, {}, function() end) to see what they do, and you can prefix the command with = to inspect the output :lua =vim.api.nvim_get_keymap("n")