Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Refactor the plugin to be lua-based
Signed-off-by: Tomas Slusny <slusnucky@gmail.com>
  • Loading branch information
deathbeam committed Feb 27, 2024
commit bb23b615a6f6668411c2b08c595f72eef69fde1b
132 changes: 46 additions & 86 deletions lua/CopilotChat/code_actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,106 +4,73 @@ local telescope_pickers = require('telescope.pickers')
local finders = require('telescope.finders')
local themes = require('telescope.themes')
local conf = require('telescope.config').values
local utils = require('CopilotChat.utils')
local select = require('CopilotChat.select')
local chat = require('CopilotChat')

local help_actions = {}
local user_prompt_actions = {}

local function generate_fix_diagnostic_prompt()
local diagnostic = utils.get_diagnostics()
if diagnostic == 'No diagnostics available' then
return diagnostic
local diagnostic = select.diagnostics()
if not diagnostic then
return 'No diagnostics available'
end

local file_name = vim.fn.expand('%:t')
local line_number = vim.fn.line('.')
return 'Please assist with fixing the following diagnostic issue in file: "'
.. file_name
.. ':'
.. line_number
.. '". '
.. diagnostic
.. diagnostic.prompt_extra
end

local function generate_explain_diagnostic_prompt()
local diagnostic = utils.get_diagnostics()
if diagnostic == 'No diagnostics available' then
return diagnostic
local diagnostic = select.diagnostics()
if not diagnostic then
return 'No diagnostics available'
end

local file_name = vim.fn.expand('%:t')
local line_number = vim.fn.line('.')
return 'Please explain the following diagnostic issue in file: "'
.. file_name
.. ':'
.. line_number
.. '". '
.. diagnostic
return 'Please explain the following diagnostic issue in file: "' .. diagnostic.prompt_extra
end

--- Help command for telescope picker
--- This will copy all the lines in the buffer to the unnamed register
--- This will send whole buffer to copilot
--- Then will send the diagnostic to copilot chat
---@param prefix string
local function diagnostic_help_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()

-- Select all the lines in the buffer to uname register
vim.cmd('normal! ggVG"*y')

-- Get value from the help_actions and execute the command
local value = ''
for _, action in pairs(help_actions) do
if action.name == selection[1] then
value = action.label
break
end
local function diagnostic_help_command(prompt_bufnr, _)
actions.select_default:replace(function()
actions.close(prompt_bufnr)
local selection = action_state.get_selected_entry()

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

vim.cmd(prefix .. value)
end)
return true
end
chat.ask(value, { selection = select.buffer })
end)
return true
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 generate_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(user_prompt_actions) do
if action.name == selection[1] then
value = action.label
break
end
local function generate_prompt_command(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(user_prompt_actions) do
if action.name == selection[1] then
value = action.label
break
end
end

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

local function show_help_actions()
Expand Down Expand Up @@ -136,25 +103,18 @@ local function show_help_actions()
results = action_names,
}),
sorter = conf.generic_sorter(opts),
attach_mappings = diagnostic_help_command('CopilotChat'),
attach_mappings = diagnostic_help_command,
})
:find()
end

--- Show prompt actions
---@param is_in_visual_mode boolean?
local function show_prompt_actions(is_in_visual_mode)
local function show_prompt_actions()
-- Convert user prompts to a table of actions
user_prompt_actions = {}

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

local cmd = 'CopilotChat'

if is_in_visual_mode then
cmd = "'<,'>CopilotChatVisual"
for key, prompt in pairs(chat.get_prompts(true)) do
table.insert(user_prompt_actions, { name = key, label = prompt.prompt })
end

-- Show the menu with telescope pickers
Expand All @@ -170,7 +130,7 @@ local function show_prompt_actions(is_in_visual_mode)
results = action_names,
}),
sorter = conf.generic_sorter(opts),
attach_mappings = generate_prompt_command(cmd),
attach_mappings = generate_prompt_command,
})
:find()
end
Expand Down
Loading