Skip to content

Commit 3c59d03

Browse files
gennaro-tedescojellydn
authored andcommitted
feat: introduce diagnostics context and prompt
1 parent f7eb423 commit 3c59d03

File tree

3 files changed

+53
-9
lines changed

3 files changed

+53
-9
lines changed

doc/CopilotChat.txt

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -239,15 +239,16 @@ Predefined prompt templates for common tasks. Reference them with `/PromptName`
239239
in chat, use `:CopilotChat<PromptName>` or `:CopilotChatPrompts` to select
240240
them:
241241

242-
Prompt Description
243-
---------- --------------------------------------------------
244-
Explain Write an explanation for the selected code
245-
Review Review the selected code
246-
Fix Rewrite the code with bug fixes
247-
Optimize Optimize code for performance and readability
248-
Docs Add documentation comments to the code
249-
Tests Generate tests for the code
250-
Commit Write commit message using commitizen convention
242+
Prompt Description
243+
---------- --------------------------------------------------
244+
Explain Write an explanation for the selected code
245+
Review Review the selected code
246+
Fix Rewrite the code with bug fixes
247+
Optimize Optimize code for performance and readability
248+
Docs Add documentation comments to the code
249+
Tests Generate tests for the code
250+
Commit Write commit message using commitizen convention
251+
Diagnostics Propose fix for workspace diagnostics
251252
Define your own prompts in the configuration:
252253

253254
>lua
@@ -363,6 +364,7 @@ Contexts provide additional information to the chat. Add context using
363364
----------- --------------- -------------------------------------
364365
buffer ✓ (number) Current or specified buffer content
365366
buffers ✓ (type) All buffers content (listed/all)
367+
diagnostics - All workspace diagnostics
366368
file ✓ (path) Content of specified file
367369
files ✓ (glob) Workspace files
368370
filenames ✓ (glob) Workspace file names

lua/CopilotChat/config/contexts.lua

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,43 @@ return {
297297
end,
298298
},
299299

300+
diagnostics = {
301+
description = 'All workspace diagnostics',
302+
resolve = function()
303+
local diagnostics = vim.diagnostic.get(nil)
304+
local lines = {}
305+
local severity_labels = {
306+
[vim.diagnostic.severity.ERROR] = 'ERROR',
307+
[vim.diagnostic.severity.WARN] = 'WARN',
308+
[vim.diagnostic.severity.INFO] = 'INFO',
309+
[vim.diagnostic.severity.HINT] = 'HINT',
310+
}
311+
312+
utils.schedule_main()
313+
314+
for _, d in ipairs(diagnostics) do
315+
local filename = vim.fn.fnamemodify(vim.api.nvim_buf_get_name(d.bufnr), ':.')
316+
table.insert(
317+
lines,
318+
string.format(
319+
'%s:%d - %s - %s',
320+
filename,
321+
(d.lnum or 0) + 1,
322+
severity_labels[d.severity] or '',
323+
d.message or ''
324+
)
325+
)
326+
end
327+
return {
328+
{
329+
content = table.concat(lines, '\n'),
330+
filename = 'workspace_diagnostics',
331+
filetype = 'text',
332+
},
333+
}
334+
end,
335+
},
336+
300337
system = {
301338
description = [[Includes output of provided system shell command in chat context. Supports input.
302339

lua/CopilotChat/config/prompts.lua

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,4 +165,9 @@ return {
165165
prompt = 'Write commit message for the change with commitizen convention. Keep the title under 50 characters and wrap message at 72 characters. Format as a gitcommit code block.',
166166
context = 'git:staged',
167167
},
168+
169+
Diagnostics = {
170+
prompt = 'Fix all LSP warnings and errors. Show different code patches for each diagnostic.',
171+
context = { 'diagnostics', 'buffers' },
172+
},
168173
}

0 commit comments

Comments
 (0)