fix: diagnostics lnum starts with 1 (#892)

This commit is contained in:
yetone 2024-11-23 23:08:10 +08:00 committed by GitHub
parent 9042f5f202
commit 67e946ef13
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 40 additions and 9 deletions

View File

@ -201,7 +201,7 @@ function Selection:create_editing_input()
input = mentions.new_content input = mentions.new_content
local project_context = mentions.enable_project_context and RepoMap.get_repo_map(file_ext) or nil local project_context = mentions.enable_project_context and RepoMap.get_repo_map(file_ext) or nil
local diagnostics = Utils.get_current_selection_diagnostics() local diagnostics = Utils.get_current_selection_diagnostics(code_bufnr, self.selection)
Llm.stream({ Llm.stream({
bufnr = code_bufnr, bufnr = code_bufnr,

View File

@ -1542,8 +1542,8 @@ function Sidebar:create_input(opts)
local diagnostics = nil local diagnostics = nil
if mentions.enable_diagnostics then if mentions.enable_diagnostics then
if self.selected_code ~= nil then if self.code ~= nil and self.code.bufnr ~= nil and self.code.selection ~= nil then
diagnostics = Utils.get_current_selection_diagnostics() diagnostics = Utils.get_current_selection_diagnostics(self.code.bufnr, self.code.selection)
else else
diagnostics = Utils.get_diagnostics(self.code.bufnr) diagnostics = Utils.get_diagnostics(self.code.bufnr)
end end

View File

@ -1,11 +1,29 @@
{%- if use_xml_format -%} {%- if use_xml_format -%}
{%- if diagnostics -%} {%- 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)
severity: The severity of the diagnostic
message: The diagnostic text
source: The source of the diagnostic
</diagnostic_field_description>
<diagnostics> <diagnostics>
{{diagnostics}} {{diagnostics}}
</diagnostics> </diagnostics>
{%- endif %} {%- endif %}
{%- else -%} {%- else -%}
{%- if diagnostics -%} {%- 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)
severity: The severity of the diagnostic
message: The diagnostic text
source: The source of the diagnostic
DIAGNOSTICS: DIAGNOSTICS:
{{diagnostics}} {{diagnostics}}
{%- endif %} {%- endif %}

View File

@ -772,18 +772,31 @@ function M.update_buffer_content(bufnr, new_lines)
end end
end end
---@param bufnr integer
---@return vim.Diagnostic[]
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
return vim.diagnostic.get(bufnr, { severity = { vim.diagnostic.severity.ERROR, vim.diagnostic.severity.WARN } }) local diagnositcs = ---@type vim.Diagnostic[]
vim.diagnostic.get(bufnr, { severity = { vim.diagnostic.severity.ERROR, vim.diagnostic.severity.WARN } })
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
end)
:totable()
end end
function M.get_current_selection_diagnostics() ---@param bufnr integer
local selection = M.get_visual_selection_and_range() ---@param selection avante.SelectionResult
if not selection then return {} end function M.get_current_selection_diagnostics(bufnr, selection)
local diagnostics = M.get_diagnostics() 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:contains(diagnostic.lnum, diagnostic.col) then if selection.range.start.lnum <= diagnostic.lnum and selection.range.finish.lnum >= diagnostic.end_lnum then
table.insert(selection_diagnostics, diagnostic) table.insert(selection_diagnostics, diagnostic)
end end
end end