Skip to content

Instantly share code, notes, and snippets.

@phelipetls
Last active December 9, 2022 18:21
Show Gist options
  • Save phelipetls/0aeb9f4aca9af25d9f45ee56e0c5a340 to your computer and use it in GitHub Desktop.
Save phelipetls/0aeb9f4aca9af25d9f45ee56e0c5a340 to your computer and use it in GitHub Desktop.
Neovim built-in LSP diagnostics into location list
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()]]
@phelipetls
Copy link
Author

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.

@andreit
Copy link

andreit commented Apr 1, 2021

Awesome, thanks! That seems to work for me too.

@pavannaganna
Copy link

From lsp documentation

vim.lsp.diagnostic.set_loclist()        Use vim.diagnostic.setloclist() instead

@hpaul
Copy link

hpaul commented Dec 9, 2022

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