forked from CopilotC-Nvim/CopilotChat.nvim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontexts.lua
More file actions
352 lines (315 loc) · 9.43 KB
/
contexts.lua
File metadata and controls
352 lines (315 loc) · 9.43 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
local context = require('CopilotChat.context')
local utils = require('CopilotChat.utils')
---@class CopilotChat.config.context
---@field description string?
---@field input fun(callback: fun(input: string?), source: CopilotChat.source)?
---@field resolve fun(input: string?, source: CopilotChat.source, prompt: string):table<CopilotChat.context.embed>
---@type table<string, CopilotChat.config.context>
return {
buffer = {
description = 'Includes specified buffer in chat context. Supports input (default current).',
input = function(callback)
vim.ui.select(
vim.tbl_map(
function(buf)
return { id = buf, name = vim.fn.fnamemodify(vim.api.nvim_buf_get_name(buf), ':p:.') }
end,
vim.tbl_filter(function(buf)
return utils.buf_valid(buf) and vim.fn.buflisted(buf) == 1
end, vim.api.nvim_list_bufs())
),
{
prompt = 'Select a buffer> ',
format_item = function(item)
return item.name
end,
},
function(choice)
callback(choice and choice.id)
end
)
end,
resolve = function(input, source)
input = input and tonumber(input) or source.bufnr
utils.schedule_main()
return {
context.get_buffer(input),
}
end,
},
buffers = {
description = 'Includes all buffers in chat context. Supports input (default listed).',
input = function(callback)
vim.ui.select({ 'listed', 'visible' }, {
prompt = 'Select buffer scope> ',
}, callback)
end,
resolve = function(input)
input = input or 'listed'
utils.schedule_main()
return vim.tbl_map(
context.get_buffer,
vim.tbl_filter(function(b)
return utils.buf_valid(b) and vim.fn.buflisted(b) == 1 and (input == 'listed' or #vim.fn.win_findbuf(b) > 0)
end, vim.api.nvim_list_bufs())
)
end,
},
file = {
description = 'Includes content of provided file in chat context. Supports input.',
input = function(callback, source)
local files = utils.scan_dir(source.cwd(), {
max_count = 0,
})
utils.schedule_main()
vim.ui.select(files, {
prompt = 'Select a file> ',
}, callback)
end,
resolve = function(input)
if not input or input == '' then
return {}
end
utils.schedule_main()
return {
context.get_file(utils.filepath(input), utils.filetype(input)),
}
end,
},
files = {
description = 'Includes all non-hidden files in the current workspace in chat context. Supports input (glob pattern).',
input = function(callback)
vim.ui.input({
prompt = 'Enter glob> ',
}, callback)
end,
resolve = function(input, source)
local files = utils.scan_dir(source.cwd(), {
glob = input,
})
utils.schedule_main()
files = vim.tbl_filter(
function(file)
return file.ft ~= nil
end,
vim.tbl_map(function(file)
return {
name = utils.filepath(file),
ft = utils.filetype(file),
}
end, files)
)
return vim
.iter(files)
:map(function(file)
return context.get_file(file.name, file.ft)
end)
:filter(function(file_data)
return file_data ~= nil
end)
:totable()
end,
},
filenames = {
description = 'Includes names of all non-hidden files in the current workspace in chat context. Supports input (glob pattern).',
input = function(callback)
vim.ui.input({
prompt = 'Enter glob> ',
}, callback)
end,
resolve = function(input, source)
local out = {}
local files = utils.scan_dir(source.cwd(), {
glob = input,
})
local chunk_size = 100
for i = 1, #files, chunk_size do
local chunk = {}
for j = i, math.min(i + chunk_size - 1, #files) do
table.insert(chunk, files[j])
end
local chunk_number = math.floor(i / chunk_size)
local chunk_name = chunk_number == 0 and 'file_map' or 'file_map' .. tostring(chunk_number)
table.insert(out, {
content = table.concat(chunk, '\n'),
filename = chunk_name,
filetype = 'text',
score = 0.1,
})
end
return out
end,
},
git = {
description = 'Requires `git`. Includes current git diff in chat context. Supports input (default unstaged, also accepts commit number).',
input = function(callback)
vim.ui.select({ 'unstaged', 'staged' }, {
prompt = 'Select diff type> ',
}, callback)
end,
resolve = function(input, source)
input = input or 'unstaged'
local cmd = {
'git',
'-C',
source.cwd(),
'diff',
'--no-color',
'--no-ext-diff',
}
if input == 'staged' then
table.insert(cmd, '--staged')
elseif input == 'unstaged' then
table.insert(cmd, '--')
else
table.insert(cmd, input)
end
local out = utils.system(cmd)
return {
{
content = out.stdout,
filename = 'git_diff_' .. input,
filetype = 'diff',
},
}
end,
},
url = {
description = 'Includes content of provided URL in chat context. Supports input.',
input = function(callback)
vim.ui.input({
prompt = 'Enter URL> ',
default = 'https://',
}, callback)
end,
resolve = function(input)
return {
context.get_url(input),
}
end,
},
register = {
description = 'Includes contents of register in chat context. Supports input (default +, e.g clipboard).',
input = function(callback)
local choices = utils.kv_list({
['+'] = 'synchronized with the system clipboard',
['*'] = 'synchronized with the selection clipboard',
['"'] = 'last deleted, changed, or yanked content',
['0'] = 'last yank',
['-'] = 'deleted or changed content smaller than one line',
['.'] = 'last inserted text',
['%'] = 'name of the current file',
[':'] = 'most recent executed command',
['#'] = 'alternate buffer',
['='] = 'result of an expression',
['/'] = 'last search pattern',
})
vim.ui.select(choices, {
prompt = 'Select a register> ',
format_item = function(choice)
return choice.key .. ' - ' .. choice.value
end,
}, function(choice)
callback(choice and choice.key)
end)
end,
resolve = function(input)
input = input or '+'
utils.schedule_main()
local lines = vim.fn.getreg(input)
if not lines or lines == '' then
return {}
end
return {
{
content = lines,
filename = 'vim_register_' .. input,
filetype = '',
},
}
end,
},
quickfix = {
description = 'Includes quickfix list file contents in chat context.',
resolve = function()
utils.schedule_main()
local items = vim.fn.getqflist()
if not items or #items == 0 then
return {}
end
local unique_files = {}
for _, item in ipairs(items) do
local filename = item.filename or vim.api.nvim_buf_get_name(item.bufnr)
if filename then
unique_files[filename] = true
end
end
local files = vim.tbl_filter(
function(file)
return file.ft ~= nil
end,
vim.tbl_map(function(file)
return {
name = utils.filepath(file),
ft = utils.filetype(file),
}
end, vim.tbl_keys(unique_files))
)
return vim
.iter(files)
:map(function(file)
return context.get_file(file.name, file.ft)
end)
:filter(function(file_data)
return file_data ~= nil
end)
:totable()
end,
},
system = {
description = [[Includes output of provided system shell command in chat context. Supports input.
Important:
- Only use system commands as last resort, they are run every time the context is requested.
- For example instead of curl use the url context, instead of finding and grepping try to check if there is any context that can query the data you need instead.
- If you absolutely need to run a system command, try to use read-only commands and avoid commands that modify the system state.
]],
input = function(callback)
vim.ui.input({
prompt = 'Enter command> ',
}, callback)
end,
resolve = function(input)
if not input or input == '' then
return {}
end
utils.schedule_main()
local shell, shell_flag
if vim.fn.has('win32') == 1 then
shell, shell_flag = 'cmd.exe', '/c'
else
shell, shell_flag = 'sh', '-c'
end
local out = utils.system({ shell, shell_flag, input })
if not out then
return {}
end
local out_type = 'command_output'
local out_text = out.stdout
if out.code ~= 0 then
out_type = 'command_error'
if out.stderr and out.stderr ~= '' then
out_text = out.stderr
elseif not out_text or out_text == '' then
out_text = 'Command failed with exit code ' .. out.code
end
end
return {
{
content = out_text,
filename = out_type .. '_' .. input:gsub('[^%w]', '_'):sub(1, 20),
filetype = 'text',
},
}
end,
},
}