diff options
author | Jeremy Fleischman <jeremyfleischman@gmail.com> | 2024-12-04 07:49:57 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-12-04 07:49:57 -0800 |
commit | 7579af3c514c44581fe33b5c03660cdfda7d658e (patch) | |
tree | c533c11c82acfc6aee6502ee6ce313a43132aeaa /runtime/lua/vim/diagnostic.lua | |
parent | e2a91876ac61b4265e62a73ea9aed37597976b85 (diff) | |
download | rneovim-7579af3c514c44581fe33b5c03660cdfda7d658e.tar.gz rneovim-7579af3c514c44581fe33b5c03660cdfda7d658e.tar.bz2 rneovim-7579af3c514c44581fe33b5c03660cdfda7d658e.zip |
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.
Diffstat (limited to 'runtime/lua/vim/diagnostic.lua')
-rw-r--r-- | runtime/lua/vim/diagnostic.lua | 21 |
1 files changed, 19 insertions, 2 deletions
diff --git a/runtime/lua/vim/diagnostic.lua b/runtime/lua/vim/diagnostic.lua index 2de996feeb..dbf4f56032 100644 --- a/runtime/lua/vim/diagnostic.lua +++ b/runtime/lua/vim/diagnostic.lua @@ -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') |