Skip to content

Commit 8a3af11

Browse files
authored
fix: Use prompt selection when picking prompts (#210)
Instead of definining the selection when generating actions and hardcoding it, use prompt configuration for grabbing selection. Closes #206 Signed-off-by: Tomas Slusny <slusnucky@gmail.com>
1 parent 925b33a commit 8a3af11

5 files changed

Lines changed: 47 additions & 45 deletions

File tree

lua/CopilotChat/actions.lua

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
---@class CopilotChat.integrations.actions
22
---@field prompt string: The prompt to display
3-
---@field selection fun(source: CopilotChat.config.source):CopilotChat.config.selection?
4-
---@field actions table<string, string>: A table with the actions to pick from
3+
---@field actions table<string, CopilotChat.config.prompt>: A table with the actions to pick from
54

65
local select = require('CopilotChat.select')
76
local chat = require('CopilotChat')
@@ -11,22 +10,26 @@ local M = {}
1110
--- Diagnostic help actions
1211
---@return CopilotChat.integrations.actions?: The help actions
1312
function M.help_actions()
14-
local diagnostic = select.diagnostics({
15-
bufnr = vim.api.nvim_get_current_buf(),
16-
winnr = vim.api.nvim_get_current_win(),
17-
})
18-
if not diagnostic then
19-
return
13+
local bufnr = vim.api.nvim_get_current_buf()
14+
local winnr = vim.api.nvim_get_current_win()
15+
local cursor = vim.api.nvim_win_get_cursor(winnr)
16+
local line_diagnostics = vim.lsp.diagnostic.get_line_diagnostics(bufnr, cursor[1] - 1)
17+
18+
if #line_diagnostics == 0 then
19+
return nil
2020
end
2121

2222
return {
2323
prompt = 'Copilot Chat Help Actions',
24-
selection = select.buffer,
2524
actions = {
26-
['Fix diagnostic'] = 'Please assist with fixing the following diagnostic issue in file: "'
27-
.. diagnostic.prompt_extra,
28-
['Explain diagnostic'] = 'Please explain the following diagnostic issue in file: "'
29-
.. diagnostic.prompt_extra,
25+
['Fix diagnostic'] = {
26+
prompt = 'Please assist with fixing the following diagnostic issue in file: "',
27+
selection = select.diagnostics,
28+
},
29+
['Explain diagnostic'] = {
30+
prompt = 'Please explain the following diagnostic issue in file: "',
31+
selection = select.diagnostics,
32+
},
3033
},
3134
}
3235
end
@@ -36,28 +39,22 @@ end
3639
function M.prompt_actions()
3740
local actions = {}
3841
for name, prompt in pairs(chat.prompts(true)) do
39-
actions[name] = prompt.prompt
42+
actions[name] = prompt
4043
end
4144
return {
4245
prompt = 'Copilot Chat Prompt Actions',
43-
selection = select.visual,
4446
actions = actions,
4547
}
4648
end
4749

4850
--- Pick an action from a list of actions
4951
---@param pick_actions CopilotChat.integrations.actions?: A table with the actions to pick from
50-
---@param config CopilotChat.config?: The chat configuration
5152
---@param opts table?: vim.ui.select options
52-
function M.pick(pick_actions, config, opts)
53+
function M.pick(pick_actions, opts)
5354
if not pick_actions or not pick_actions.actions or vim.tbl_isempty(pick_actions.actions) then
5455
return
5556
end
5657

57-
config = vim.tbl_extend('force', {
58-
selection = pick_actions.selection,
59-
}, config or {})
60-
6158
opts = vim.tbl_extend('force', {
6259
prompt = pick_actions.prompt .. '> ',
6360
}, opts or {})
@@ -67,7 +64,7 @@ function M.pick(pick_actions, config, opts)
6764
return
6865
end
6966
vim.defer_fn(function()
70-
chat.ask(pick_actions.actions[selected], config)
67+
chat.ask(pick_actions.actions[selected].prompt, pick_actions.actions[selected])
7168
end, 100)
7269
end)
7370
end

lua/CopilotChat/config.lua

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@ local select = require('CopilotChat.select')
1717

1818
---@class CopilotChat.config.prompt
1919
---@field prompt string?
20-
---@field selection nil|fun(source: CopilotChat.config.source):CopilotChat.config.selection?
21-
---@field mapping string?
2220
---@field description string?
21+
---@field kind string?
22+
---@field mapping string?
23+
---@field system_prompt string?
24+
---@field callback fun(response: string)?
25+
---@field selection nil|fun(source: CopilotChat.config.source):CopilotChat.config.selection?
2326

2427
---@class CopilotChat.config.window
2528
---@field layout string?

lua/CopilotChat/init.lua

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ local function update_prompts(prompt, system_prompt)
9898
if found then
9999
if found.kind == 'user' then
100100
local out = found.prompt
101-
if string.match(out, [[/[%w_]+]]) then
101+
if out and string.match(out, [[/[%w_]+]]) then
102102
try_again = true
103103
end
104104
return out
@@ -195,6 +195,7 @@ end
195195

