-- Jump to next warning or errorvim.diagnostic.goto_next({ severity = { min = vim.diagnostic.severity.WARN }})-- Jump to next diagnostic with a custom message filtervim.diagnostic.goto_next({ severity = vim.diagnostic.severity.ERROR, float = true, -- Show float automatically})-- Wrap around when reaching the last diagnosticvim.diagnostic.goto_next({ wrap = true })
-- Get only errorslocal errors = vim.diagnostic.get(0, { severity = vim.diagnostic.severity.ERROR})-- Get errors and warningslocal errors_and_warnings = vim.diagnostic.get(0, { severity = { min = vim.diagnostic.severity.WARN, max = vim.diagnostic.severity.ERROR }})
Filter diagnostics from specific language servers:
-- Get diagnostics from a specific namespacelocal client = vim.lsp.get_clients({ name = 'lua_ls' })[1]if client then local ns = vim.lsp.diagnostic.get_namespace(client.id) local diagnostics = vim.diagnostic.get(0, { namespace = ns })end
The LSP client automatically handles pull diagnostics if the server supports it. Manually refresh:
-- Force refresh diagnostics for current buffervim.lsp.diagnostic._refresh(0)-- Refresh for all buffers attached to a clientvim.api.nvim_create_autocmd('LspNotify', { callback = function(args) if args.data.method == 'textDocument/didSave' then vim.lsp.diagnostic._refresh(args.buf) end end})
vim.lsp.handlers['textDocument/publishDiagnostics'] = function(err, result, ctx, config) -- Filter out certain diagnostics if result and result.diagnostics then result.diagnostics = vim.tbl_filter(function(d) -- Ignore "unused variable" warnings return not (d.message:match('unused') or d.message:match('never read')) end, result.diagnostics) end -- Call default handler vim.lsp.diagnostic.on_publish_diagnostics(err, result, ctx, config)end
Populate quickfix or location list with diagnostics:
-- All diagnostics to quickfixvim.diagnostic.setqflist()-- Only errors to quickfixvim.diagnostic.setqflist({ severity = vim.diagnostic.severity.ERROR })-- Diagnostics for current buffer to location listvim.diagnostic.setloclist()-- Custom titlevim.diagnostic.setqflist({ open = true, title = 'LSP Diagnostics' })