Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: Add option to ask for help with a diagnostic. #193

Open
arnevm123 opened this issue Jan 18, 2024 · 3 comments
Open

Feature request: Add option to ask for help with a diagnostic. #193

arnevm123 opened this issue Jan 18, 2024 · 3 comments

Comments

@arnevm123
Copy link

https://github.com/piersolenski/wtf.nvim is a plugin to ask for explanation for your diagnostic message.
It would be a nice addition to get this inside of neovim with Cody.
So the gist of this would be fetch the diagnostic, give cody some context (maybe with some treesitter help?) and let it explain what I'm doing wrong.

If this is not something in scope of this project feel free to close this issue.

If it is inside of the scope I can try to implement this.

@arnevm123
Copy link
Author

arnevm123 commented Jan 19, 2024

For anyone interested, this works in my personal config:

keymap("n", "gw", utils.search_diagnostics_cody, opts)
keymap("v", "gw", utils.search_diagnostics_cody, opts)

function M:search_diagnostics_cody()
	local start_line, end_line
	local mode = string.lower(vim.fn.mode())
	if mode == "v" then
		start_line = vim.fn.line("v")
		end_line = vim.fn.line(".")
		if start_line > end_line then
			start_line, end_line = end_line, start_line
		end
	else
		start_line = vim.fn.line(".")
		end_line = start_line
	end

	local bufnr = vim.api.nvim_get_current_buf()
	local all_diagnostics = vim.diagnostic.get(bufnr)
	local diagnostics = {}
	for _, diagnostic in ipairs(all_diagnostics) do
		if diagnostic.lnum + 1 >= start_line and diagnostic.lnum < end_line then
			table.insert(diagnostics, diagnostic)
		end
	end

	if #diagnostics == 0 then
		vim.notify("No diagnostics", vim.log.levels.WARN)
		return
	end

	local index = 1
	if #diagnostics > 1 then
		local inputList = { "Which diagnostic do you want explained?:" }
		for i, diagnostic in ipairs(diagnostics) do
			table.insert(inputList, string.format("%d. %s", i, diagnostic.message))
		end
		index = vim.fn.inputlist(inputList)
		if index <= 0 or index > #diagnostics then
			vim.notify("No diagnostic selected", vim.log.levels.INFO)
			return
		end
	end

	local clean_message = diagnostics[index].message:gsub("[A-Za-z0-9:/\\._%-]+[.][A-Za-z0-9]+", "")
	clean_message = clean_message:gsub("[A-Za-z0-9:/\\._%-]+[/\\][A-Za-z0-9:/\\._%-]+[.][A-Za-z0-9]+", "")

	require("sg.cody.commands").ask_range(bufnr, start_line - 1, end_line, msg)
end

@tjdevries
Copy link
Collaborator

Oh, this is a great idea! let me see if I can get something cleaned up based on this and in the next release (I'm currently rewriting some of the chat stuff, so this will have to wait a little bit)

@zippeurfou
Copy link

For other people reading.
This is the prompt I am using following the wtf plugin that uses openai:

  local msg = "You are an expert coder and helpful assistant who can help debug code diagnostics, such as warning and error messages."
  .. "When appropriate, give solutions with code snippets as fenced codeblocks with a language identifier to enable syntax highlighting.\n"
  .. "This is a the diagnostic message for the code:\n"
  .. clean_message

Not sure what @arnevm123 uses.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants