feat: format diagnostic (#895)
This commit is contained in:
@@ -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
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user