Debian Codegraph @

lua/lualine/components/diagnostics/sources.lua

raw
1 local M = {}
2
3 ---functions that how how to retrieve diagnostics from specific source.
4 ---returns error_count:number, warning_count:number,
5 --- info_count:number, hint_count:number
6 M.sources = {
7 nvim_lsp = function()
8 local error_count, warning_count, info_count, hint_count
9 local diagnostics = vim.diagnostic.get(0)
10 local count = { 0, 0, 0, 0 }
11 for _, diagnostic in ipairs(diagnostics) do
12 if vim.startswith(vim.diagnostic.get_namespace(diagnostic.namespace).name, 'vim.lsp') then
13 count[diagnostic.severity] = count[diagnostic.severity] + 1
14 end
15 end
16 error_count = count[vim.diagnostic.severity.ERROR]
17 warning_count = count[vim.diagnostic.severity.WARN]
18 info_count = count[vim.diagnostic.severity.INFO]
19 hint_count = count[vim.diagnostic.severity.HINT]
20 return error_count, warning_count, info_count, hint_count
21 end,
22 nvim_workspace_diagnostic = function()
23 local diag_severity = vim.diagnostic.severity
24
25 local function workspace_diag(severity)
26 local count = vim.diagnostic.get(nil, { severity = severity })
27 return vim.tbl_count(count)
28 end
29
30 return workspace_diag(diag_severity.ERROR),
31 workspace_diag(diag_severity.WARN),
32 workspace_diag(diag_severity.INFO),
33 workspace_diag(diag_severity.HINT)
34 end,
35 nvim_diagnostic = function()
36 local count
37
38 if vim.diagnostic.count ~= nil then -- neovim >= 0.10.0
39 count = vim.diagnostic.count(0)
40 return count[vim.diagnostic.severity.ERROR] or 0,
41 count[vim.diagnostic.severity.WARN] or 0,
42 count[vim.diagnostic.severity.INFO] or 0,
43 count[vim.diagnostic.severity.HINT] or 0
44 end
45
46 -- fallback
47 local diagnostics = vim.diagnostic.get(0)
48 count = { 0, 0, 0, 0 }
49 for _, diagnostic in ipairs(diagnostics) do
50 count[diagnostic.severity] = count[diagnostic.severity] + 1
51 end
52 return count[vim.diagnostic.severity.ERROR],
53 count[vim.diagnostic.severity.WARN],
54 count[vim.diagnostic.severity.INFO],
55 count[vim.diagnostic.severity.HINT]
56 end,
57 coc = function()
58 local data = vim.b.coc_diagnostic_info
59 if data then
60 return data.error, data.warning, data.information, data.hint
61 else
62 return 0, 0, 0, 0
63 end
64 end,
65 ale = function()
66 local ok, data = pcall(vim.fn['ale#statusline#Count'], vim.api.nvim_get_current_buf())
67 if ok then
68 return data.error + data.style_error, data.warning + data.style_warning, data.info, 0
69 else
70 return 0, 0, 0, 0
71 end
72 end,
73 vim_lsp = function()
74 local ok, data = pcall(vim.fn['lsp#get_buffer_diagnostics_counts'])
75 if ok then
76 return data.error, data.warning, data.information, data.hint
77 else
78 return 0, 0, 0, 0
79 end
80 end,
81 }
82
83 ---returns list of diagnostics count from all sources
84 ---@param sources table list of sources
85 ---@return table {{error_count, warning_count, info_count, hint_count}}
86 M.get_diagnostics = function(sources)
87 local result = {}
88 for index, source in ipairs(sources) do
89 if type(source) == 'string' then
90 local error_count, warning_count, info_count, hint_count = M.sources[source]()
91 result[index] = {
92 error = error_count,
93 warn = warning_count,
94 info = info_count,
95 hint = hint_count,
96 }
97 elseif type(source) == 'function' then
98 local source_result = source()
99 source_result = type(source_result) == 'table' and source_result or {}
100 result[index] = {
101 error = source_result.error or 0,
102 warn = source_result.warn or 0,
103 info = source_result.info or 0,
104 hint = source_result.hint or 0,
105 }
106 end
107 end
108 return result
109 end
110
111 return M