Add feature to provide LSP diagnostics alongside code context
It would be nice to have an option to provide LSP diagnostics when using GpChatNew with some selected context. So if you select code which has a LSP error/warning etc then it would be included in the context in addition to the code. I am willing to work on this when I have time if this is a feature you are interested in merging
There exists https://github.com/piersolenski/wtf.nvim which does that but it does not let you have a persistent conversation like this plugin allows you to do, so I think this feature would be useful in this plugin
Here’s my hook. You can paste it into your config or modify it to suit your needs. No need to wait for the plugin implementation, then use it by Gp[CustomHook]
FixErrorAtCursor = function(gp, params)
local bufnr = vim.api.nvim_get_current_buf()
local cursor_line, cursor_col = unpack(vim.api.nvim_win_get_cursor(0))
cursor_line = cursor_line - 1 -- Convert to 0-based index
local diagnostics = vim.diagnostic.get(bufnr)
local cursor_diagnostic = nil
for _, diagnostic in ipairs(diagnostics) do
if diagnostic.lnum == cursor_line and diagnostic.col <= cursor_col and cursor_col <= diagnostic.end_col then
cursor_diagnostic = diagnostic
break
end
end
if not cursor_diagnostic then
print("No diagnostic found at cursor position")
return
end
local error_message = string.format("Error at Line %d: %s [%s]. Source: %s\n",
cursor_diagnostic.lnum + 1,
cursor_diagnostic.message,
cursor_diagnostic.code or "N/A",
cursor_diagnostic.source or "N/A")
-- Select the entire range of the error
vim.api.nvim_win_set_cursor(0, { cursor_diagnostic.lnum + 1, cursor_diagnostic.col })
vim.cmd('normal! v')
vim.api.nvim_win_set_cursor(0, { cursor_diagnostic.end_lnum + 1, cursor_diagnostic.end_col })
local selected_text = vim.fn.getline(cursor_diagnostic.lnum + 1, cursor_diagnostic.end_lnum + 1)
local selection = table.concat(selected_text, '\n')
local template = "Having following from {{filename}}:\n\n" ..
"```lua\n" .. selection .. "\n```\n\n" ..
"Error found at the cursor:\n" .. error_message ..
"Please respond by fixing the error above." ..
"\n\nRespond exclusively with the snippet that should replace the selection above."
local agent = gp.get_command_agent()
gp.logger.info("Fixing error at cursor with agent: " .. agent.name)
gp.Prompt(
params,
gp.Target.rewrite,
agent,
template,
nil, -- command will run directly without any prompting for user input
nil -- no predefined instructions
)
end,