onsails / diaglist.nvim

Live render workspace diagnostics in quickfix with current buf errors on top, buffer diagnostics in loclist
184 stars 5 forks source link

Use title to determine wheather to update the quickfix/location list #3

Open piotr-machura opened 3 years ago

piotr-machura commented 3 years ago

The plugin sets a flag on QuickFixCmdPre to signify that the quickfix has been populated by something other than the diagnostics. This unfortunatelly conflicts with non-builtin functions utilizing quickfix (like vim.lsp.buf.workspace_symbol() or vim.lsp.buf.references()).

Both this and the current issue of not respecting foregin location lists can be fixed using setqflist() (and getqflist(), setloclist(), getloclist()), perhaps by specifying the lists' ID or even just the title. The lsp_diagnostic_hook can then check if current quickfix list has a specific title (say "Diagnostics") and only then update. If the user wants to force the update they can do so with open_all_diagnostics() or open_buffer_diagnostics().

I would try making a PR myself, but the above are vimscript functions and I have no clue how to nicely use them from lua.

piotr-machura commented 3 years ago

To expand on this: neovim 0.6 nightly fetaures a handy setqflist in addition to the already available setloclist, which adds workspace diagnostics to the quickfix list (similar to what this plugin does).

I am currently using something like this in my personal config:

function update_diagnostics(global_too)
    if not vim.lsp.buf.server_ready() then
        return
    end
    if vim.fn.getloclist(vim.fn.winnr(), { title = 0 }).title == 'Buffer diagnostics' then
        vim.diagnostic.setloclist{ open = false, title = 'Buffer diagnostics' }
    end
    if global_too and vim.fn.getqflist{ title = 0 }.title == 'Workspace diagnostics' then
        vim.diagnostic.setqflist{ open = false,  title = 'Workspace diagnostics' }
    end
end

and in my setup.on_attach callback I have

vim.api.nvim_command('autocmd BufWinEnter * lua update_diagnostics(false)')
vim.api.nvim_command('autocmd User DiagnosticsChanged lua update_diagnostics(true)')
vim.api.nvim_buf_set_keymap(0, 'n', '<C-k>k','<CMD>silent! lua vim.diagnostic.setloclist{ title = "Buffer diagnostics" }<CR>', {noremap=true})
vim.api.nvim_buf_set_keymap(0, 'n', '<C-k><C-k>','<CMD>silent! lua vim.diagnostic.setqflist{ title = "Workspace diagnostics" }<CR>', {noremap=true})

This allows me to use all kinds of non-standard quickfix/location lists (references, implementations, symbols etc.) and populate them with auto-updating diagnostics using a keymap.

Something similar (but much more polished) could be integrated into this plugin (although the "release" neovim 0.5.1 does not have setqflist yet).

onsails commented 2 years ago

Hi @piotr-machura , I finally found time to fully adapt plugin to 0.6.x and test your idea. Apparently it works well so master now uses title to identify foreign quickfix! Thank you for the great idea and wonderful examples.

Foreign loclist is still to be done.