From 4848f851f67eb3c9976571f74208f28dbee7994b Mon Sep 17 00:00:00 2001 From: Steven Arcangeli Date: Fri, 17 Dec 2021 11:20:16 -0800 Subject: [PATCH] feat(select): allow user to override format_item (#6) --- doc/dressing.txt | 18 ++++++++++++++++++ doc/tags | 1 + lua/dressing/config.lua | 3 +++ lua/dressing/select/init.lua | 7 +++++-- 4 files changed, 27 insertions(+), 2 deletions(-) diff --git a/doc/dressing.txt b/doc/dressing.txt index cfc9e33..0050df4 100644 --- a/doc/dressing.txt +++ b/doc/dressing.txt @@ -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 diff --git a/doc/tags b/doc/tags index f2a5270..d20ad4c 100644 --- a/doc/tags +++ b/doc/tags @@ -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* diff --git a/lua/dressing/config.lua b/lua/dressing/config.lua index 438cc9b..e32ddfb 100644 --- a/lua/dressing/config.lua +++ b/lua/dressing/config.lua @@ -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, }, diff --git a/lua/dressing/select/init.lua b/lua/dressing/select/init.lua index 8210da7..7da483f 100644 --- a/lua/dressing/select/init.lua +++ b/lua/dressing/select/init.lua @@ -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