Skip to content

Commit 152c1d5

Browse files
committed
Include filenames in buffer context selection, improve buffers context
Signed-off-by: Tomas Slusny <[email protected]>
1 parent edea005 commit 152c1d5

File tree

2 files changed

+35
-16
lines changed

2 files changed

+35
-16
lines changed

README.md

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -180,11 +180,11 @@ You can set the context in the prompt by using `#` followed by the context name.
180180
If context supports input, you can set the input in the prompt by using `:` followed by the input (or pressing `complete` key after `:`).
181181
Default contexts are:
182182

183-
- `buffer` - Includes only the current buffer in chat context. Supports input.
184-
- `buffers` - Includes all open buffers in chat context
183+
- `buffer` - Includes specified buffer in chat context (default current). Supports input.
184+
- `buffers` - Includes all buffers in chat context (default listed). Supports input.
185185
- `file` - Includes content of provided file in chat context. Supports input.
186186
- `files` - Includes all non-hidden filenames in the current workspace in chat context. Supports input.
187-
- `git` - Includes current git diff in chat context. Supports input.
187+
- `git` - Includes current git diff in chat context (default unstaged). Supports input.
188188

189189
### API
190190

@@ -297,23 +297,18 @@ Also see [here](/lua/CopilotChat/config.lua):
297297
-- default contexts
298298
contexts = {
299299
buffer = {
300-
description = 'Includes only the current buffer in chat context. Supports input.',
301300
-- see config.lua for implementation
302301
},
303302
buffers = {
304-
description = 'Includes all open buffers in chat context.',
305303
-- see config.lua for implementation
306304
},
307305
file = {
308-
description = 'Includes content of provided file in chat context. Supports input.',
309306
-- see config.lua for implementation
310307
},
311308
files = {
312-
description = 'Includes all non-hidden filenames in the current workspace in chat context. Supports input.',
313309
-- see config.lua for implementation
314310
},
315311
git = {
316-
description = 'Includes current git diff in chat context. Supports input.',
317312
-- see config.lua for implementation
318313
},
319314
},

lua/CopilotChat/config.lua

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -136,11 +136,27 @@ return {
136136
-- default contexts
137137
contexts = {
138138
buffer = {
139-
description = 'Includes only the current buffer in chat context. Supports input.',
139+
description = 'Includes specified buffer in chat context (default current). Supports input.',
140140
input = function(callback)
141-
vim.ui.select(vim.api.nvim_list_bufs(), {
142-
prompt = 'Select a buffer> ',
143-
}, callback)
141+
vim.ui.select(
142+
vim.tbl_map(
143+
function(buf)
144+
return { id = buf, name = vim.api.nvim_buf_get_name(buf) }
145+
end,
146+
vim.tbl_filter(function(buf)
147+
return vim.api.nvim_buf_is_loaded(buf) and vim.fn.buflisted(buf) == 1
148+
end, vim.api.nvim_list_bufs())
149+
),
150+
{
151+
prompt = 'Select a buffer> ',
152+
format_item = function(item)
153+
return item.name
154+
end,
155+
},
156+
function(choice)
157+
callback(choice and choice.id)
158+
end
159+
)
144160
end,
145161
resolve = function(input, source)
146162
return {
@@ -149,12 +165,20 @@ return {
149165
end,
150166
},
151167
buffers = {
152-
description = 'Includes all open buffers in chat context.',
153-
resolve = function()
168+
description = 'Includes all buffers in chat context (default listed). Supports input.',
169+
input = function(callback)
170+
vim.ui.select({ 'listed', 'visible' }, {
171+
prompt = 'Select buffer scope> ',
172+
}, callback)
173+
end,
174+
resolve = function(input)
175+
input = input or 'listed'
154176
return vim.tbl_map(
155177
context.outline,
156178
vim.tbl_filter(function(b)
157-
return vim.api.nvim_buf_is_loaded(b) and vim.fn.buflisted(b) == 1
179+
return vim.api.nvim_buf_is_loaded(b)
180+
and vim.fn.buflisted(b) == 1
181+
and (input == 'listed' or #vim.fn.win_findbuf(b) > 0)
158182
end, vim.api.nvim_list_bufs())
159183
)
160184
end,
@@ -189,7 +213,7 @@ return {
189213
end,
190214
},
191215
git = {
192-
description = 'Includes current git diff in chat context. Supports input.',
216+
description = 'Includes current git diff in chat context (default unstaged). Supports input.',
193217
input = function(callback)
194218
vim.ui.select({ 'unstaged', 'staged' }, {
195219
prompt = 'Select diff type> ',

0 commit comments

Comments
 (0)