Skip to content

Commit

Permalink
fix: diagnostics lnum starts with 1 (yetone#892)
Browse files Browse the repository at this point in the history
  • Loading branch information
yetone authored Nov 23, 2024
1 parent 9042f5f commit 67e946e
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 9 deletions.
2 changes: 1 addition & 1 deletion lua/avante/selection.lua
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ function Selection:create_editing_input()
input = mentions.new_content
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({
bufnr = code_bufnr,
Expand Down
4 changes: 2 additions & 2 deletions lua/avante/sidebar.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1542,8 +1542,8 @@ function Sidebar:create_input(opts)

local diagnostics = nil
if mentions.enable_diagnostics then
if self.selected_code ~= nil then
diagnostics = Utils.get_current_selection_diagnostics()
if self.code ~= nil and self.code.bufnr ~= nil and self.code.selection ~= nil then
diagnostics = Utils.get_current_selection_diagnostics(self.code.bufnr, self.code.selection)
else
diagnostics = Utils.get_diagnostics(self.code.bufnr)
end
Expand Down
18 changes: 18 additions & 0 deletions lua/avante/templates/_diagnostics.avanterules
Original file line number Diff line number Diff line change
@@ -1,11 +1,29 @@
{%- 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)
severity: The severity of the diagnostic
message: The diagnostic text
source: The source of the diagnostic
</diagnostic_field_description>
<diagnostics>
{{diagnostics}}
</diagnostics>
{%- endif %}
{%- 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)
severity: The severity of the diagnostic
message: The diagnostic text
source: The source of the diagnostic

DIAGNOSTICS:
{{diagnostics}}
{%- endif %}
Expand Down
25 changes: 19 additions & 6 deletions lua/avante/utils/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -772,18 +772,31 @@ function M.update_buffer_content(bufnr, new_lines)
end
end

---@param bufnr integer
---@return vim.Diagnostic[]
function M.get_diagnostics(bufnr)
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

function M.get_current_selection_diagnostics()
local selection = M.get_visual_selection_and_range()
if not selection then return {} end
local diagnostics = M.get_diagnostics()
---@param bufnr integer
---@param selection avante.SelectionResult
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: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)
end
end
Expand Down

0 comments on commit 67e946e

Please sign in to comment.