forked from CopilotC-Nvim/CopilotChat.nvim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode_actions.lua
More file actions
181 lines (158 loc) · 4.73 KB
/
code_actions.lua
File metadata and controls
181 lines (158 loc) · 4.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
local actions = require('telescope.actions')
local action_state = require('telescope.actions.state')
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 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
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
end
local function generate_explain_diagnostic_prompt()
local diagnostic = utils.get_diagnostics()
if diagnostic == 'No diagnostics available' then
return diagnostic
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
end
--- 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 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
end
vim.cmd(prefix .. value)
end)
return true
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 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
end
vim.cmd(prefix .. value)
end)
return true
end
end
local function show_help_actions()
help_actions = {
{
label = generate_fix_diagnostic_prompt(),
name = 'Fix diagnostic',
},
{
label = generate_explain_diagnostic_prompt(),
name = 'Explain diagnostic',
},
}
-- Filter all no diagnostics available actions
help_actions = vim.tbl_filter(function(value)
return value.label ~= 'No diagnostics available'
end, help_actions)
-- Show the menu with telescope pickers
local opts = themes.get_dropdown({})
local action_names = {}
for _, value in pairs(help_actions) do
table.insert(action_names, value.name)
end
telescope_pickers
.new(opts, {
prompt_title = 'Copilot Chat Help Actions',
finder = finders.new_table({
results = action_names,
}),
sorter = conf.generic_sorter(opts),
attach_mappings = diagnostic_help_command('CopilotChat'),
})
:find()
end
--- Show prompt actions
---@param is_in_visual_mode boolean?
local function show_prompt_actions(is_in_visual_mode)
-- 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"
end
-- Show the menu with telescope pickers
local opts = themes.get_dropdown({})
local action_names = {}
for _, value in pairs(user_prompt_actions) do
table.insert(action_names, value.name)
end
telescope_pickers
.new(opts, {
prompt_title = 'Copilot Chat Actions',
finder = finders.new_table({
results = action_names,
}),
sorter = conf.generic_sorter(opts),
attach_mappings = generate_prompt_command(cmd),
})
:find()
end
return {
show_help_actions = show_help_actions,
show_prompt_actions = show_prompt_actions,
}