Skip to content
This repository was archived by the owner on Jan 3, 2024. It is now read-only.

Commit 2b3344a

Browse files
committed
Telescope based selector for previews
I wish vim.ui.select had a way to supply the secondary window information
1 parent 52cca9d commit 2b3344a

File tree

4 files changed

+103
-6
lines changed

4 files changed

+103
-6
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,13 @@ local opts = {
237237
-- automatically call RustReloadWorkspace when writing to a Cargo.toml file.
238238
reload_workspace_from_cargo_toml = true,
239239

240+
code_action_group = {
241+
-- Function with signature of `vim.ui.select` used to display the code_actions
242+
-- default: require("rust-tools.code_action_group").telescope_select(),
243+
selector = require("rust-tools.code_action_group").telescope_select(),
244+
-- selector = vim.ui.select,
245+
},
246+
240247
-- These apply to the default RustSetInlayHints command
241248
inlay_hints = {
242249
-- automatically set inlay hints (type hints)

doc/rust-tools.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,13 @@ for keys that are not provided.
162162

163163
-- automatically call RustReloadWorkspace when writing to a Cargo.toml file.
164164
reload_workspace_from_cargo_toml = true,
165+
166+
code_action_group = {
167+
-- Function with signature of `vim.ui.select` used to display the code_actions
168+
-- default: require("rust-tools.code_action_group").telescope_select(),
169+
selector = require("rust-tools.code_action_group").telescope_select(),
170+
-- selector = vim.ui.select,
171+
},
165172

166173
-- These apply to the default RustSetInlayHints command
167174
inlay_hints = {

lua/rust-tools/code_action_group.lua

Lines changed: 82 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ local function apply_action(action, client, ctx)
1313
enriched_ctx.client_id = client.id
1414
fn(command, ctx)
1515
else
16-
M.execute_command(command)
16+
local opts = require("rust-tools").config.options.tools
17+
opts.executor.execute_command(command)
1718
end
1819
end
1920
end
@@ -60,9 +61,71 @@ local function on_user_choice(action, ctx)
6061
end
6162
end
6263

63-
local select_code_action_results = function(results, ctx)
64+
function M.telescope_select(picker_opts, sorter_opts)
65+
picker_opts = picker_opts or require("telescope.themes").get_cursor() -- get_dropdown
66+
local conf = require("telescope.config").values
67+
68+
return function(items, opts, on_choice)
69+
opts = opts or {}
70+
local prompt_title = opts.prompt or "Rust Code Actions"
71+
72+
require("telescope.pickers")
73+
.new(picker_opts, {
74+
prompt_title = prompt_title,
75+
finder = require("telescope.finders").new_table({
76+
results = items, -- TODO:
77+
entry_maker = function(entry)
78+
local str = entry.title
79+
80+
return {
81+
value = entry,
82+
display = str,
83+
ordinal = str,
84+
}
85+
end,
86+
}),
87+
sorter = conf.generic_sorter(sorter_opts),
88+
previewer = require("telescope.previewers").new_buffer_previewer({
89+
define_preview = function(self, entry, status)
90+
local bufnr = self.state.bufnr
91+
local item = entry.value
92+
vim.api.nvim_buf_set_lines(
93+
bufnr,
94+
0,
95+
-1,
96+
false,
97+
M.code_action_submenu(item)
98+
)
99+
end,
100+
}),
101+
attach_mappings = function(prompt_bufnr, map)
102+
require("telescope.actions").select_default:replace(function()
103+
require("telescope.actions").close(prompt_bufnr)
104+
local selection =
105+
require("telescope.actions.state").get_selected_entry()
106+
on_choice(selection.value, selection.index)
107+
end)
108+
return true
109+
end,
110+
})
111+
:find()
112+
end
113+
end
114+
115+
M.code_action_submenu = function(item)
116+
if item.actions then
117+
vim.tbl_map(function(action)
118+
return "" .. action.title
119+
end, item.actions)
120+
else
121+
return {}
122+
end
123+
end
124+
125+
M.code_actions_results_to_nested_table = function(results)
64126
if results.error then
65127
vim.notify(results.error, vim.log.levels.ERROR)
128+
return
66129
end
67130

68131
local action_tuples = {}
@@ -96,28 +159,41 @@ local select_code_action_results = function(results, ctx)
96159
end
97160
end
98161

99-
vim.ui.select(entries, {
162+
return entries
163+
end
164+
165+
local select_code_action_results = function(results, ctx)
166+
local entries = M.code_actions_results_to_nested_table(results)
167+
if entries == nil then
168+
return
169+
end
170+
local opts = require("rust-tools").config.options.tools
171+
local select = opts.code_action_group.selector
172+
173+
select(entries, {
100174
prompt = "Code Actions:",
101175
format_item = function(item)
102176
return item.title
103177
end,
178+
preview_item = function(item)
179+
return M.code_action_submenu(item)
180+
end,
104181
}, function(selected)
105182
if not selected then
106183
return
107184
end
108185

109186
if selected.actions then
110187
vim.schedule(function()
111-
vim.ui.select(selected.actions, {
112-
prompt = selected.title .. ": ",
188+
select(selected.actions, {
189+
prompt = selected.title,
113190
format_item = function(item)
114191
return item.title
115192
end,
116193
}, function(selected_group)
117194
if not selected_group then
118195
return
119196
end
120-
print(vim.inspect(selected_group))
121197

122198
vim.schedule(function()
123199
on_user_choice(selected_group, ctx)

lua/rust-tools/config.lua

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ local defaults = {
2121
-- automatically call RustReloadWorkspace when writing to a Cargo.toml file.
2222
reload_workspace_from_cargo_toml = true,
2323

24+
code_action_group = {
25+
-- Function with signature of `vim.ui.select` used to display the code_actions
26+
-- default: require("rust-tools.code_action_group").telescope_select(),
27+
selector = require("rust-tools.code_action_group").telescope_select(),
28+
-- selector = vim.ui.select,
29+
},
30+
2431
-- These apply to the default RustSetInlayHints command
2532
inlay_hints = {
2633
-- automatically set inlay hints (type hints)

0 commit comments

Comments
 (0)