-
-
Notifications
You must be signed in to change notification settings - Fork 170
Expand file tree
/
Copy pathfunctions.lua
More file actions
496 lines (435 loc) · 13.8 KB
/
functions.lua
File metadata and controls
496 lines (435 loc) · 13.8 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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
local resources = require('CopilotChat.resources')
local utils = require('CopilotChat.utils')
local files = require('CopilotChat.utils.files')
--- Get diagnostics for a buffer and format them as text
---@param bufnr number
---@param start_line number?
---@param end_line number?
---@return string
local function get_diagnostics_text(bufnr, start_line, end_line)
local diagnostics = vim.diagnostic.get(bufnr, {
severity = { min = vim.diagnostic.severity.HINT },
})
if #diagnostics == 0 then
return ''
end
local diag_lines = { '\n--- Diagnostics ---' }
for _, diag in ipairs(diagnostics) do
local diag_lnum = diag.lnum + 1
-- If range is specified, filter diagnostics within range
if not start_line or (diag_lnum >= start_line and diag_lnum <= end_line) then
local severity = vim.diagnostic.severity[diag.severity] or 'UNKNOWN'
local line_text = vim.api.nvim_buf_get_lines(bufnr, diag.lnum, diag.lnum + 1, false)[1] or ''
table.insert(
diag_lines,
string.format(
'%s line=%d-%d: %s\n > %s',
severity,
diag.lnum + 1,
diag.end_lnum and (diag.end_lnum + 1) or (diag.lnum + 1),
diag.message,
line_text
)
)
end
end
return #diag_lines > 1 and table.concat(diag_lines, '\n') or ''
end
---@class CopilotChat.config.functions.Function
---@field description string?
---@field schema table?
---@field group string?
---@field trusted boolean?
---@field uri string?
---@field resolve fun(input: table, source: CopilotChat.ui.chat.Source):CopilotChat.client.Resource[]
---@type table<string, CopilotChat.config.functions.Function>
return {
file = {
group = 'copilot',
uri = 'file://{path}',
description = 'Reads content from a specified file path, even if the file is not currently loaded as a buffer.',
schema = {
type = 'object',
required = { 'path' },
properties = {
path = {
type = 'string',
description = 'Path to file to include in chat context.',
enum = function(source)
return files.glob(source.cwd(), {
max_count = 0,
hidden = true,
})
end,
},
},
},
resolve = function(input)
utils.schedule_main()
local data, mimetype = resources.get_file(input.path)
if not data then
error('File not found: ' .. input.path)
end
return {
{
uri = 'file://' .. input.path,
name = input.path,
mimetype = mimetype,
data = data,
},
}
end,
},
url = {
group = 'copilot',
uri = 'https://{url}',
description = 'Fetches content from a specified URL. Useful for referencing documentation, examples, or other online resources.',
schema = {
type = 'object',
required = { 'url' },
properties = {
url = {
type = 'string',
description = 'URL to include in chat context.',
},
},
},
resolve = function(input)
if not input.url:match('^https?://') then
input.url = 'https://' .. input.url
end
utils.schedule_main()
local data, mimetype = resources.get_url(input.url)
if not data then
error('URL not found: ' .. input.url)
end
return {
{
uri = input.url,
mimetype = mimetype,
data = data,
},
}
end,
},
buffer = {
group = 'copilot',
uri = 'neovim://buffer/{scope}',
description = 'Retrieves content from buffer(s) with diagnostics. Scope can be a buffer number, filename, or one of: active, visible, listed, quickfix.',
schema = {
type = 'object',
required = { 'scope' },
properties = {
scope = {
type = 'string',
description = 'Buffer scope: active (current), visible (shown in windows), listed (all listed buffers), quickfix (buffers in quickfix list), or a specific buffer number/filename.',
enum = function()
local opts = {
{ display = 'active (current buffer)', value = 'active' },
{ display = 'visible (all visible buffers)', value = 'visible' },
{ display = 'listed (all listed buffers)', value = 'listed' },
{ display = 'quickfix (buffers in quickfix)', value = 'quickfix' },
}
for _, buf in ipairs(vim.api.nvim_list_bufs()) do
if utils.buf_valid(buf) and vim.fn.buflisted(buf) == 1 then
local name = vim.api.nvim_buf_get_name(buf)
if name and name ~= '' then
local display_name = vim.fn.fnamemodify(name, ':~:.')
table.insert(opts, { display = display_name, value = tostring(buf) })
end
end
end
return opts
end,
default = 'active',
},
},
},
resolve = function(input, source)
utils.schedule_main()
local scope = input.scope or 'active'
local buffers = {}
-- Determine which buffers to include based on scope
if scope == 'active' then
if source and source.bufnr and utils.buf_valid(source.bufnr) then
buffers = { source.bufnr }
end
elseif scope == 'visible' then
buffers = vim.tbl_filter(function(b)
return utils.buf_valid(b) and vim.fn.buflisted(b) == 1 and #vim.fn.win_findbuf(b) > 0
end, vim.api.nvim_list_bufs())
elseif scope == 'listed' then
buffers = vim.tbl_filter(function(b)
return utils.buf_valid(b) and vim.fn.buflisted(b) == 1
end, vim.api.nvim_list_bufs())
elseif scope == 'quickfix' then
local items = vim.fn.getqflist()
local file_to_bufnr = {}
for _, item in ipairs(items) do
local filename = item.filename or vim.api.nvim_buf_get_name(item.bufnr)
if filename and item.bufnr and utils.buf_valid(item.bufnr) then
file_to_bufnr[filename] = item.bufnr
end
end
buffers = vim.tbl_values(file_to_bufnr)
elseif tonumber(scope) then
local bufnr = tonumber(scope)
if utils.buf_valid(bufnr) then
buffers = { bufnr }
end
end
if #buffers == 0 then
error('No buffers found for input: ' .. scope)
end
local results = {}
for _, bufnr in ipairs(buffers) do
local name = vim.api.nvim_buf_get_name(bufnr)
local data, mimetype = resources.get_buffer(bufnr)
if data then
local diag_text = get_diagnostics_text(bufnr)
if diag_text ~= '' then
data = data .. diag_text
end
table.insert(results, {
uri = 'buffer://' .. bufnr,
name = name,
mimetype = mimetype,
data = data,
})
end
end
return results
end,
},
selection = {
group = 'copilot',
uri = 'neovim://selection',
description = 'Includes the content of the current visual selection with diagnostics. Useful for discussing specific code snippets or text blocks.',
resolve = function(_, source)
utils.schedule_main()
local select = require('CopilotChat.select')
local selection = select.get(source.bufnr)
if not selection then
return {}
end
local data = selection.content
local diag_text = get_diagnostics_text(source.bufnr, selection.start_line, selection.end_line)
if diag_text ~= '' then
data = data .. diag_text
end
return {
{
uri = 'neovim://selection',
name = selection.filename,
mimetype = files.filetype_to_mimetype(selection.filetype),
data = data,
annotations = {
start_line = selection.start_line,
end_line = selection.end_line,
},
},
}
end,
},
clipboard = {
group = 'copilot',
uri = 'neovim://clipboard',
description = 'Provides access to the system clipboard content. Useful for discussing copied text or code snippets.',
resolve = function()
utils.schedule_main()
local lines = vim.fn.getreg('+')
if not lines or lines == '' then
return {}
end
return {
{
uri = 'neovim://clipboard',
mimetype = 'text/plain',
data = lines,
},
}
end,
},
glob = {
group = 'copilot',
uri = 'files://glob/{pattern}',
description = 'Lists filenames matching a pattern in your workspace. Useful for discovering relevant files or understanding the project structure.',
schema = {
type = 'object',
required = { 'pattern' },
properties = {
pattern = {
type = 'string',
description = 'Glob pattern to match files.',
default = '**/*',
},
},
},
resolve = function(input, source)
local out = files.glob(source.cwd(), {
pattern = input.pattern,
})
return {
{
uri = 'files://glob/' .. input.pattern,
mimetype = 'text/plain',
data = table.concat(out, '\n'),
},
}
end,
},
grep = {
group = 'copilot',
uri = 'files://grep/{pattern}',
description = 'Searches for a pattern across files in your workspace. Helpful for finding specific code elements or patterns.',
schema = {
type = 'object',
required = { 'pattern' },
properties = {
pattern = {
type = 'string',
description = 'Pattern to search for.',
},
},
},
resolve = function(input, source)
local out = files.grep(source.cwd(), {
pattern = input.pattern,
})
return {
{
uri = 'files://grep/' .. input.pattern,
mimetype = 'text/plain',
data = table.concat(out, '\n'),
},
}
end,
},
gitdiff = {
group = 'copilot',
uri = 'git://diff/{target}',
description = 'Retrieves git diff information. Requires git to be installed. Useful for discussing code changes or explaining the purpose of modifications.',
schema = {
type = 'object',
required = { 'target' },
properties = {
target = {
type = 'string',
description = 'Target to diff against.',
enum = { 'unstaged', 'staged', '<sha>' },
default = 'unstaged',
},
},
},
resolve = function(input, source)
local cmd = {
'git',
'-C',
source.cwd(),
'diff',
'--no-color',
'--no-ext-diff',
}
if input.target == 'staged' then
table.insert(cmd, '--staged')
elseif input.target == 'unstaged' then
table.insert(cmd, '--')
else
table.insert(cmd, input.target)
end
local out = utils.system(cmd)
return {
{
uri = 'git://diff/' .. input.target,
mimetype = 'text/plain',
data = out.stdout,
},
}
end,
},
bash = {
group = 'copilot',
description = 'Executes a bash command and returns its output. Useful for running shell commands, checking file contents, or gathering system information.',
schema = {
type = 'object',
required = { 'command' },
properties = {
command = {
type = 'string',
description = 'Bash command to execute.',
},
},
},
resolve = function(input, source)
local cmd = { 'bash', '-c', input.command }
local out = utils.system(cmd, source.cwd())
return {
{
data = out.stdout,
},
}
end,
},
edit = {
group = 'copilot',
description = 'Applies a unified diff to a file. The diff should be in unified diff format (similar to diff -U0 output).',
schema = {
type = 'object',
required = { 'filename', 'diff' },
properties = {
filename = {
type = 'string',
description = 'Path to file to edit.',
},
diff = {
type = 'string',
description = 'Unified diff content to apply to the file.',
},
},
},
resolve = function(input, source)
utils.schedule_main()
local select = require('CopilotChat.select')
local diff = require('CopilotChat.utils.diff')
-- Find or create the buffer for the file
local filename = input.filename
local diff_bufnr = nil
-- Try to find matching buffer first
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 still not found, try to load or create buffer
if not diff_bufnr then
diff_bufnr = vim.fn.bufadd(filename)
vim.fn.bufload(diff_bufnr)
end
-- Get current buffer content
local lines = vim.api.nvim_buf_get_lines(diff_bufnr, 0, -1, false)
local content = table.concat(lines, '\n')
-- Apply the unified diff
local new_lines, applied, first, last = diff.apply_unified_diff(input.diff, content)
if applied then
-- Apply changes to buffer
vim.api.nvim_buf_set_lines(diff_bufnr, 0, -1, false, new_lines)
-- If source window is valid, switch to the edited buffer and highlight changes
if source and source.winnr and vim.api.nvim_win_is_valid(source.winnr) then
vim.api.nvim_win_set_buf(source.winnr, diff_bufnr)
if first and last then
select.set(diff_bufnr, source.winnr, first, last)
select.highlight(diff_bufnr)
end
end
return {
{
data = string.format('Successfully applied diff to %s (lines %d-%d)', filename, first or 0, last or 0),
},
}
else
error('Failed to apply diff to ' .. filename)
end
end,
},
}