2021-12-03 01:43:52 +00:00
|
|
|
local M = {}
|
|
|
|
|
|
|
|
M.is_supported = function()
|
|
|
|
return pcall(require, "telescope")
|
|
|
|
end
|
|
|
|
|
2022-05-27 04:54:33 +00:00
|
|
|
M.custom_kind = {
|
|
|
|
codeaction = function(opts, defaults, items)
|
|
|
|
local entry_display = require("telescope.pickers.entry_display")
|
|
|
|
local finders = require("telescope.finders")
|
2022-05-30 01:42:36 +00:00
|
|
|
local displayer
|
2022-05-27 04:54:33 +00:00
|
|
|
|
|
|
|
local function make_display(entry)
|
|
|
|
local columns = {
|
2022-09-15 14:41:02 +00:00
|
|
|
{ entry.idx .. ":", "TelescopePromptPrefix" },
|
2022-05-30 01:42:36 +00:00
|
|
|
entry.text,
|
|
|
|
{ entry.client_name, "Comment" },
|
2022-05-27 04:54:33 +00:00
|
|
|
}
|
|
|
|
return displayer(columns)
|
|
|
|
end
|
2022-05-30 01:42:36 +00:00
|
|
|
|
|
|
|
local entries = {}
|
|
|
|
local client_width = 1
|
|
|
|
local text_width = 1
|
2022-09-15 12:23:31 +00:00
|
|
|
local idx_width = 1
|
2022-09-15 09:50:30 +00:00
|
|
|
for idx, item in ipairs(items) do
|
2022-05-30 01:42:36 +00:00
|
|
|
local client_id = item[1]
|
|
|
|
local client_name = vim.lsp.get_client_by_id(client_id).name
|
|
|
|
local text = opts.format_item(item)
|
2022-09-15 12:23:31 +00:00
|
|
|
|
|
|
|
client_width = math.max(client_width, vim.api.nvim_strwidth(client_name))
|
|
|
|
text_width = math.max(text_width, vim.api.nvim_strwidth(text))
|
|
|
|
idx_width = math.max(idx_width, vim.api.nvim_strwidth(tostring(idx)))
|
|
|
|
|
2022-05-30 01:42:36 +00:00
|
|
|
table.insert(entries, {
|
2022-09-15 09:50:30 +00:00
|
|
|
idx = idx,
|
2022-05-30 01:42:36 +00:00
|
|
|
display = make_display,
|
|
|
|
text = text,
|
|
|
|
client_name = client_name,
|
2022-09-15 13:14:33 +00:00
|
|
|
ordinal = idx .. " " .. text .. " " .. client_name,
|
2022-05-30 01:42:36 +00:00
|
|
|
value = item,
|
|
|
|
})
|
|
|
|
end
|
|
|
|
displayer = entry_display.create({
|
|
|
|
separator = " ",
|
|
|
|
items = {
|
2022-09-15 09:50:30 +00:00
|
|
|
{ width = idx_width + 1 },
|
2022-05-30 01:42:36 +00:00
|
|
|
{ width = text_width },
|
|
|
|
{ width = client_width },
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2022-05-27 04:54:33 +00:00
|
|
|
defaults.finder = finders.new_table({
|
2022-05-30 01:42:36 +00:00
|
|
|
results = entries,
|
2022-05-27 04:54:33 +00:00
|
|
|
entry_maker = function(item)
|
2022-05-30 01:42:36 +00:00
|
|
|
return item
|
2022-05-27 04:54:33 +00:00
|
|
|
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
|
|
|
|
|
2023-05-25 05:59:28 +00:00
|
|
|
-- schedule_wrap because closing the windows is deferred
|
|
|
|
-- See https://github.com/nvim-telescope/telescope.nvim/pull/2336
|
|
|
|
-- And we only want to dispatch the callback when we're back in the original win
|
|
|
|
on_choice = vim.schedule_wrap(on_choice)
|
|
|
|
|
2021-12-03 01:43:52 +00:00
|
|
|
local entry_maker = function(item)
|
|
|
|
local formatted = opts.format_item(item)
|
|
|
|
return {
|
|
|
|
display = formatted,
|
|
|
|
ordinal = formatted,
|
|
|
|
value = item,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2022-03-17 04:03:10 +00:00
|
|
|
local picker_opts = config
|
|
|
|
|
2022-03-17 17:01:53 +00:00
|
|
|
-- Default to the dropdown theme if no options supplied
|
2022-03-17 04:03:10 +00:00
|
|
|
if picker_opts == nil then
|
|
|
|
picker_opts = themes.get_dropdown()
|
|
|
|
end
|
2022-02-26 13:26:05 +00:00
|
|
|
|
2022-05-27 02:45:23 +00:00
|
|
|
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
|
2022-05-27 02:45:23 +00:00
|
|
|
on_choice = function(_, _) end
|
2022-04-23 04:51:20 +00:00
|
|
|
actions.close(prompt_bufnr)
|
2021-12-06 00:29:37 +00:00
|
|
|
if not selection then
|
|
|
|
-- User did not select anything.
|
2022-04-23 04:51:20 +00:00
|
|
|
callback(nil, nil)
|
2021-12-06 00:29:37 +00:00
|
|
|
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,
|
2022-05-27 02:45:23 +00:00
|
|
|
}
|
|
|
|
|
2022-05-27 04:54:33 +00:00
|
|
|
if M.custom_kind[opts.kind] then
|
|
|
|
M.custom_kind[opts.kind](opts, defaults, items)
|
|
|
|
end
|
|
|
|
|
2022-05-27 02:45:23 +00:00
|
|
|
-- 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
|