Skip to content

feat: add sync ui select util #345

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 26, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions lua/java/utils/ui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,30 @@ function M.select(prompt, values, format_item, opts)
end)
end

--Sync vim.ui.select function
---@generic T
---@param prompt string
---@param values T[]
---@param format_item? fun(item: T, index: number): string
---@param opts? { return_one: boolean }
---@return T | nil
function M.select_sync(prompt, values, format_item, opts)
opts = opts or { prompt_single = false }

if not opts.prompt_single and #values == 1 then
return values[1]
end

local labels = { prompt }
for index, value in ipairs(values) do
table.insert(labels, format_item and format_item(value, index) or value)
end

local selected_index = vim.fn.inputlist(labels)

return values[selected_index]
end

---Async vim.ui.select function
---@generic T
---@param prompt string
Expand Down
Loading