196196
--- Get the prompts to use.
197197
---@param skip_system boolean|nil
198+
---@return table<string, CopilotChat.config.prompt>
198199
function M.prompts(skip_system)
199200
local function get_prompt_kind(name)
200201
return vim.startswith(name, 'COPILOT_') and 'system' or 'user'
@@ -229,7 +230,7 @@ function M.prompts(skip_system)
229230
end
230231

231232
--- Open the chat window.
232-
---@param config CopilotChat.config?
233+
---@param config CopilotChat.config|CopilotChat.config.prompt|nil
233234
---@param source CopilotChat.config.source?
234235
function M.open(config, source, no_focus)
235236
local should_reset = config and config.window ~= nil and not vim.tbl_isempty(config.window)
@@ -285,7 +286,7 @@ end
285286

286287
--- Ask a question to the Copilot model.
287288
---@param prompt string
288-
---@param config CopilotChat.config|nil
289+
---@param config CopilotChat.config|CopilotChat.config.prompt|nil
289290
---@param source CopilotChat.config.source?
290291
function M.ask(prompt, config, source)
291292
M.open(config, source, true)
@@ -426,7 +427,9 @@ function M.save(name, history_path)
426427
end
427428

428429
history_path = history_path or M.config.history_path
429-
state.copilot:save(name, history_path)
430+
if history_path then
431+
state.copilot:save(name, history_path)
432+
end
430433
end
431434

432435
--- Load the chat history from a file.
@@ -440,6 +443,10 @@ function M.load(name, history_path)
440443
end
441444

442445
history_path = history_path or M.config.history_path
446+
if not history_path then
447+
return
448+
end
449+
443450
state.copilot:reset()
444451
state.chat:clear()
445452

@@ -720,7 +727,9 @@ function M.setup(config)
720727
if args.args and vim.trim(args.args) ~= '' then
721728
input = input .. ' ' .. args.args
722729
end
723-
M.ask(input, prompt)
730+
if input then
731+
M.ask(input, prompt)
732+
end
724733
end, {
725734
nargs = '*',
726735
force = true,

lua/CopilotChat/integrations/fzflua.lua

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,24 @@ local M = {}
55

66
--- Pick an action from a list of actions
77
---@param pick_actions CopilotChat.integrations.actions?: A table with the actions to pick from
8-
---@param config CopilotChat.config?: The chat configuration
98
---@param opts table?: fzf-lua options
10-
function M.pick(pick_actions, config, opts)
9+
function M.pick(pick_actions, opts)
1110
if not pick_actions or not pick_actions.actions or vim.tbl_isempty(pick_actions.actions) then
1211
return
1312
end
1413

15-
config = vim.tbl_extend('force', {
16-
selection = pick_actions.selection,
17-
}, config or {})
18-
1914
opts = vim.tbl_extend('force', {
2015
prompt = pick_actions.prompt .. '> ',
2116
preview = fzflua.shell.raw_preview_action_cmd(function(items)
22-
return string.format('echo "%s"', pick_actions.actions[items[1]])
17+
return string.format('echo "%s"', pick_actions.actions[items[1]].prompt)
2318
end),
2419
actions = {
2520
['default'] = function(selected)
2621
if not selected or vim.tbl_isempty(selected) then
2722
return
2823
end
2924
vim.defer_fn(function()
30-
chat.ask(pick_actions.actions[selected[1]], config)
25+
chat.ask(pick_actions.actions[selected[1]].prompt, pick_actions.actions[selected[1]])
3126
end, 100)
3227
end,
3328
},

lua/CopilotChat/integrations/telescope.lua

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,12 @@ local M = {}
1111

1212
--- Pick an action from a list of actions
1313
---@param pick_actions CopilotChat.integrations.actions?: A table with the actions to pick from
14-
---@param config CopilotChat.config?: The chat configuration
1514
---@param opts table?: Telescope options
16-
function M.pick(pick_actions, config, opts)
15+
function M.pick(pick_actions, opts)
1716
if not pick_actions or not pick_actions.actions or vim.tbl_isempty(pick_actions.actions) then
1817
return
1918
end
2019

21-
config = vim.tbl_extend('force', {
22-
selection = pick_actions.selection,
23-
}, config or {})
24-
2520
opts = themes.get_dropdown(opts or {})
2621
pickers
2722
.new(opts, {
@@ -37,7 +32,7 @@ function M.pick(pick_actions, config, opts)
3732
0,
3833
-1,
3934
false,
40-
{ pick_actions.actions[entry[1]] }
35+
{ pick_actions.actions[entry[1]].prompt }
4136
)
4237
end,
4338
}),
@@ -49,7 +44,10 @@ function M.pick(pick_actions, config, opts)
4944
if not selected or vim.tbl_isempty(selected) then
5045
return
5146
end
52-
chat.ask(pick_actions.actions[selected[1]], config)
47+
48+
vim.defer_fn(function()
49+
chat.ask(pick_actions.actions[selected[1]].prompt, pick_actions.actions[selected[1]])
50+
end, 100)
5351
end)
5452
return true
5553
end,

0 commit comments

Comments
 (0)