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

118 lines
2.9 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.custom_kind = {
codeaction = function(opts, defaults, items)
local entry_display = require("telescope.pickers.entry_display")
local finders = require("telescope.finders")
local displayer = entry_display.create({
separator = " ",
items = {
{ remaining = true },
{ width = 16 },
},
})
local function make_display(entry)
local client_name = vim.lsp.get_client_by_id(entry.client_id).name
local columns = {
opts.format_item(entry.value),
{ client_name, "Comment" },
}
return displayer(columns)
end
defaults.finder = finders.new_table({
results = items,
entry_maker = function(item)
return {
display = make_display,
client_id = item[1],
ordinal = opts.format_item(item),
value = item,
}
end,
})
end,
}
2021-12-03 01:43:52 +00:00
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
-- Default to the dropdown theme if no options supplied
if picker_opts == nil then
picker_opts = themes.get_dropdown()
end
local defaults = {
2021-12-03 01:43:52 +00:00
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()
2022-04-23 04:51:20 +00:00
local callback = on_choice
-- Replace on_choice with a no-op so closing doesn't trigger it
on_choice = function(_, _) end
2022-04-23 04:51:20 +00:00
actions.close(prompt_bufnr)
if not selection then
-- User did not select anything.
2022-04-23 04:51:20 +00:00
callback(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
2022-04-23 04:51:20 +00:00
callback(selection.value, idx)
2021-12-03 01:43:52 +00:00
end)
2022-04-23 04:51:20 +00:00
actions.close:enhance({
post = function()
on_choice(nil, nil)
end,
})
2021-12-03 01:43:52 +00:00
return true
end,
}
if M.custom_kind[opts.kind] then
M.custom_kind[opts.kind](opts, defaults, items)
end
-- Hook to allow the caller of vim.ui.select to customize the telescope opts
if opts.telescope then
pickers.new(opts.telescope, defaults):find()
else
pickers.new(picker_opts, defaults):find()
end
2021-12-03 01:43:52 +00:00
end
return M