Last active
December 9, 2022 18:21
-
-
Save phelipetls/0aeb9f4aca9af25d9f45ee56e0c5a340 to your computer and use it in GitHub Desktop.
Neovim built-in LSP diagnostics into location list
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local severity_map = { "E", "W", "I", "H" } | |
local parse_diagnostics = function(diagnostics) | |
if not diagnostics then return end | |
local items = {} | |
for _, diagnostic in ipairs(diagnostics) do | |
local fname = vim.fn.bufname() | |
local position = diagnostic.range.start | |
local severity = diagnostic.severity | |
table.insert(items, { | |
filename = fname, | |
type = severity_map[severity], | |
lnum = position.line + 1, | |
col = position.character + 1, | |
text = diagnostic.message:gsub("\r", ""):gsub("\n", " ") | |
}) | |
end | |
return items | |
end | |
-- redefine unwanted callbacks to be an empty function | |
-- notice that I keep `vim.lsp.util.buf_diagnostics_underline()` | |
vim.lsp.util.buf_diagnostics_signs = function() return end | |
vim.lsp.util.buf_diagnostics_virtual_text = function() return end | |
update_diagnostics_loclist = function() | |
bufnr = vim.fn.bufnr() | |
diagnostics = vim.lsp.util.diagnostics_by_buf[bufnr] | |
items = parse_diagnostics(diagnostics) | |
vim.lsp.util.set_loclist(items) | |
vim.api.nvim_command("doautocmd QuickFixCmdPost") | |
end | |
vim.api.nvim_command [[autocmd! User LspDiagnosticsChanged lua update_diagnostics_loclist()]] |
This is pretty old already, I believe you can just use vim.lsp.diagnostic.set_loclist()
. At least that's what I'm using and it works.
Awesome, thanks! That seems to work for me too.
From lsp documentation
vim.lsp.diagnostic.set_loclist() Use vim.diagnostic.setloclist() instead
Yes, it was added inside neovim api. One could write it as this snippet:
-- Populate loclist with the current buffer diagnostics
vim.api.nvim_create_autocmd('DiagnosticChanged', {
callback = function(args)
vim.diagnostic.setloclist({open = false})
end,
})
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for this! Using
NVIM v0.5.0-dev+94cf7bb
I had to changediagnostics = vim.lsp.util.diagnostics_by_buf[bufnr]
tovim.lsp.diagnostic.get()
.