feat: enable telescope customization for vim.ui.select caller

This commit is contained in:
Steven Arcangeli 2022-05-26 19:45:23 -07:00
parent 55e4ceae81
commit e607dd99ae
2 changed files with 40 additions and 3 deletions

View File

@ -16,6 +16,7 @@ options in the functions. Customization will be done entirely using a separate
- [Installation](#installation)
- [Configuration](#configuration)
- [Advanced configuration](#advanced-configuration)
- [Notes for plugin authors](#notes-for-plugin-authors)
- [Alternative and related projects](#alternative-and-related-projects)
## Requirements
@ -265,6 +266,35 @@ require('dressing').setup({
```
## Notes for plugin authors
TL;DR: you can customize the telescope `vim.ui.select` implementation by passing `telescope` into `opts`.
The `vim.ui` hooks are a great boon for us because we can now assume that users
will have a reasonable UI available for simple input operations. We no longer
have to build separate implementations for each of fzf, telescope, ctrlp, etc.
The tradeoff is that `vim.ui.select` is less customizable than any of these
options, so if you wanted to have a preview window (like telescope supports), it
is no longer an option.
My solution to this is extending the `opts` that are passed to `vim.ui.select`.
You can add a `telescope` field that will be passed directly into the picker,
allowing you to customize any part of the UI. If a user has both dressing and
telescope installed, they will get your custom picker UI. If either of those
are not true, the selection UI will gracefully degrade to whatever the user has
configured for `vim.ui.select`.
An example of usage:
```lua
vim.ui.select({'apple', 'banana', 'mango'}, {
prompt = "Title",
telescope = require("telescope.themes").get_cursor(),
}, function(selected) end)
```
For now this is available only for the telescope backend, but feel free to request additions.
## Alternative and related projects
- [telescope-ui-select](https://github.com/nvim-telescope/telescope-ui-select.nvim) - provides a `vim.ui.select` implementation for telescope

View File

@ -28,7 +28,7 @@ M.select = function(config, items, opts, on_choice)
picker_opts = themes.get_dropdown()
end
pickers.new(picker_opts, {
local defaults = {
prompt_title = opts.prompt,
previewer = false,
finder = finders.new_table({
@ -41,7 +41,7 @@ M.select = function(config, items, opts, on_choice)
local selection = state.get_selected_entry()
local callback = on_choice
-- Replace on_choice with a no-op so closing doesn't trigger it
on_choice = function() end
on_choice = function(_, _) end
actions.close(prompt_bufnr)
if not selection then
-- User did not select anything.
@ -66,7 +66,14 @@ M.select = function(config, items, opts, on_choice)
return true
end,
}):find()
}
-- Hook to allow the caller of vim.ui.select to customize the telescope opts
if opts.telescope then
pickers.new(opts.telescope, defaults):find()
else
pickers.new(picker_opts, defaults):find()
end
end
return M