Skip to content

Instantly share code, notes, and snippets.

@hachy
Created April 12, 2022 05:49
Show Gist options
  • Save hachy/e285d037e188f7232bd4c30f376d6e2b to your computer and use it in GitHub Desktop.
Save hachy/e285d037e188f7232bd4c30f376d6e2b to your computer and use it in GitHub Desktop.
Use PrintDiagnostics with highlight in nvim-lspconfig
local hl_tbl = {
[vim.diagnostic.severity.ERROR] = "DiagnosticError",
[vim.diagnostic.severity.WARN] = "DiagnosticWarn",
[vim.diagnostic.severity.HINT] = "DiagnosticHint",
[vim.diagnostic.severity.INFO] = "DiagnosticInfo",
}
function PrintDiagnostics(opts, bufnr, line_nr, client_id)
bufnr = bufnr or 0
line_nr = line_nr or (vim.api.nvim_win_get_cursor(0)[1] - 1)
opts = opts or {['lnum'] = line_nr}
local line_diagnostics = vim.diagnostic.get(bufnr, opts)
if vim.tbl_isempty(line_diagnostics) then return end
local diagnostic_message = ""
local hl = "Normal"
for i, diagnostic in ipairs(line_diagnostics) do
diagnostic_message = diagnostic_message .. string.format("%d: %s", i, diagnostic.message or "")
if i ~= #line_diagnostics then
diagnostic_message = diagnostic_message .. "\n"
end
hl=hl_tbl[diagnostic.severity]
end
vim.api.nvim_echo({{diagnostic_message, hl}}, false, {})
end
vim.cmd [[ autocmd! CursorHold * lua PrintDiagnostics() ]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment