-
-
Notifications
You must be signed in to change notification settings - Fork 170
Expand file tree
/
Copy pathmappings.lua
More file actions
470 lines (405 loc) · 14.1 KB
/
mappings.lua
File metadata and controls
470 lines (405 loc) · 14.1 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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
local async = require('plenary.async')
local copilot = require('CopilotChat')
local utils = require('CopilotChat.utils')
---@param chat CopilotChat.ui.Chat
---@return CopilotChat.ui.Diff.Diff?
local function get_diff(chat)
local config = chat.config
local block = chat:get_closest_block()
-- If no block found, return nil
if not block then
return nil
end
-- Initialize variables with selection if available
local header = block.header
local selection = copilot.get_selection(config)
local reference = selection and selection.content
local start_line = selection and selection.start_line
local end_line = selection and selection.end_line
local filename = selection and selection.filename
local filetype = selection and selection.filetype
local bufnr = selection and selection.bufnr
-- If we have header info, use it as source of truth
if header.start_line and header.end_line then
-- Try to find matching buffer and window
bufnr = nil
for _, win in ipairs(vim.api.nvim_list_wins()) do
local win_buf = vim.api.nvim_win_get_buf(win)
if utils.filename_same(vim.api.nvim_buf_get_name(win_buf), header.filename) then
bufnr = win_buf
break
end
end
filename = header.filename
filetype = header.filetype or vim.filetype.match({ filename = filename })
start_line = header.start_line
end_line = header.end_line
-- If we found a valid buffer, get the reference content
if bufnr and utils.buf_valid(bufnr) then
reference =
table.concat(vim.api.nvim_buf_get_lines(bufnr, start_line - 1, end_line, false), '\n')
filetype = vim.bo[bufnr].filetype
end
end
-- If we are missing info, there is no diff to be made
if not start_line or not end_line or not filename then
return nil
end
return {
change = block.content,
reference = reference or '',
filetype = filetype or '',
filename = filename,
start_line = start_line,
end_line = end_line,
bufnr = bufnr,
}
end
---@param winnr number
---@param bufnr number
---@param start_line number
---@param end_line number
---@param config CopilotChat.config.shared
local function jump_to_diff(winnr, bufnr, start_line, end_line, config)
pcall(vim.api.nvim_buf_set_mark, bufnr, '<', start_line, 0, {})
pcall(vim.api.nvim_buf_set_mark, bufnr, '>', end_line, 0, {})
pcall(vim.api.nvim_buf_set_mark, bufnr, '[', start_line, 0, {})
pcall(vim.api.nvim_buf_set_mark, bufnr, ']', end_line, 0, {})
pcall(vim.api.nvim_win_set_cursor, winnr, { start_line, 0 })
copilot.update_selection(config)
end
---@param diff CopilotChat.ui.Diff.Diff?
---@param config CopilotChat.config.shared
local function apply_diff(diff, config)
if not diff or not diff.bufnr then
return
end
local winnr = vim.fn.win_findbuf(diff.bufnr)[1]
if not winnr then
return
end
local lines = vim.split(diff.change, '\n', { trimempty = false })
vim.api.nvim_buf_set_lines(diff.bufnr, diff.start_line - 1, diff.end_line, false, lines)
jump_to_diff(winnr, diff.bufnr, diff.start_line, diff.start_line + #lines - 1, config)
end
---@class CopilotChat.config.mapping
---@field normal string?
---@field insert string?
---@field detail string?
---@field callback fun(overlay: CopilotChat.ui.Overlay, diff: CopilotChat.ui.Diff, chat: CopilotChat.ui.Chat, source: CopilotChat.source)
---@class CopilotChat.config.mapping.yank_diff : CopilotChat.config.mapping
---@field register string?
---@class CopilotChat.config.mapping.show_diff : CopilotChat.config.mapping
---@field full_diff boolean?
---@class CopilotChat.config.mappings
---@field complete CopilotChat.config.mapping?
---@field close CopilotChat.config.mapping?
---@field reset CopilotChat.config.mapping?
---@field submit_prompt CopilotChat.config.mapping?
---@field toggle_sticky CopilotChat.config.mapping?
---@field accept_diff CopilotChat.config.mapping?
---@field jump_to_diff CopilotChat.config.mapping?
---@field quickfix_diffs CopilotChat.config.mapping?
---@field yank_diff CopilotChat.config.mapping.yank_diff?
---@field show_diff CopilotChat.config.mapping.show_diff?
---@field show_info CopilotChat.config.mapping?
---@field show_context CopilotChat.config.mapping?
---@field show_help CopilotChat.config.mapping?
return {
complete = {
insert = '<Tab>',
callback = function(overlay, diff, chat)
copilot.trigger_complete(true)
end,
},
close = {
normal = 'q',
insert = '<C-c>',
callback = function(overlay, diff, chat)
copilot.close()
end,
},
reset = {
normal = '<C-l>',
insert = '<C-l>',
callback = function(overlay, diff, chat)
copilot.reset()
end,
},
submit_prompt = {
normal = '<CR>',
insert = '<C-s>',
callback = function(overlay, diff, chat)
local section = chat:get_closest_section()
if not section or section.answer then
return
end
copilot.ask(section.content)
end,
},
toggle_sticky = {
detail = 'Makes line under cursor sticky or deletes sticky line.',
normal = 'gr',
callback = function(overlay, diff, chat)
local section = chat:get_closest_section()
if not section or section.answer then
return
end
local current_line = vim.trim(vim.api.nvim_get_current_line())
if current_line == '' then
return
end
local cursor = vim.api.nvim_win_get_cursor(0)
local cur_line = cursor[1]
vim.api.nvim_buf_set_lines(chat.bufnr, cur_line - 1, cur_line, false, {})
if vim.startswith(current_line, '> ') then
return
end
local lines = vim.split(section.content, '\n')
local insert_line = 1
local first_one = true
for i = insert_line, #lines do
local line = lines[i]
if line and vim.trim(line) ~= '' then
if vim.startswith(line, '> ') then
first_one = false
else
break
end
elseif i >= 2 then
break
end
insert_line = insert_line + 1
end
insert_line = section.start_line + insert_line - 1
local to_insert = first_one and { '> ' .. current_line, '' } or { '> ' .. current_line }
vim.api.nvim_buf_set_lines(chat.bufnr, insert_line - 1, insert_line - 1, false, to_insert)
vim.api.nvim_win_set_cursor(0, cursor)
end,
},
accept_diff = {
normal = '<C-y>',
insert = '<C-y>',
callback = function(overlay, diff, chat, source)
apply_diff(get_diff(chat), chat.config)
end,
},
jump_to_diff = {
normal = 'gj',
callback = function(overlay, diff, chat, source)
if not source or not source.winnr or not vim.api.nvim_win_is_valid(source.winnr) then
return
end
local diff = get_diff(chat)
if not diff then
return
end
local diff_bufnr = diff.bufnr
-- If buffer is not found, try to load it
if not diff_bufnr then
diff_bufnr = vim.fn.bufadd(diff.filename)
vim.fn.bufload(diff_bufnr)
end
source.bufnr = diff_bufnr
vim.api.nvim_win_set_buf(source.winnr, diff_bufnr)
jump_to_diff(source.winnr, diff_bufnr, diff.start_line, diff.end_line, chat.config)
end,
},
quickfix_answers = {
normal = 'gqa',
callback = function(overlay, diff, chat)
local items = {}
for i, section in ipairs(chat.sections) do
if section.answer then
local prev_section = chat.sections[i - 1]
local text = ''
if prev_section then
text = vim.trim(
table.concat(
vim.api.nvim_buf_get_lines(
chat.bufnr,
prev_section.start_line - 1,
prev_section.end_line,
false
),
' '
)
)
end
table.insert(items, {
bufnr = chat.bufnr,
lnum = section.start_line,
end_lnum = section.end_line,
text = text,
})
end
end
vim.fn.setqflist(items)
vim.cmd('copen')
end,
},
quickfix_diffs = {
normal = 'gqd',
callback = function(overlay, diff, chat)
local selection = copilot.get_selection(chat.config)
local items = {}
for _, section in ipairs(chat.sections) do
for _, block in ipairs(section.blocks) do
local header = block.header
if not header.start_line and selection then
header.filename = selection.filename .. ' (selection)'
header.start_line = selection.start_line
header.end_line = selection.end_line
end
local text = string.format('%s (%s)', header.filename, header.filetype)
if header.start_line and header.end_line then
text = text .. string.format(' [lines %d-%d]', header.start_line, header.end_line)
end
table.insert(items, {
bufnr = chat.bufnr,
lnum = block.start_line,
end_lnum = block.end_line,
text = text,
})
end
end
vim.fn.setqflist(items)
vim.cmd('copen')
end,
},
yank_diff = {
normal = 'gy',
register = '"', -- Default register to use for yanking
callback = function(overlay, diff, chat)
local block = chat:get_closest_block()
if not block then
return
end
vim.fn.setreg(copilot.config.mappings.yank_diff.register, block.content)
end,
},
show_diff = {
normal = 'gd',
full_diff = false, -- Show full diff instead of unified diff when showing diff window
callback = function(overlay, diff, chat)
local content = get_diff(chat)
if not content then
return
end
diff:show(content, chat.winnr, copilot.config.mappings.show_diff.full_diff)
end,
},
show_info = {
normal = 'gi',
callback = function(overlay, diff, chat)
local section = chat:get_closest_section()
if not section or section.answer then
return
end
local lines = {}
local prompt, config = copilot.resolve_prompts(section.content, chat.config)
local system_prompt = config.system_prompt
async.run(function()
local _, selected_agent = pcall(copilot.resolve_agent, prompt, config)
local _, selected_model = pcall(copilot.resolve_model, prompt, config)
async.util.scheduler()
table.insert(lines, '**Logs**: `' .. chat.config.log_path .. '`')
table.insert(lines, '**History**: `' .. chat.config.history_path .. '`')
table.insert(lines, '**Temp Files**: `' .. vim.fn.fnamemodify(os.tmpname(), ':h') .. '`')
table.insert(lines, '')
if selected_model then
table.insert(lines, '**Model**: `' .. selected_model .. '`')
table.insert(lines, '')
end
if selected_agent then
table.insert(lines, '**Agent**: `' .. selected_agent .. '`')
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
overlay:show(vim.trim(table.concat(lines, '\n')) .. '\n', chat.winnr, 'markdown')
end)
end,
},
show_context = {
normal = 'gc',
callback = function(overlay, diff, chat)
local section = chat:get_closest_section()
if not section or section.answer then
return
end
local lines = {}
local selection = copilot.get_selection(chat.config)
if selection then
table.insert(lines, '**Selection**')
table.insert(lines, '```' .. selection.filetype)
for _, line in ipairs(vim.split(selection.content, '\n')) do
table.insert(lines, line)
end
table.insert(lines, '```')
table.insert(lines, '')
end
async.run(function()
local embeddings = {}
if section and not section.answer then
local _, selected_model = pcall(copilot.resolve_model, section.content, chat.config)
embeddings = copilot.resolve_embeddings(section.content, selected_model, chat.config)
end
for _, embedding in ipairs(embeddings) do
local embed_lines = vim.split(embedding.content, '\n')
local preview = vim.list_slice(embed_lines, 1, math.min(10, #embed_lines))
local header = string.format('**%s** (%s lines)', embedding.filename, #embed_lines)
if #embed_lines > 10 then
header = header .. ' (truncated)'
end
table.insert(lines, header)
table.insert(lines, '```' .. embedding.filetype)
for _, line in ipairs(preview) do
table.insert(lines, line)
end
table.insert(lines, '```')
table.insert(lines, '')
end
async.util.scheduler()
overlay:show(vim.trim(table.concat(lines, '\n')) .. '\n', chat.winnr, 'markdown')
end)
end,
},
show_help = {
normal = 'gh',
callback = function(overlay, diff, chat)
local chat_help = '**`Special tokens`**\n'
chat_help = chat_help .. '`@<agent>` to select an agent\n'
chat_help = chat_help .. '`#<context>` to select a context\n'
chat_help = chat_help .. '`#<context>:<input>` to select input for context\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(copilot.config.mappings)
table.sort(chat_keys, function(a, b)
a = copilot.config.mappings[a]
a = a.normal or a.insert
b = copilot.config.mappings[b]
b = b.normal or b.insert
return a < b
end)
for _, name in ipairs(chat_keys) do
if name ~= 'close' then
local info = utils.key_to_info(name, copilot.config.mappings[name], '`')
if info ~= '' then
chat_help = chat_help .. info .. '\n'
end
end
end
overlay:show(chat_help, chat.winnr, 'markdown')
end,
},
}