Skip to content

Commit 5eea5d7

Browse files
petobenscleong14
authored andcommitted
feat: improve Telescope as action palette provider (olimorris#1195)
1 parent 2a8202d commit 5eea5d7

File tree

1 file changed

+68
-2
lines changed

1 file changed

+68
-2
lines changed

lua/codecompanion/providers/actions/telescope.lua

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,54 @@ local finders = require("telescope.finders")
22
local pickers = require("telescope.pickers")
33
local conf = require("telescope.config").values
44
local action_state = require("telescope.actions.state")
5+
local entry_display = require("telescope.pickers.entry_display")
6+
local previewers = require("telescope.previewers")
57
local telescope_actions = require("telescope.actions")
68

79
local log = require("codecompanion.utils.log")
810

11+
local function wrap_text_to_table(text, max_line_length)
12+
local lines = {}
13+
14+
for line in (text .. "\n"):gmatch("(.-)\n") do
15+
local tmp_line = ""
16+
for word in line:gmatch("%S+") do
17+
if #tmp_line + #word + (tmp_line == "" and 0 or 1) > max_line_length then
18+
table.insert(lines, tmp_line)
19+
tmp_line = word
20+
else
21+
tmp_line = tmp_line == "" and word or tmp_line .. " " .. word
22+
end
23+
end
24+
if tmp_line ~= "" then
25+
table.insert(lines, tmp_line)
26+
end
27+
end
28+
29+
return lines
30+
end
31+
32+
local action_previewer = previewers.new_buffer_previewer({
33+
define_preview = function(self, entry)
34+
local width = vim.api.nvim_win_get_width(self.state.winid)
35+
entry.preview_command(entry, self.state.bufnr, width)
36+
end,
37+
})
38+
39+
local function preview_command(entry, bufnr, width)
40+
vim.api.nvim_buf_call(bufnr, function()
41+
local preview
42+
if entry.value.prompts and entry.value.prompts[1] then
43+
local content = entry.value.prompts[1].content
44+
if type(content) == "string" then
45+
preview = content
46+
end
47+
end
48+
preview = preview or entry.value.description
49+
vim.api.nvim_buf_set_lines(bufnr, 0, -1, true, wrap_text_to_table(preview, width))
50+
end)
51+
end
52+
953
---@class CodeCompanion.Actions.Provider.Telescope: CodeCompanion.SlashCommand.Provider
1054
local Provider = {}
1155

@@ -22,20 +66,42 @@ end
2266
function Provider:picker(items, opts)
2367
opts = opts or {}
2468

69+
local max_name = 1
70+
for _, item in ipairs(items) do
71+
max_name = math.max(max_name, #item.name)
72+
end
73+
74+
local displayer = entry_display.create({
75+
separator = " ",
76+
items = {
77+
{ width = max_name + 1 },
78+
{ remaining = true },
79+
},
80+
})
81+
82+
local function make_display(entry)
83+
local columns = { entry.value.name }
84+
if entry.value.strategy then
85+
columns[2] = { entry.value.strategy, "Comment" }
86+
end
87+
return displayer(columns)
88+
end
89+
2590
return pickers
2691
.new(opts, {
2792
prompt_title = opts.prompt or "CodeCompanion actions",
2893
finder = finders.new_table({
2994
results = items,
3095
entry_maker = function(entry)
31-
local description = entry.description and " - " .. entry.description or ""
3296
return {
3397
value = entry,
34-
display = entry.name .. description,
98+
display = make_display,
3599
ordinal = entry.name,
100+
preview_command = preview_command,
36101
}
37102
end,
38103
}),
104+
previewer = action_previewer,
39105
sorter = conf.generic_sorter(opts),
40106
attach_mappings = function(bufnr, _)
41107
telescope_actions.select_default:replace(function()

0 commit comments

Comments
 (0)