Skip to content

Commit

Permalink
feat: add diagnostic troubleshooting command
Browse files Browse the repository at this point in the history
  • Loading branch information
jellydn committed Feb 15, 2024
1 parent fbe3f2c commit 0e5eced
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
17 changes: 17 additions & 0 deletions lua/CopilotChat/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,23 @@ M.setup = function(options)
end, { nargs = '*', range = true })
end

-- Troubleshoot and fix the diagnostic issue at the current cursor position.
utils.create_cmd('CopilotChatFixDiagnostic', function()
local diagnostic = utils.get_diagnostics()
local file_name = vim.fn.expand('%:t')
local line_number = vim.fn.line('.')
-- Copy all the lines from current buffer to unnamed register
vim.cmd('normal! ggVG"*y')
vim.cmd(
'CopilotChat Please assist with the following diagnostic issue in file: "'
.. file_name
.. ':'
.. line_number
.. '". '
.. diagnostic
)
end, { nargs = '*', range = true })

-- Show debug info
utils.create_cmd('CopilotChatDebugInfo', function()
-- Get the log file path
Expand Down
22 changes: 22 additions & 0 deletions lua/CopilotChat/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,26 @@ M.log_error = function(...)
log.error(...)
end

--- Get diagnostics for the current line
--- It uses the built-in LSP client in Neovim to get the diagnostics.
--- @return string
M.get_diagnostics = function()
local buffer_number = vim.api.nvim_get_current_buf()
local cursor = vim.api.nvim_win_get_cursor(0)
local line_diagnostics = vim.lsp.diagnostic.get_line_diagnostics(buffer_number, cursor[1] - 1)

if #line_diagnostics == 0 then
return 'No diagnostics available'
end

local diagnostics = {}
for _, diagnostic in ipairs(line_diagnostics) do
table.insert(diagnostics, diagnostic.message)
end

local result = table.concat(diagnostics, '. ')
result = result:gsub('^%s*(.-)%s*$', '%1'):gsub('\n', ' ')
return result
end

return M

0 comments on commit 0e5eced

Please sign in to comment.