forked from CopilotC-Nvim/CopilotChat.nvim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.lua
More file actions
516 lines (463 loc) · 13.5 KB
/
functions.lua
File metadata and controls
516 lines (463 loc) · 13.5 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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
local resources = require('CopilotChat.resources')
local utils = require('CopilotChat.utils')
---@class CopilotChat.config.functions.Result
---@field data string
---@field mimetype string?
---@field uri string?
---@class CopilotChat.config.functions.Function
---@field description string?
---@field schema table?
---@field group string?
---@field uri string?
---@field resolve fun(input: table, source: CopilotChat.source, prompt: string):table<CopilotChat.config.functions.Result>
---@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 utils.glob(source.cwd(), {
max_count = 0,
})
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,
mimetype = mimetype,
data = data,
},
}
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 files = utils.glob(source.cwd(), {
pattern = input.pattern,
})
return {
{
uri = 'files://glob/' .. input.pattern,
mimetype = 'text/plain',
data = table.concat(files, '\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 files = utils.grep(source.cwd(), {
pattern = input.pattern,
})
return {
{
uri = 'files://grep/' .. input.pattern,
mimetype = 'text/plain',
data = table.concat(files, '\n'),
},
}
end,
},
buffer = {
group = 'copilot',
uri = 'buffer://{name}',
description = 'Retrieves content from a specific buffer. Useful for discussing or analyzing code from a particular file that is currently loaded.',
schema = {
type = 'object',
required = { 'name' },
properties = {
name = {
type = 'string',
description = 'Buffer filename to include in chat context.',
enum = function()
return vim
.iter(vim.api.nvim_list_bufs())
:filter(function(buf)
return buf and utils.buf_valid(buf) and vim.fn.buflisted(buf) == 1
end)
:map(function(buf)
return vim.api.nvim_buf_get_name(buf)
end)
:totable()
end,
},
},
},
resolve = function(input, source)
utils.schedule_main()
local name = input.name or vim.api.nvim_buf_get_name(source.bufnr)
local found_buf = nil
for _, buf in ipairs(vim.api.nvim_list_bufs()) do
if vim.api.nvim_buf_get_name(buf) == name then
found_buf = buf
break
end
end
if not found_buf then
error('Buffer not found: ' .. name)
end
local data, mimetype = resources.get_buffer(found_buf)
if not data then
error('Buffer not found: ' .. name)
end
return {
{
uri = 'buffer://' .. name,
mimetype = mimetype,
data = data,
},
}
end,
},
buffers = {
group = 'copilot',
uri = 'buffers://{scope}',
description = 'Fetches content from multiple buffers. Helps with discussing or analyzing code across multiple files simultaneously.',
schema = {
type = 'object',
required = { 'scope' },
properties = {
scope = {
type = 'string',
description = 'Scope of buffers to include in chat context.',
enum = { 'listed', 'visible' },
default = 'listed',
},
},
},
resolve = function(input)
utils.schedule_main()
return vim
.iter(vim.api.nvim_list_bufs())
:filter(function(bufnr)
return utils.buf_valid(bufnr)
and vim.fn.buflisted(bufnr) == 1
and (input.scope == 'listed' or #vim.fn.win_findbuf(bufnr) > 0)
end)
:map(function(bufnr)
local name = vim.api.nvim_buf_get_name(bufnr)
local data, mimetype = resources.get_buffer(bufnr)
if not data then
return nil
end
return {
uri = 'buffer://' .. name,
mimetype = mimetype,
data = data,
}
end)
:filter(function(file_data)
return file_data ~= nil
end)
:totable()
end,
},
quickfix = {
group = 'copilot',
uri = 'neovim://quickfix',
description = 'Includes the content of all files referenced in the current quickfix list. Useful for discussing compilation errors, search results, or other collected locations.',
resolve = function()
utils.schedule_main()
local items = vim.fn.getqflist()
if not items or #items == 0 then
return {}
end
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 then
if item.bufnr and utils.buf_valid(item.bufnr) then
file_to_bufnr[filename] = item.bufnr
else
file_to_bufnr[filename] = false
end
end
end
return vim
.iter(vim.tbl_keys(file_to_bufnr))
:map(function(file)
local bufnr = file_to_bufnr[file]
local data, mimetype, uri
if bufnr and bufnr ~= false then
data, mimetype = resources.get_buffer(bufnr)
uri = 'buffer://' .. file
else
data, mimetype = resources.get_file(file)
uri = 'file://' .. file
end
if not data then
return nil
end
return {
uri = uri,
mimetype = mimetype,
data = data,
}
end)
:filter(function(file_data)
return file_data ~= nil
end)
:totable()
end,
},
diagnostics = {
group = 'copilot',
uri = 'neovim://diagnostics/{scope}/{severity}',
description = 'Collects code diagnostics (errors, warnings, etc.) from specified buffers. Helpful for troubleshooting and fixing code issues.',
schema = {
type = 'object',
required = { 'scope', 'severity' },
properties = {
scope = {
type = 'string',
description = 'Scope of buffers to use for retrieving diagnostics.',
enum = { 'current', 'listed', 'visible' },
default = 'current',
},
severity = {
type = 'string',
description = 'Minimum severity level of diagnostics to include.',
enum = { 'error', 'warn', 'info', 'hint' },
default = 'warn',
},
},
},
resolve = function(input, source)
utils.schedule_main()
local out = {}
local scope = input.scope or 'current'
local buffers = {}
-- Get buffers based on scope
if scope == 'current' then
if source and source.bufnr and utils.buf_valid(source.bufnr) then
buffers = { source.bufnr }
end
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 == '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())
else
buffers = vim.tbl_filter(function(b)
return utils.buf_valid(b) and vim.api.nvim_buf_get_name(b) == input.scope
end, vim.api.nvim_list_bufs())
end
-- Collect diagnostics for each buffer
for _, bufnr in ipairs(buffers) do
local name = vim.api.nvim_buf_get_name(bufnr)
local diagnostics = vim.diagnostic.get(bufnr, {
severity = {
min = vim.diagnostic.severity[input.severity:upper()],
},
})
if #diagnostics > 0 then
local diag_lines = {}
for _, diag in ipairs(diagnostics) do
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
table.insert(out, {
uri = 'neovim://diagnostics/' .. name,
mimetype = 'text/plain',
data = table.concat(diag_lines, '\n'),
})
end
end
return out
end,
},
register = {
group = 'copilot',
uri = 'neovim://register/{register}',
description = 'Provides access to the content of a specified Vim register. Useful for discussing yanked text, clipboard content, or previously executed commands.',
schema = {
type = 'object',
required = { 'register' },
properties = {
register = {
type = 'string',
description = 'Register to include in chat context.',
enum = {
'+',
'*',
'"',
'0',
'-',
'.',
'%',
':',
'#',
'=',
'/',
},
default = '+',
},
},
},
resolve = function(input)
utils.schedule_main()
local lines = vim.fn.getreg(input.register)
if not lines or lines == '' then
return {}
end
return {
{
uri = 'neovim://register/' .. input.register,
mimetype = 'text/plain',
data = lines,
},
}
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,
},
gitstatus = {
group = 'copilot',
uri = 'git://status',
description = 'Retrieves the status of the current git repository. Useful for discussing changes, commits, and other git-related tasks.',
resolve = function(_, source)
local cmd = {
'git',
'-C',
source.cwd(),
'status',
}
local out = utils.system(cmd)
return {
{
uri = 'git://status',
mimetype = 'text/plain',
data = out.stdout,
},
}
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
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,
},
}