Skip to content

Commit

Permalink
feat: add prompt actions support in Telescope integration
Browse files Browse the repository at this point in the history
  • Loading branch information
jellydn committed Feb 17, 2024
1 parent 4e7d929 commit f124645
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 4 deletions.
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -236,13 +236,23 @@ To integrate CopilotChat with Telescope, you can add the following configuration
end,
desc = "CopilotChat - Help actions",
},
-- Show prompts actions with telescope
{
"<leader>ccp",
function()
require("CopilotChat.code_actions").show_prompt_actions()
end,
desc = "CopilotChat - Help actions",
},
}
}
```

In this configuration, the `CopilotChat.code_actions.show_help_actions()` function is mapped to the `<leader>cch` key combination. When you press these keys, Telescope will display a list of help actions provided by CopilotChat as below.
1. Select help actions base the diagnostic message under the cursor.
[![Help action with Copilot Chat](https://i.gyazo.com/146dc35368592ba9f5de047ddc4728ad.gif)](https://gyazo.com/146dc35368592ba9f5de047ddc4728ad)

[![Help action with Copilot Chat](https://i.gyazo.com/146dc35368592ba9f5de047ddc4728ad.gif)](https://gyazo.com/146dc35368592ba9f5de047ddc4728ad)
2. Select action base on user prompts.
[![Select action base on user prompts](https://i.gyazo.com/a9c41e6398591c2f1d1d872fd58a2c63.gif)](https://gyazo.com/a9c41e6398591c2f1d1d872fd58a2c63)

### Integration with `edgy.nvim`

Expand Down
67 changes: 65 additions & 2 deletions lua/CopilotChat/code_actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ local conf = require('telescope.config').values
local utils = require('CopilotChat.utils')

local help_actions = {}
local prompt_actions = {}

local function fix_diagnostic()
local diagnostic = utils.get_diagnostics()
Expand All @@ -32,7 +33,11 @@ local function explain_diagnostic()
.. diagnostic
end

local function nvim_command(prefix)
--- Help command for telescope picker
--- This will copy all the lines in the buffer to the unnamed register
--- Then will send the diagnostic to copilot chat
---@param prefix string
local function help_command(prefix)
if prefix == nil then
prefix = ''
else
Expand Down Expand Up @@ -62,6 +67,37 @@ local function nvim_command(prefix)
end
end

--- Prompt command for telescope picker
--- This will show all the user prompts in the telescope picker
--- Then will execute the command selected by the user
---@param prefix string
local function prompt_command(prefix)
if prefix == nil then
prefix = ''
else
prefix = prefix .. ' '
end

return function(prompt_bufnr, _)
actions.select_default:replace(function()
actions.close(prompt_bufnr)
local selection = action_state.get_selected_entry()

-- Get value from the prompt_actions and execute the command
local value = ''
for _, action in pairs(prompt_actions) do
if action.name == selection[1] then
value = action.label
break
end
end

vim.cmd(prefix .. value)
end)
return true
end
end

local function show_help_actions()
help_actions = {
{
Expand Down Expand Up @@ -92,11 +128,38 @@ local function show_help_actions()
results = picker_names,
}),
sorter = conf.generic_sorter(opts),
attach_mappings = nvim_command('CopilotChat'),
attach_mappings = help_command('CopilotChat'),
})
:find()
end

local function show_prompt_actions()
-- Convert user prompts to a table of actions
prompt_actions = {}

for key, prompt in pairs(vim.g.copilot_chat_user_prompts) do
table.insert(prompt_actions, { name = key, label = prompt })
end

-- Show the menu with telescope pickers
local opts = themes.get_dropdown({})
local picker_names = {}
for _, value in pairs(prompt_actions) do
table.insert(picker_names, value.name)
end
telescope_pickers
.new(opts, {
prompt_title = 'Copilot Chat Actions',
finder = finders.new_table({
results = picker_names,
}),
sorter = conf.generic_sorter(opts),
attach_mappings = prompt_command('CopilotChat'),
})
:find()
end

return {
show_help_actions = show_help_actions,
show_prompt_actions = show_prompt_actions,
}

0 comments on commit f124645

Please sign in to comment.