-
-
Notifications
You must be signed in to change notification settings - Fork 170
Expand file tree
/
Copy pathmappings.lua
More file actions
401 lines (348 loc) · 11.9 KB
/
mappings.lua
File metadata and controls
401 lines (348 loc) · 11.9 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
local async = require('plenary.async')
local client = require('CopilotChat.client')
local constants = require('CopilotChat.constants')
local select = require('CopilotChat.select')
local utils = require('CopilotChat.utils')
local files = require('CopilotChat.utils.files')
--- Prepare a buffer for applying a diff
---@param filename string?
---@param source CopilotChat.ui.chat.Source
---@return integer
local function prepare_diff_buffer(filename, source)
if not filename then
filename = vim.api.nvim_buf_get_name(source.bufnr)
end
-- Try to find matching buffer first
local diff_bufnr = nil
for _, buf in ipairs(vim.api.nvim_list_bufs()) do
if files.filename_same(vim.api.nvim_buf_get_name(buf), filename) then
diff_bufnr = buf
break
end
end
-- If not found, create a new buffer
if not diff_bufnr then
diff_bufnr = vim.fn.bufadd(filename)
vim.fn.bufload(diff_bufnr)
end
-- If source exists, update it to point to the diff buffer
if source and source.winnr and vim.api.nvim_win_is_valid(source.winnr) then
source.bufnr = diff_bufnr
vim.api.nvim_win_set_buf(source.winnr, diff_bufnr)
end
return diff_bufnr
end
---@class CopilotChat.config.mapping
---@field normal string?
---@field insert string?
---@field callback fun(source: CopilotChat.ui.chat.Source)
---@class CopilotChat.config.mapping.yank_diff : CopilotChat.config.mapping
---@field register string?
---@class CopilotChat.config.mappings
---@field complete CopilotChat.config.mapping|false|nil
---@field close CopilotChat.config.mapping|false|nil
---@field reset CopilotChat.config.mapping|false|nil
---@field submit_prompt CopilotChat.config.mapping|false|nil
---@field accept_diff CopilotChat.config.mapping|false|nil
---@field jump_to_diff CopilotChat.config.mapping|false|nil
---@field quickfix_diffs CopilotChat.config.mapping|false|nil
---@field quickfix_answers CopilotChat.config.mapping|false|nil
---@field yank_diff CopilotChat.config.mapping.yank_diff|false|nil
---@field show_diff CopilotChat.config.mapping|false|nil
---@field show_info CopilotChat.config.mapping|false|nil
---@field show_help CopilotChat.config.mapping|false|nil
return {
complete = {
insert = '<Tab>',
callback = function()
require('CopilotChat.completion').complete()
end,
},
close = {
normal = 'q',
insert = '<C-c>',
callback = function()
require('CopilotChat').close()
end,
},
reset = {
normal = '<C-l>',
insert = '<C-l>',
callback = function()
require('CopilotChat').reset()
end,
},
submit_prompt = {
normal = '<CR>',
insert = '<C-s>',
callback = function()
local copilot = require('CopilotChat')
local message = copilot.chat:get_message(constants.ROLE.USER, true)
if not message then
return
end
copilot.ask(message.content)
end,
},
accept_diff = {
normal = '<C-y>',
insert = '<C-y>',
callback = function(source)
local chat = require('CopilotChat').chat
local diff = require('CopilotChat.utils.diff')
local block = chat:get_block(constants.ROLE.ASSISTANT, true)
if not block then
return
end
local path = block.header.filename
local bufnr = prepare_diff_buffer(path, source)
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
local new_lines = diff.apply_diff(block, lines)
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, new_lines)
local first, last = diff.get_diff_region(block, lines)
if first and last then
select.set(bufnr, source.winnr, first, last)
select.highlight(bufnr)
end
end,
},
jump_to_diff = {
normal = 'gj',
callback = function(source)
local chat = require('CopilotChat').chat
local diff = require('CopilotChat.utils.diff')
local block = chat:get_block(constants.ROLE.ASSISTANT, true)
if not block then
return
end
local path = block.header.filename
local bufnr = prepare_diff_buffer(path, source)
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
local first, last = diff.get_diff_region(block, lines)
if first and last and bufnr then
select.set(bufnr, source.winnr, first, last)
select.highlight(bufnr)
end
end,
},
yank_diff = {
normal = 'gy',
register = '"', -- Default register to use for yanking
callback = function()
local config = require('CopilotChat.config')
local chat = require('CopilotChat').chat
local block = chat:get_block(constants.ROLE.ASSISTANT, true)
if not block then
return
end
vim.fn.setreg(config.mappings.yank_diff.register, block.content)
end,
},
show_diff = {
normal = 'gd',
callback = function(source)
local chat = require('CopilotChat').chat
local diff = require('CopilotChat.utils.diff')
local block = chat:get_block(constants.ROLE.ASSISTANT, true)
if not block then
return
end
local path = block.header.filename
local bufnr = prepare_diff_buffer(path, source)
-- Collect all blocks for the same filename
local message = chat:get_message(constants.ROLE.ASSISTANT, true)
local blocks = {}
if message and message.section and message.section.blocks then
for _, b in ipairs(message.section.blocks) do
if b.header.filename == path then
table.insert(blocks, b)
end
end
else
blocks = { block }
end
-- Apply all diffs for the filename
local new_lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
for i = #blocks, 1, -1 do
new_lines = diff.apply_diff(blocks[i], new_lines)
end
local opts = {
filetype = vim.bo[bufnr].filetype,
text = table.concat(new_lines, '\n'),
}
opts.on_show = function()
vim.api.nvim_win_call(source.winnr, function()
vim.cmd('diffthis')
end)
vim.api.nvim_win_call(chat.winnr, function()
vim.cmd('diffthis')
end)
end
opts.on_hide = function()
vim.api.nvim_win_call(chat.winnr, function()
vim.cmd('diffoff')
end)
end
chat:overlay(opts)
end,
},
quickfix_diffs = {
normal = 'gqd',
callback = function()
local chat = require('CopilotChat').chat
local items = {}
local messages = chat:get_messages()
for _, message in ipairs(messages) do
if message.section then
for _, block in ipairs(message.section.blocks) do
local text = string.format('%s (%s)', block.header.filename, block.header.filetype)
if block.header.start_line and block.header.end_line then
text = text .. string.format(' [lines %d-%d]', block.header.start_line, block.header.end_line)
end
table.insert(items, {
bufnr = chat.bufnr,
lnum = block.start_line,
end_lnum = block.end_line,
text = text,
})
end
end
end
vim.fn.setqflist(items)
vim.cmd('copen')
end,
},
quickfix_answers = {
normal = 'gqa',
callback = function()
local chat = require('CopilotChat').chat
local items = {}
local messages = chat:get_messages()
for i, message in ipairs(messages) do
if message.section and message.role == constants.ROLE.ASSISTANT then
local prev_message = messages[i - 1]
local text = ''
if prev_message then
text = prev_message.content
end
table.insert(items, {
bufnr = chat.bufnr,
lnum = message.section.start_line,
end_lnum = message.section.end_line,
text = text,
})
end
end
vim.fn.setqflist(items)
vim.cmd('copen')
end,
},
show_info = {
normal = 'gc',
callback = function(source)
local chat = require('CopilotChat').chat
local prompts = require('CopilotChat.prompts')
local message = chat:get_message(constants.ROLE.USER, true)
if not message then
return
end
local lines = {}
async.run(function()
local config, prompt = prompts.resolve_prompt(message.content)
local system_prompt = config.system_prompt
local selected_tools = prompts.resolve_tools(prompt, config)
local selected_model = prompts.resolve_model(prompt, config)
local infos = client:info()
selected_tools = vim.tbl_map(function(tool)
return tool.name
end, selected_tools)
utils.schedule_main()
table.insert(lines, '**Logs**: `' .. config.log_path .. '`')
table.insert(lines, '**History**: `' .. config.history_path .. '`')
table.insert(lines, '')
for provider, infolines in pairs(infos) do
table.insert(lines, '**Provider**: `' .. provider .. '`')
for _, line in ipairs(infolines) do
table.insert(lines, line)
end
table.insert(lines, '')
end
if source and utils.buf_valid(source.bufnr) then
local source_name = vim.api.nvim_buf_get_name(source.bufnr)
table.insert(lines, '**Source**: `' .. source_name .. '`')
table.insert(lines, '')
end
if selected_model then
table.insert(lines, '**Model**: `' .. selected_model .. '`')
table.insert(lines, '')
end
if not utils.empty(selected_tools) then
table.insert(lines, '**Tools**')
table.insert(lines, '```')
table.insert(lines, table.concat(selected_tools, ', '))
table.insert(lines, '```')
table.insert(lines, '')
end
if system_prompt then
table.insert(lines, '**System Prompt**')
table.insert(lines, '````')
for _, line in ipairs(vim.split(vim.trim(system_prompt), '\n')) do
table.insert(lines, line)
end
table.insert(lines, '````')
table.insert(lines, '')
end
local selection = select.get(source.bufnr)
if selection then
table.insert(lines, '**Selection**')
table.insert(lines, '')
table.insert(
lines,
string.format('**%s** (%s-%s)', selection.filename, selection.start_line, selection.end_line)
)
table.insert(lines, string.format('````%s', selection.filetype))
for _, line in ipairs(vim.split(selection.content, '\n')) do
table.insert(lines, line)
end
table.insert(lines, '````')
table.insert(lines, '')
end
chat:overlay({
text = vim.trim(table.concat(lines, '\n')) .. '\n',
})
end)
end,
},
show_help = {
normal = 'gh',
callback = function()
local config = require('CopilotChat.config')
local chat = require('CopilotChat').chat
local chat_help = '**`Special tokens`**\n'
chat_help = chat_help .. '`@<function>` to share function\n'
chat_help = chat_help .. '`#<function>` to add resource\n'
chat_help = chat_help .. '`#<function>:<input>` to add resource with input\n'
chat_help = chat_help .. '`/<prompt>` to select a prompt\n'
chat_help = chat_help .. '`$<model>` to select a model\n'
chat_help = chat_help .. '`> <text>` to make a sticky prompt (copied to next prompt)\n'
chat_help = chat_help .. '\n**`Mappings`**\n'
local chat_keys = vim.tbl_keys(config.mappings)
table.sort(chat_keys, function(a, b)
a = config.mappings[a]
a = a and (a.normal or a.insert) or ''
b = config.mappings[b]
b = b and (b.normal or b.insert) or ''
return a < b
end)
for _, name in ipairs(chat_keys) do
local info = utils.key_to_info(name, config.mappings[name], '`')
if info ~= '' then
chat_help = chat_help .. info .. '\n'
end
end
chat:overlay({
text = chat_help,
})
end,
},
}