Skip to content

Commit

Permalink
feat: format diagnostic (#895)
Browse files Browse the repository at this point in the history
  • Loading branch information
yetone authored Nov 23, 2024
1 parent 5e6d336 commit c8e688a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 18 deletions.
16 changes: 6 additions & 10 deletions lua/avante/templates/_diagnostics.avanterules
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
{%- if use_xml_format -%}
{%- if diagnostics -%}
<diagnostic_field_description>
lnum: The starting line of the diagnostic (1-indexed)
end_lnum: The final line of the diagnostic (1-indexed)
col: The starting column of the diagnostic (1-indexed)
end_col: The final column of the diagnostic (1-indexed)
content: The diagnostic content
start_line: The starting line of the diagnostic (1-indexed)
end_line: The final line of the diagnostic (1-indexed)
severity: The severity of the diagnostic
message: The diagnostic text
source: The source of the diagnostic
</diagnostic_field_description>
<diagnostics>
Expand All @@ -16,12 +14,10 @@ source: The source of the diagnostic
{%- else -%}
{%- if diagnostics -%}
DIAGNOSTIC_FIELD_DESCRIPTION:
lnum: The starting line of the diagnostic (1-indexed)
end_lnum: The final line of the diagnostic (1-indexed)
col: The starting column of the diagnostic (1-indexed)
end_col: The final column of the diagnostic (1-indexed)
content: The diagnostic content
start_line: The starting line of the diagnostic (1-indexed)
end_line: The final line of the diagnostic (1-indexed)
severity: The severity of the diagnostic
message: The diagnostic text
source: The source of the diagnostic

DIAGNOSTICS:
Expand Down
36 changes: 28 additions & 8 deletions lua/avante/utils/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -772,20 +772,40 @@ function M.update_buffer_content(bufnr, new_lines)
end
end

local severity = {
[1] = "ERROR",
[2] = "WARNING",
[3] = "INFORMATION",
[4] = "HINT",
}

---@class AvanteDiagnostic
---@field content string
---@field start_line number
---@field end_line number
---@field severity string
---@field source string

---@param bufnr integer
---@return vim.Diagnostic[]
---@return AvanteDiagnostic[]
function M.get_diagnostics(bufnr)
if bufnr == nil then bufnr = api.nvim_get_current_buf() end
local diagnositcs = ---@type vim.Diagnostic[]
vim.diagnostic.get(bufnr, { severity = { vim.diagnostic.severity.ERROR, vim.diagnostic.severity.WARN } })
vim.diagnostic.get(
bufnr,
{ severity = { vim.diagnostic.severity.ERROR, vim.diagnostic.severity.WARN, vim.diagnostic.severity.HINT } }
)
return vim
.iter(diagnositcs)
:map(function(diagnostic)
diagnostic.lnum = diagnostic.lnum + 1
diagnostic.end_lnum = diagnostic.end_lnum + 1
diagnostic.col = diagnostic.col + 1
diagnostic.end_col = diagnostic.end_col + 1
return diagnostic
local d = {
content = diagnostic.message,
start_line = diagnostic.lnum + 1,
end_line = diagnostic.end_lnum and diagnostic.end_lnum + 1 or diagnostic.lnum + 1,
severity = severity[diagnostic.severity],
source = diagnostic.source,
}
return d
end)
:totable()
end
Expand All @@ -796,7 +816,7 @@ function M.get_current_selection_diagnostics(bufnr, selection)
local diagnostics = M.get_diagnostics(bufnr)
local selection_diagnostics = {}
for _, diagnostic in ipairs(diagnostics) do
if selection.range.start.lnum <= diagnostic.lnum and selection.range.finish.lnum >= diagnostic.end_lnum then
if selection.range.start.lnum <= diagnostic.start_line and selection.range.finish.lnum >= diagnostic.end_line then
table.insert(selection_diagnostics, diagnostic)
end
end
Expand Down

0 comments on commit c8e688a

Please sign in to comment.