-
-
Notifications
You must be signed in to change notification settings - Fork 170
Expand file tree
/
Copy pathcontexts.lua
More file actions
182 lines (170 loc) · 5.28 KB
/
contexts.lua
File metadata and controls
182 lines (170 loc) · 5.28 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
local async = require('plenary.async')
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, model: 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
return {
context.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'
return context.buffers(input)
end,
},
file = {
description = 'Includes content of provided file in chat context. Supports input.',
input = function(callback, source)
local cwd = utils.win_cwd(source.winnr)
local files = utils.scan_dir(cwd, {
add_dirs = false,
respect_gitignore = true,
max_files = 1000,
})
async.util.scheduler()
vim.ui.select(files, {
prompt = 'Select a file> ',
}, callback)
end,
resolve = function(input)
return {
context.file(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)
return context.files(source.winnr, true, {
search_pattern = input and utils.glob_to_regex(input),
})
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)
return context.files(source.winnr, false, {
search_pattern = input and utils.glob_to_regex(input),
})
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'
return {
context.gitdiff(input, source.winnr),
}
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.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 '+'
return {
context.register(input),
}
end,
},
quickfix = {
description = 'Includes quickfix list file contents in chat context.',
resolve = function()
return context.quickfix()
end,
},
workspace = {
description = 'Includes all non-hidden files in the current workspace in chat context.',
resolve = function(_, _, prompt, model)
return context.workspace(prompt, model)
end,
},
}