2021-02-22 00:59:35 +00:00
|
|
|
-- Copyright (c) 2020-2021 hoob3rt
|
|
|
|
-- MIT license, see LICENSE for more details.
|
2021-09-03 18:28:20 +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-09-25 17:15:42 +00:00
|
|
|
local M = lualine_require.require 'lualine.component':extend()
|
2021-04-11 08:20:41 +00:00
|
|
|
|
2021-09-03 12:04:48 +00:00
|
|
|
local default_symbols = {
|
|
|
|
icons = {
|
2021-04-11 08:20:41 +00:00
|
|
|
error = ' ', -- xf659
|
|
|
|
warn = ' ', -- xf529
|
2021-05-25 20:13:43 +00:00
|
|
|
info = ' ', -- xf7fc
|
2021-09-03 18:28:20 +00:00
|
|
|
hint = ' ', -- xf838
|
2021-09-03 12:04:48 +00:00
|
|
|
},
|
2021-09-03 18:28:20 +00:00
|
|
|
no_icons = { error = 'E:', warn = 'W:', info = 'I:', hint = 'H:' },
|
2021-09-03 12:04:48 +00:00
|
|
|
}
|
2021-04-11 08:20:41 +00:00
|
|
|
|
2021-09-03 12:04:48 +00:00
|
|
|
local default_options = {
|
|
|
|
colored = true,
|
|
|
|
update_in_insert = false,
|
2021-09-21 06:26:19 +00:00
|
|
|
sources = { 'nvim_lsp', 'coc' },
|
2021-09-03 18:28:20 +00:00
|
|
|
sections = { 'error', 'warn', 'info', 'hint' },
|
2021-09-17 08:40:10 +00:00
|
|
|
diagnostics_color = {
|
2021-09-14 15:14:23 +00:00
|
|
|
error = {
|
2021-09-17 09:16:38 +00:00
|
|
|
fg = modules.utils.extract_color_from_hllist(
|
|
|
|
'fg',
|
|
|
|
{ 'DiagnosticError', 'LspDiagnosticsDefaultError', 'DiffDelete' },
|
|
|
|
'#e32636'
|
|
|
|
),
|
2021-09-14 15:14:23 +00:00
|
|
|
},
|
|
|
|
warn = {
|
2021-09-17 09:16:38 +00:00
|
|
|
fg = modules.utils.extract_color_from_hllist(
|
|
|
|
'fg',
|
|
|
|
{ 'DiagnosticWarn', 'LspDiagnosticsDefaultWarning', 'DiffText' },
|
|
|
|
'#ffa500'
|
|
|
|
),
|
2021-09-14 15:14:23 +00:00
|
|
|
},
|
|
|
|
info = {
|
2021-09-17 09:16:38 +00:00
|
|
|
fg = modules.utils.extract_color_from_hllist(
|
|
|
|
'fg',
|
|
|
|
{ 'DiagnosticInfo', 'LspDiagnosticsDefaultInformation', 'Normal' },
|
|
|
|
'#ffffff'
|
|
|
|
),
|
2021-09-14 15:14:23 +00:00
|
|
|
},
|
|
|
|
hint = {
|
2021-09-17 09:16:38 +00:00
|
|
|
fg = modules.utils.extract_color_from_hllist(
|
|
|
|
'fg',
|
|
|
|
{ 'DiagnosticHint', 'LspDiagnosticsDefaultHint', 'DiffChange' },
|
|
|
|
'#273faf'
|
|
|
|
),
|
2021-09-14 15:14:23 +00:00
|
|
|
},
|
2021-09-03 12:04:48 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
-- Initializer
|
2021-09-25 17:15:42 +00:00
|
|
|
function M:init(options)
|
2021-09-03 12:04:48 +00:00
|
|
|
-- Run super()
|
2021-09-25 17:15:42 +00:00
|
|
|
M.super.init(self, options)
|
2021-09-03 12:04:48 +00:00
|
|
|
-- Apply default options
|
2021-09-25 17:15:42 +00:00
|
|
|
self.options = vim.tbl_deep_extend('keep', self.options or {}, default_options)
|
2021-09-03 12:04:48 +00:00
|
|
|
-- Apply default symbols
|
2021-09-25 17:15:42 +00:00
|
|
|
self.symbols = vim.tbl_extend(
|
2021-09-03 18:28:20 +00:00
|
|
|
'keep',
|
2021-09-25 17:15:42 +00:00
|
|
|
self.options.symbols or {},
|
|
|
|
self.options.icons_enabled ~= false and default_symbols.icons or default_symbols.no_icons
|
2021-09-03 18:28:20 +00:00
|
|
|
)
|
2021-09-03 12:04:48 +00:00
|
|
|
-- Initialize highlight groups
|
2021-09-25 17:15:42 +00:00
|
|
|
if self.options.colored then
|
|
|
|
self.highlight_groups = {
|
2021-08-16 18:17:56 +00:00
|
|
|
error = modules.highlight.create_component_highlight_group(
|
2021-09-25 17:15:42 +00:00
|
|
|
self.options.diagnostics_color.error,
|
2021-09-03 18:28:20 +00:00
|
|
|
'diagnostics_error',
|
2021-09-25 17:15:42 +00:00
|
|
|
self.options
|
2021-09-03 18:28:20 +00:00
|
|
|
),
|
2021-08-16 18:17:56 +00:00
|
|
|
warn = modules.highlight.create_component_highlight_group(
|
2021-09-25 17:15:42 +00:00
|
|
|
self.options.diagnostics_color.warn,
|
2021-09-03 18:28:20 +00:00
|
|
|
'diagnostics_warn',
|
2021-09-25 17:15:42 +00:00
|
|
|
self.options
|
2021-09-03 18:28:20 +00:00
|
|
|
),
|
2021-08-16 18:17:56 +00:00
|
|
|
info = modules.highlight.create_component_highlight_group(
|
2021-09-25 17:15:42 +00:00
|
|
|
self.options.diagnostics_color.info,
|
2021-09-03 18:28:20 +00:00
|
|
|
'diagnostics_info',
|
2021-09-25 17:15:42 +00:00
|
|
|
self.options
|
2021-09-03 18:28:20 +00:00
|
|
|
),
|
2021-08-16 18:17:56 +00:00
|
|
|
hint = modules.highlight.create_component_highlight_group(
|
2021-09-25 17:15:42 +00:00
|
|
|
self.options.diagnostics_color.hint,
|
2021-09-03 18:28:20 +00:00
|
|
|
'diagnostics_hint',
|
2021-09-25 17:15:42 +00:00
|
|
|
self.options
|
2021-09-03 18:28:20 +00:00
|
|
|
),
|
2021-04-11 08:20:41 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2021-09-03 12:04:48 +00:00
|
|
|
-- Error out no source
|
2021-09-25 17:15:42 +00:00
|
|
|
if #self.options.sources < 1 then
|
2021-09-03 18:28:20 +00:00
|
|
|
print 'no sources for diagnostics configured'
|
2021-09-03 12:04:48 +00:00
|
|
|
return ''
|
|
|
|
end
|
|
|
|
-- Initialize variable to store last update so we can use it in insert
|
|
|
|
-- mode for no update_in_insert
|
2021-09-25 17:15:42 +00:00
|
|
|
self.last_update = ''
|
2021-04-11 08:20:41 +00:00
|
|
|
end
|
|
|
|
|
2021-09-25 17:15:42 +00:00
|
|
|
function M:update_status()
|
2021-09-03 18:28:20 +00:00
|
|
|
if not self.options.update_in_insert and vim.api.nvim_get_mode().mode:sub(1, 1) == 'i' then
|
2021-07-25 07:48:50 +00:00
|
|
|
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,
|
2021-09-03 18:28:20 +00:00
|
|
|
hint = hint_count,
|
2021-05-25 20:13:43 +00:00
|
|
|
}
|
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
|
2021-09-03 18:28:20 +00:00
|
|
|
table.insert(result, colors[section] .. self.symbols[section] .. data[section])
|
2021-04-11 08:20:41 +00:00
|
|
|
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
|
|
|
|
|
2021-09-25 17:15:42 +00:00
|
|
|
M.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,
|
2021-09-17 12:50:32 +00:00
|
|
|
nvim = 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,
|
2021-02-22 00:59:35 +00:00
|
|
|
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-09-03 18:28:20 +00:00
|
|
|
end,
|
2021-09-17 12:50:32 +00:00
|
|
|
vim_lsp = function()
|
|
|
|
local ok, data = pcall(vim.fn['lsp#get_buffer_diagnostics_counts'])
|
|
|
|
if ok then
|
|
|
|
return data.error, data.warning, data.information
|
|
|
|
else
|
|
|
|
return 0, 0, 0
|
|
|
|
end
|
|
|
|
end,
|
2021-02-22 00:59:35 +00:00
|
|
|
}
|
|
|
|
|
2021-09-25 17:15:42 +00:00
|
|
|
M.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
|
2021-09-25 17:15:42 +00:00
|
|
|
local error_count, warning_count, info_count, hint_count = M.diagnostic_sources[source]()
|
2021-08-16 18:17:56 +00:00
|
|
|
result[index] = {
|
|
|
|
error = error_count,
|
|
|
|
warn = warning_count,
|
|
|
|
info = info_count,
|
2021-09-03 18:28:20 +00:00
|
|
|
hint = hint_count,
|
2021-08-16 18:17:56 +00:00
|
|
|
}
|
|
|
|
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,
|
2021-09-17 08:40:10 +00:00
|
|
|
warn = source_result.warn or 0,
|
2021-08-16 18:17:56 +00:00
|
|
|
info = source_result.info or 0,
|
2021-09-17 08:40:10 +00:00
|
|
|
hint = source_result.hint or 0,
|
2021-08-16 18:17:56 +00:00
|
|
|
}
|
|
|
|
end
|
2021-02-22 00:59:35 +00:00
|
|
|
end
|
|
|
|
return result
|
|
|
|
end
|
|
|
|
|
2021-09-25 17:15:42 +00:00
|
|
|
return M
|