diff --git a/lua/avante/selection.lua b/lua/avante/selection.lua index 7130fd663..342235c74 100644 --- a/lua/avante/selection.lua +++ b/lua/avante/selection.lua @@ -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, diff --git a/lua/avante/sidebar.lua b/lua/avante/sidebar.lua index 5ea5116e8..25c9c65ef 100644 --- a/lua/avante/sidebar.lua +++ b/lua/avante/sidebar.lua @@ -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 diff --git a/lua/avante/templates/_diagnostics.avanterules b/lua/avante/templates/_diagnostics.avanterules index 095907774..23c3be344 100644 --- a/lua/avante/templates/_diagnostics.avanterules +++ b/lua/avante/templates/_diagnostics.avanterules @@ -1,11 +1,29 @@ {%- if use_xml_format -%} {%- if diagnostics -%} + +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}} {%- 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 %} diff --git a/lua/avante/utils/init.lua b/lua/avante/utils/init.lua index 840ff850f..a84800123 100644 --- a/lua/avante/utils/init.lua +++ b/lua/avante/utils/init.lua @@ -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