2021-02-22 00:59:35 +00:00
|
|
|
-- Copyright (c) 2020-2021 hoob3rt
|
|
|
|
-- MIT license, see LICENSE for more details.
|
2021-08-30 11:54:23 +00:00
|
|
|
local lualine_require = require'lualine_require'
|
|
|
|
local modules = lualine_require.lazy_require{
|
2021-08-16 18:17:56 +00:00
|
|
|
highlight = 'lualine.highlight',
|
|
|
|
utils = 'lualine.utils.utils',
|
|
|
|
utils_notices = 'lualine.utils.notices',
|
|
|
|
}
|
2021-02-22 00:59:35 +00:00
|
|
|
|
2021-08-30 11:54:23 +00:00
|
|
|
local Diagnostics = lualine_require.require('lualine.component'):new()
|
2021-04-11 08:20:41 +00:00
|
|
|
|
2021-03-15 23:37:46 +00:00
|
|
|
-- LuaFormatter off
|
2021-04-11 08:20:41 +00:00
|
|
|
Diagnostics.default_colors = {
|
|
|
|
error = '#e32636',
|
|
|
|
warn = '#ffdf00',
|
|
|
|
info = '#ffffff',
|
2021-05-25 20:13:43 +00:00
|
|
|
hint = '#d7afaf',
|
2021-04-11 08:20:41 +00:00
|
|
|
}
|
2021-03-15 23:37:46 +00:00
|
|
|
-- LuaFormatter on
|
2021-02-22 00:59:35 +00:00
|
|
|
|
2021-04-11 08:20:41 +00:00
|
|
|
-- Initializer
|
|
|
|
Diagnostics.new = function(self, options, child)
|
|
|
|
local new_diagnostics = self._parent:new(options, child or Diagnostics)
|
|
|
|
local default_symbols = new_diagnostics.options.icons_enabled and {
|
|
|
|
error = ' ', -- xf659
|
|
|
|
warn = ' ', -- xf529
|
2021-05-25 20:13:43 +00:00
|
|
|
info = ' ', -- xf7fc
|
|
|
|
hint = ' ' -- xf838
|
|
|
|
} or {error = 'E:', warn = 'W:', info = 'I:', hint = 'H:'}
|
2021-04-11 08:20:41 +00:00
|
|
|
new_diagnostics.symbols = vim.tbl_extend('force', default_symbols,
|
|
|
|
new_diagnostics.options.symbols or {})
|
|
|
|
if new_diagnostics.options.sources == nil then
|
|
|
|
print('no sources for diagnostics configured')
|
|
|
|
return ''
|
|
|
|
end
|
|
|
|
if new_diagnostics.options.sections == nil then
|
2021-05-25 20:13:43 +00:00
|
|
|
new_diagnostics.options.sections = {'error', 'warn', 'info', 'hint'}
|
2021-04-11 08:20:41 +00:00
|
|
|
end
|
|
|
|
if new_diagnostics.options.colored == nil then
|
|
|
|
new_diagnostics.options.colored = true
|
|
|
|
end
|
2021-07-25 07:48:50 +00:00
|
|
|
if new_diagnostics.options.update_in_insert == nil then
|
|
|
|
new_diagnostics.options.update_in_insert = false
|
|
|
|
end
|
|
|
|
new_diagnostics.last_update = ''
|
2021-04-11 08:20:41 +00:00
|
|
|
-- apply colors
|
|
|
|
if not new_diagnostics.options.color_error then
|
2021-08-09 07:53:42 +00:00
|
|
|
new_diagnostics.options.color_error = {fg =
|
2021-08-16 18:17:56 +00:00
|
|
|
modules.utils.extract_highlight_colors('LspDiagnosticsDefaultError', 'fg') or
|
|
|
|
modules.utils.extract_highlight_colors('DiffDelete', 'fg') or
|
2021-08-09 07:53:42 +00:00
|
|
|
Diagnostics.default_colors.error }
|
2021-04-11 08:20:41 +00:00
|
|
|
end
|
|
|
|
if not new_diagnostics.options.color_warn then
|
2021-08-09 07:53:42 +00:00
|
|
|
new_diagnostics.options.color_warn = {fg =
|
2021-08-16 18:17:56 +00:00
|
|
|
modules.utils.extract_highlight_colors('LspDiagnosticsDefaultWarning', 'fg') or
|
|
|
|
modules.utils.extract_highlight_colors('DiffText', 'fg') or
|
2021-08-09 07:53:42 +00:00
|
|
|
Diagnostics.default_colors.warn }
|
2021-04-11 08:20:41 +00:00
|
|
|
end
|
|
|
|
if not new_diagnostics.options.color_info then
|
2021-08-09 07:53:42 +00:00
|
|
|
new_diagnostics.options.color_info = {fg =
|
2021-08-16 18:17:56 +00:00
|
|
|
modules.utils.extract_highlight_colors('LspDiagnosticsDefaultInformation', 'fg') or
|
|
|
|
modules.utils.extract_highlight_colors('Normal', 'fg') or
|
2021-08-09 07:53:42 +00:00
|
|
|
Diagnostics.default_colors.info}
|
2021-04-11 08:20:41 +00:00
|
|
|
end
|
2021-05-25 20:13:43 +00:00
|
|
|
if not new_diagnostics.options.color_hint then
|
2021-08-09 07:53:42 +00:00
|
|
|
new_diagnostics.options.color_hint = {fg =
|
2021-08-16 18:17:56 +00:00
|
|
|
modules.utils.extract_highlight_colors('LspDiagnosticsDefaultHint', 'fg') or
|
|
|
|
modules.utils.extract_highlight_colors('DiffChange', 'fg') or
|
2021-08-09 07:53:42 +00:00
|
|
|
Diagnostics.default_colors.hint}
|
2021-05-25 20:13:43 +00:00
|
|
|
end
|
2021-04-11 08:20:41 +00:00
|
|
|
|
|
|
|
if new_diagnostics.options.colored then
|
|
|
|
new_diagnostics.highlight_groups = {
|
2021-08-16 18:17:56 +00:00
|
|
|
error = modules.highlight.create_component_highlight_group(
|
2021-08-09 07:53:42 +00:00
|
|
|
new_diagnostics.options.color_error, 'diagnostics_error',
|
2021-04-11 08:20:41 +00:00
|
|
|
new_diagnostics.options),
|
2021-08-16 18:17:56 +00:00
|
|
|
warn = modules.highlight.create_component_highlight_group(
|
2021-08-09 07:53:42 +00:00
|
|
|
new_diagnostics.options.color_warn, 'diagnostics_warn',
|
2021-04-11 08:20:41 +00:00
|
|
|
new_diagnostics.options),
|
2021-08-16 18:17:56 +00:00
|
|
|
info = modules.highlight.create_component_highlight_group(
|
2021-08-09 07:53:42 +00:00
|
|
|
new_diagnostics.options.color_info, 'diagnostics_info',
|
2021-05-25 20:13:43 +00:00
|
|
|
new_diagnostics.options),
|
2021-08-16 18:17:56 +00:00
|
|
|
hint = modules.highlight.create_component_highlight_group(
|
2021-08-09 07:53:42 +00:00
|
|
|
new_diagnostics.options.color_hint, 'diagnostics_hint',
|
2021-04-11 08:20:41 +00:00
|
|
|
new_diagnostics.options)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
return new_diagnostics
|
|
|
|
end
|
|
|
|
|
|
|
|
Diagnostics.update_status = function(self)
|
2021-07-25 07:48:50 +00:00
|
|
|
if not self.options.update_in_insert
|
|
|
|
and vim.api.nvim_get_mode().mode:sub(1,1) == 'i' then
|
|
|
|
return self.last_update
|
|
|
|
end
|
2021-05-25 20:13:43 +00:00
|
|
|
local error_count, warning_count, info_count, hint_count = 0, 0, 0, 0
|
2021-04-11 08:20:41 +00:00
|
|
|
local diagnostic_data = self.get_diagnostics(self.options.sources)
|
|
|
|
for _, data in pairs(diagnostic_data) do
|
|
|
|
error_count = error_count + data.error
|
|
|
|
warning_count = warning_count + data.warn
|
|
|
|
info_count = info_count + data.info
|
2021-05-25 20:13:43 +00:00
|
|
|
hint_count = hint_count + data.hint
|
2021-04-11 08:20:41 +00:00
|
|
|
end
|
|
|
|
local result = {}
|
2021-05-25 20:13:43 +00:00
|
|
|
local data = {
|
|
|
|
error = error_count,
|
|
|
|
warn = warning_count,
|
|
|
|
info = info_count,
|
|
|
|
hint = hint_count
|
|
|
|
}
|
2021-04-11 08:20:41 +00:00
|
|
|
if self.options.colored then
|
|
|
|
local colors = {}
|
|
|
|
for name, hl in pairs(self.highlight_groups) do
|
2021-08-16 18:17:56 +00:00
|
|
|
colors[name] = modules.highlight.component_format_highlight(hl)
|
2021-04-11 08:20:41 +00:00
|
|
|
end
|
|
|
|
for _, section in ipairs(self.options.sections) do
|
|
|
|
if data[section] ~= nil and data[section] > 0 then
|
|
|
|
table.insert(result,
|
|
|
|
colors[section] .. self.symbols[section] .. data[section])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
for _, section in ipairs(self.options.sections) do
|
|
|
|
if data[section] ~= nil and data[section] > 0 then
|
|
|
|
table.insert(result, self.symbols[section] .. data[section])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2021-07-25 07:48:50 +00:00
|
|
|
self.last_update = ''
|
2021-04-11 08:20:41 +00:00
|
|
|
if result[1] ~= nil then
|
2021-07-25 07:48:50 +00:00
|
|
|
self.last_update = table.concat(result, ' ')
|
2021-04-11 08:20:41 +00:00
|
|
|
end
|
2021-07-25 07:48:50 +00:00
|
|
|
return self.last_update
|
2021-04-11 08:20:41 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
Diagnostics.diagnostic_sources = {
|
2021-02-22 00:59:35 +00:00
|
|
|
nvim_lsp = function()
|
|
|
|
local error_count = vim.lsp.diagnostic.get_count(0, 'Error')
|
|
|
|
local warning_count = vim.lsp.diagnostic.get_count(0, 'Warning')
|
2021-05-25 20:13:43 +00:00
|
|
|
local info_count = vim.lsp.diagnostic.get_count(0, 'Information')
|
|
|
|
local hint_count = vim.lsp.diagnostic.get_count(0, 'Hint')
|
|
|
|
return error_count, warning_count, info_count, hint_count
|
2021-02-22 00:59:35 +00:00
|
|
|
end,
|
|
|
|
coc = function()
|
|
|
|
local data = vim.b.coc_diagnostic_info
|
|
|
|
if data then
|
2021-05-25 20:13:43 +00:00
|
|
|
return data.error, data.warning, data.information, data.hint
|
2021-02-22 00:59:35 +00:00
|
|
|
else
|
2021-05-25 20:13:43 +00:00
|
|
|
return 0, 0, 0, 0
|
2021-02-22 00:59:35 +00:00
|
|
|
end
|
|
|
|
end,
|
|
|
|
ale = function()
|
|
|
|
local ok, data = pcall(vim.fn['ale#statusline#Count'], vim.fn.bufnr())
|
|
|
|
if ok then
|
2021-05-25 20:13:43 +00:00
|
|
|
return data.error + data.style_error, data.warning + data.style_warning, data.info, 0
|
2021-02-22 00:59:35 +00:00
|
|
|
else
|
2021-05-25 20:13:43 +00:00
|
|
|
return 0, 0, 0, 0
|
2021-02-22 00:59:35 +00:00
|
|
|
end
|
2021-03-15 23:37:46 +00:00
|
|
|
end
|
2021-02-22 00:59:35 +00:00
|
|
|
}
|
|
|
|
|
2021-04-11 08:20:41 +00:00
|
|
|
Diagnostics.get_diagnostics = function(sources)
|
2021-02-22 00:59:35 +00:00
|
|
|
local result = {}
|
|
|
|
for index, source in ipairs(sources) do
|
2021-08-16 18:17:56 +00:00
|
|
|
if type(source) == 'string' then
|
|
|
|
local error_count, warning_count, info_count, hint_count =
|
|
|
|
Diagnostics.diagnostic_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.warning or 0,
|
|
|
|
info = source_result.info or 0,
|
|
|
|
hint = source_result.hin or 0
|
|
|
|
}
|
|
|
|
end
|
2021-02-22 00:59:35 +00:00
|
|
|
end
|
|
|
|
return result
|
|
|
|
end
|
|
|
|
|
2021-04-11 08:20:41 +00:00
|
|
|
return Diagnostics
|