this repo has no description
1local M = {}
2
3-- Get diagnostics for a specific line range
4function M.get_range_diagnostics(bufnr, start_line, end_line)
5 local all_diagnostics = vim.diagnostic.get(bufnr or 0)
6 local range_diagnostics = {}
7
8 for _, diagnostic in ipairs(all_diagnostics) do
9 -- Convert from 0-indexed to 1-indexed for user-facing line numbers
10 local diag_line = diagnostic.lnum + 1
11 if diag_line >= start_line and diag_line <= end_line then
12 table.insert(range_diagnostics, diagnostic)
13 end
14 end
15
16 return range_diagnostics
17end
18
19-- Format diagnostics for Claude prompt
20function M.format_diagnostics_for_prompt(diagnostics)
21 if #diagnostics == 0 then
22 return "No diagnostics found in the selected range."
23 end
24
25 local severity_names = {
26 [vim.diagnostic.severity.ERROR] = "ERROR",
27 [vim.diagnostic.severity.WARN] = "WARNING",
28 [vim.diagnostic.severity.INFO] = "INFO",
29 [vim.diagnostic.severity.HINT] = "HINT"
30 }
31
32 local formatted_lines = {}
33 table.insert(formatted_lines, "Diagnostic Issues:")
34
35 for _, diagnostic in ipairs(diagnostics) do
36 local line_num = diagnostic.lnum + 1 -- Convert to 1-indexed
37 local severity = severity_names[diagnostic.severity] or "UNKNOWN"
38 local message = diagnostic.message:gsub("\n", " ") -- Remove newlines from message
39
40 table.insert(formatted_lines, string.format("Line %d: [%s] %s", line_num, severity, message))
41 end
42
43 return table.concat(formatted_lines, "\n")
44end
45
46-- Quick check if any diagnostics exist in range
47function M.has_diagnostics(bufnr, start_line, end_line)
48 local diagnostics = M.get_range_diagnostics(bufnr, start_line, end_line)
49 return #diagnostics > 0
50end
51
52return M