From e607dd99aeb5ce21e9a3d8f4c028650db12bd3af Mon Sep 17 00:00:00 2001 From: Steven Arcangeli Date: Thu, 26 May 2022 19:45:23 -0700 Subject: [PATCH] feat: enable telescope customization for vim.ui.select caller --- README.md | 30 ++++++++++++++++++++++++++++++ lua/dressing/select/telescope.lua | 13 ++++++++++--- 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 14b9083..bc85762 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lua/dressing/select/telescope.lua b/lua/dressing/select/telescope.lua index 2c47382..7247cd0 100644 --- a/lua/dressing/select/telescope.lua +++ b/lua/dressing/select/telescope.lua @@ -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