feat(diagnostic): vim.diagnostic.setqflist improvements #30868

1. Use the new "u" action to update the quickfix list so we don't lose
   our position in the quickfix list when updating it.
2. Rather than creating a new quickfix list each time, update the
   exiting one if we've already created one.
This commit is contained in:
Jeremy Fleischman 2024-12-04 07:49:57 -08:00 committed by GitHub
parent e2a91876ac
commit 7579af3c51
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 21 additions and 2 deletions

View File

@ -303,6 +303,8 @@ UI
|hl-PmenuSel| and |hl-PmenuMatch| both inherit from |hl-Pmenu|, and
|hl-PmenuMatchSel| inherits highlights from both |hl-PmenuSel| and
|hl-PmenuMatch|.
• |vim.diagnostic.setqflist()| updates existing diagnostics quickfix list if one
exists.
• |ui-messages| content chunks now also contain the highlight group ID.

View File

@ -2,6 +2,8 @@ local api, if_nil = vim.api, vim.F.if_nil
local M = {}
local _qf_id = nil
--- [diagnostic-structure]()
---
--- Diagnostics use the same indexing as the rest of the Nvim API (i.e. 0-based
@ -848,9 +850,24 @@ local function set_list(loclist, opts)
local diagnostics = get_diagnostics(bufnr, opts --[[@as vim.diagnostic.GetOpts]], false)
local items = M.toqflist(diagnostics)
if loclist then
vim.fn.setloclist(winnr, {}, ' ', { title = title, items = items })
vim.fn.setloclist(winnr, {}, 'u', { title = title, items = items })
else
vim.fn.setqflist({}, ' ', { title = title, items = items })
-- Check if the diagnostics quickfix list no longer exists.
if _qf_id and vim.fn.getqflist({ id = _qf_id }).id == 0 then
_qf_id = nil
end
-- If we already have a diagnostics quickfix, update it rather than creating a new one.
-- This avoids polluting the finite set of quickfix lists, and preserves the currently selected
-- entry.
vim.fn.setqflist({}, _qf_id and 'u' or ' ', {
title = title,
items = items,
id = _qf_id,
})
-- Get the id of the newly created quickfix list.
_qf_id = vim.fn.getqflist({ id = 0 }).id
end
if open then
api.nvim_command(loclist and 'lwindow' or 'botright cwindow')