feat: format diagnostic (#895)

This commit is contained in:
yetone 2024-11-24 06:02:34 +08:00 committed by GitHub
parent 5e6d3368f7
commit c8e688a0ac
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 34 additions and 18 deletions

View File

@ -1,12 +1,10 @@
{%- if use_xml_format -%} {%- if use_xml_format -%}
{%- if diagnostics -%} {%- if diagnostics -%}
<diagnostic_field_description> <diagnostic_field_description>
lnum: The starting line of the diagnostic (1-indexed) content: The diagnostic content
end_lnum: The final line of the diagnostic (1-indexed) start_line: The starting line of the diagnostic (1-indexed)
col: The starting column of the diagnostic (1-indexed) end_line: The final line of the diagnostic (1-indexed)
end_col: The final column of the diagnostic (1-indexed)
severity: The severity of the diagnostic severity: The severity of the diagnostic
message: The diagnostic text
source: The source of the diagnostic source: The source of the diagnostic
</diagnostic_field_description> </diagnostic_field_description>
<diagnostics> <diagnostics>
@ -16,12 +14,10 @@ source: The source of the diagnostic
{%- else -%} {%- else -%}
{%- if diagnostics -%} {%- if diagnostics -%}
DIAGNOSTIC_FIELD_DESCRIPTION: DIAGNOSTIC_FIELD_DESCRIPTION:
lnum: The starting line of the diagnostic (1-indexed) content: The diagnostic content
end_lnum: The final line of the diagnostic (1-indexed) start_line: The starting line of the diagnostic (1-indexed)
col: The starting column of the diagnostic (1-indexed) end_line: The final line of the diagnostic (1-indexed)
end_col: The final column of the diagnostic (1-indexed)
severity: The severity of the diagnostic severity: The severity of the diagnostic
message: The diagnostic text
source: The source of the diagnostic source: The source of the diagnostic
DIAGNOSTICS: DIAGNOSTICS:

View File

@ -772,20 +772,40 @@ function M.update_buffer_content(bufnr, new_lines)
end end
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 ---@param bufnr integer
---@return vim.Diagnostic[] ---@return AvanteDiagnostic[]
function M.get_diagnostics(bufnr) function M.get_diagnostics(bufnr)
if bufnr == nil then bufnr = api.nvim_get_current_buf() end if bufnr == nil then bufnr = api.nvim_get_current_buf() end
local diagnositcs = ---@type vim.Diagnostic[] 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 return vim
.iter(diagnositcs) .iter(diagnositcs)
:map(function(diagnostic) :map(function(diagnostic)
diagnostic.lnum = diagnostic.lnum + 1 local d = {
diagnostic.end_lnum = diagnostic.end_lnum + 1 content = diagnostic.message,
diagnostic.col = diagnostic.col + 1 start_line = diagnostic.lnum + 1,
diagnostic.end_col = diagnostic.end_col + 1 end_line = diagnostic.end_lnum and diagnostic.end_lnum + 1 or diagnostic.lnum + 1,
return diagnostic severity = severity[diagnostic.severity],
source = diagnostic.source,
}
return d
end) end)
:totable() :totable()
end end
@ -796,7 +816,7 @@ function M.get_current_selection_diagnostics(bufnr, selection)
local diagnostics = M.get_diagnostics(bufnr) local diagnostics = M.get_diagnostics(bufnr)
local selection_diagnostics = {} local selection_diagnostics = {}
for _, diagnostic in ipairs(diagnostics) do 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) table.insert(selection_diagnostics, diagnostic)
end end
end end