shadmansaleh 70691ae350 enhance: undeprecate nvim_lsp and completely remove nvim diagnostics source 😉
- Now `nvim_lsp` source shows diagnostics generated by vim.lsp only
  avoids uses vim.diagnostic to avoid deprecation warning on nvim 0.6+
  Why? `vim.lsp.diagnostic` deprecation warnings are way too aggressive.
  And I'm annoyed by people complaining here even though the warning is
  generated by some other plugin (After introduction of nvim_diagnostic
  source lualine just can't produce that warning it used to switch users
  to nvim_diagnostic source with a warning on nvim0.6+).
  Forget it just let anyone that wants lsp specific diagnostics have
  `nvim_lsp` . I'd still recommend using `nvim_diagnostic` over
  `nvim_lsp` as diagnostics generayed by plugins like `null-ls` won't
  showup in `nvim_lsp`
- Remove remanents of `nvim` diagnostics source.
2022-01-12 22:35:44 +06:00

99 lines
3.5 KiB
Lua

local M = {}
---functions that how how to retrieve diagnostics from specific source.
---returns error_count:number, warning_count:number,
--- info_count:number, hint_count:number
M.sources = {
nvim_lsp = function()
local error_count, warning_count, info_count, hint_count
if vim.fn.has('nvim-0.6') == 1 then
-- On nvim 0.6+ use vim.diagnostic to get lsp generated diagnostic count.
local diagnostics = vim.diagnostic.get(0)
local count = { 0, 0, 0, 0 }
for _, diagnostic in ipairs(diagnostics) do
if vim.startswith(vim.diagnostic.get_namespace(diagnostic.namespace).name, 'vim.lsp') then
count[diagnostic.severity] = count[diagnostic.severity] + 1
end
end
error_count = count[vim.diagnostic.severity.ERROR]
warning_count = count[vim.diagnostic.severity.WARN]
info_count = count[vim.diagnostic.severity.INFO]
hint_count = count[vim.diagnostic.severity.HINT]
else
-- On 0.5 use older vim.lsp.diagnostic module.
-- Maybe we should phase out support for 0.5 though I haven't yet found a solid reason to.
-- Eventually this will be removed when 0.5 is no longer supported.
error_count = vim.lsp.diagnostic.get_count(0, 'Error')
warning_count = vim.lsp.diagnostic.get_count(0, 'Warning')
info_count = vim.lsp.diagnostic.get_count(0, 'Information')
hint_count = vim.lsp.diagnostic.get_count(0, 'Hint')
end
return error_count, warning_count, info_count, hint_count
end,
nvim_diagnostic = function()
local diagnostics = vim.diagnostic.get(0)
local count = { 0, 0, 0, 0 }
for _, diagnostic in ipairs(diagnostics) do
count[diagnostic.severity] = count[diagnostic.severity] + 1
end
return count[vim.diagnostic.severity.ERROR],
count[vim.diagnostic.severity.WARN],
count[vim.diagnostic.severity.INFO],
count[vim.diagnostic.severity.HINT]
end,
coc = function()
local data = vim.b.coc_diagnostic_info
if data then
return data.error, data.warning, data.information, data.hint
else
return 0, 0, 0, 0
end
end,
ale = function()
local ok, data = pcall(vim.fn['ale#statusline#Count'], vim.api.nvim_get_current_buf())
if ok then
return data.error + data.style_error, data.warning + data.style_warning, data.info, 0
else
return 0, 0, 0, 0
end
end,
vim_lsp = function()
local ok, data = pcall(vim.fn['lsp#get_buffer_diagnostics_counts'])
if ok then
return data.error, data.warning, data.information, data.hint
else
return 0, 0, 0, 0
end
end,
}
---returns list of diagnostics count from all sources
---@param sources table list of sources
---@return table {{error_count, warning_count, info_count, hint_count}}
M.get_diagnostics = function(sources)
local result = {}
for index, source in ipairs(sources) do
if type(source) == 'string' then
local error_count, warning_count, info_count, hint_count = M.sources[source]()
result[index] = {
error = error_count,
warn = warning_count,
info = info_count,
hint = hint_count,
}
elseif type(source) == 'function' then
local source_result = source()
source_result = type(source_result) == 'table' and source_result or {}
result[index] = {
error = source_result.error or 0,
warn = source_result.warn or 0,
info = source_result.info or 0,
hint = source_result.hint or 0,
}
end
end
return result
end
return M