dressing.nvim/lua/dressing/select/telescope.lua

84 lines
2.1 KiB
Lua
Raw Normal View History

2021-12-03 01:43:52 +00:00
local M = {}
M.is_supported = function()
return pcall(require, "telescope")
end
M.select = function(config, items, opts, on_choice)
2022-03-14 18:49:29 +00:00
local themes = require("telescope.themes")
local actions = require("telescope.actions")
local state = require("telescope.actions.state")
local pickers = require("telescope.pickers")
local finders = require("telescope.finders")
2021-12-03 01:43:52 +00:00
local conf = require("telescope.config").values
local entry_maker = function(item)
local formatted = opts.format_item(item)
return {
display = formatted,
ordinal = formatted,
value = item,
}
end
local picker_opts = config
if picker_opts == nil then
-- Default to the dropdown theme if no options supplied
picker_opts = themes.get_dropdown()
elseif config.theme then
-- Backwards compatibility for the `theme` option
local theme
local ttype = type(config.theme)
if ttype == "string" then
theme = themes[string.format("get_%s", config.theme)]
elseif ttype == "function" then
theme = config.theme
else
theme = function(s)
return vim.tbl_extend("keep", s, config.theme or {})
end
end
picker_opts = vim.tbl_extend("keep", config, theme({}))
end
2021-12-03 01:43:52 +00:00
pickers.new(picker_opts, {
prompt_title = opts.prompt,
2022-03-17 03:48:49 +00:00
previewer = false,
2022-03-14 18:49:29 +00:00
finder = finders.new_table({
2021-12-03 01:43:52 +00:00
results = items,
entry_maker = entry_maker,
2022-03-14 18:49:29 +00:00
}),
2021-12-03 01:43:52 +00:00
sorter = conf.generic_sorter(opts),
attach_mappings = function(prompt_bufnr)
actions.select_default:replace(function()
local selection = state.get_selected_entry()
actions._close(prompt_bufnr, false)
if not selection then
-- User did not select anything.
2021-12-06 17:26:00 +00:00
on_choice(nil, nil)
return
end
2021-12-03 01:43:52 +00:00
local idx = nil
for i, item in ipairs(items) do
if item == selection.value then
idx = i
break
end
end
on_choice(selection.value, idx)
end)
actions.close:replace(function()
actions._close(prompt_bufnr, false)
on_choice(nil, nil)
end)
return true
end,
}):find()
end
return M