mirror of
https://github.com/neovim/neovim.git
synced 2026-04-24 08:15:41 +00:00
feat(ui): add vim.ui.select and use in code actions (#15771)
Continuation of https://github.com/neovim/neovim/pull/15202 A plugin like telescope could override it with a fancy implementation and then users would get the telescope-ui within each plugin that utilizes the vim.ui.select function. There are some plugins which override the `textDocument/codeAction` handler solely to provide a different UI. With custom client commands and soon codeAction resolve support, it becomes more difficult to implement the handler right - so having a dedicated way to override the picking function will be useful.
This commit is contained in:
committed by
GitHub
parent
6736ee8be5
commit
63fde086d9
36
runtime/lua/vim/ui.lua
Normal file
36
runtime/lua/vim/ui.lua
Normal file
@@ -0,0 +1,36 @@
|
||||
local M = {}
|
||||
|
||||
--- Prompts the user to pick a single item from a collection of entries
|
||||
---
|
||||
---@param items table Arbitrary items
|
||||
---@param opts table Additional options
|
||||
--- - prompt (string|nil)
|
||||
--- Text of the prompt. Defaults to `Select one of:`
|
||||
--- - format_item (function item -> text)
|
||||
--- Function to format an
|
||||
--- individual item from `items`. Defaults to `tostring`.
|
||||
---@param on_choice function ((item|nil, idx|nil) -> ())
|
||||
--- Called once the user made a choice.
|
||||
--- `idx` is the 1-based index of `item` within `item`.
|
||||
--- `nil` if the user aborted the dialog.
|
||||
function M.select(items, opts, on_choice)
|
||||
vim.validate {
|
||||
items = { items, 'table', false },
|
||||
on_choice = { on_choice, 'function', false },
|
||||
}
|
||||
opts = opts or {}
|
||||
local choices = {opts.prompt or 'Select one of:'}
|
||||
local format_entry = opts.format_entry or tostring
|
||||
for i, item in pairs(items) do
|
||||
table.insert(choices, string.format('%d: %s', i, format_entry(item)))
|
||||
end
|
||||
local choice = vim.fn.inputlist(choices)
|
||||
if choice < 1 or choice > #items then
|
||||
on_choice(nil, nil)
|
||||
else
|
||||
on_choice(items[choice], choice)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
return M
|
||||
Reference in New Issue
Block a user