feat(select): allow user to override format_item (#6)

This commit is contained in:
Steven Arcangeli 2021-12-17 11:20:16 -08:00
parent 3787e28932
commit 4848f851f6
4 changed files with 27 additions and 2 deletions

View File

@ -130,5 +130,23 @@ pass `prompt_buffer = true` to use that implementation.
There are slight visual differences in where the "prompt" text in placed, but
otherwise they should be functionally identical.
===============================================================================
*dressing-format*
Sometimes you may wish to change how choices are displayed for
`vim.ui.select`. The calling function can pass a specific "kind" to the select
function (for example, code actions from |vim.lsp.buf.code_action|
use kind="codeaction"). You can, in turn, specify an override for the
"format_item" function when selecting for that kind. For example, this
configuration will display the name of the language server next to code
actions:
>
format_item_override = {
codeaction = function(action_tuple)
local title = action_tuple[2].title:gsub("\r\n", "\\r\\n")
local client = vim.lsp.get_client_by_id(action_tuple[1])
return string.format("%s\t[%s]", title:gsub("\n", "\\n"), client.name)
end,
}
===============================================================================
vim:ft=help:et:ts=2:sw=2:sts=2:norl

View File

@ -1,6 +1,7 @@
Dressing dressing.txt /*Dressing*
dressing dressing.txt /*dressing*
dressing-configuration dressing.txt /*dressing-configuration*
dressing-format dressing.txt /*dressing-format*
dressing-prompt dressing.txt /*dressing-prompt*
dressing.nvim dressing.txt /*dressing.nvim*
dressing.txt dressing.txt /*dressing.txt*

View File

@ -78,6 +78,9 @@ local default_config = {
min_height = 10,
},
-- Used to override format_item. See :help dressing-format
format_item_override = {},
-- see :help dressing_get_config
get_config = nil,
},

View File

@ -28,8 +28,11 @@ return function(items, opts, on_choice)
opts = opts or {}
local config = global_config.get_mod_config("select", opts)
opts.prompt = opts.prompt or "Select one of:"
if opts.format_item then
-- Make format_item doesn't *technically* have to return a string for the
local format_override = config.format_item_override[opts.kind]
if format_override then
opts.format_item = format_override
elseif opts.format_item then
-- format_item doesn't *technically* have to return a string for the
-- core implementation. We should maintain compatibility by wrapping the
-- return value with tostring
local format_item = opts.format_item