Skip to content

Commit

Permalink
feat: enable telescope customization for vim.ui.select caller
Browse files Browse the repository at this point in the history
  • Loading branch information
stevearc committed May 27, 2022
1 parent 55e4cea commit e607dd9
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
13 changes: 10 additions & 3 deletions lua/dressing/select/telescope.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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.
Expand All @@ -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

0 comments on commit e607dd9

Please sign in to comment.