feat: Allow function as diagnostics source

This commit is contained in:
shadmansaleh 2021-08-17 00:17:56 +06:00
parent 0126ac0cc0
commit 33defeffb5
3 changed files with 46 additions and 29 deletions

View File

@ -296,8 +296,10 @@ sections = {
{ {
'diagnostics', 'diagnostics',
-- table of diagnostic sources, available sources: -- table of diagnostic sources, available sources:
-- nvim_lsp, coc, ale, vim_lsp -- 'nvim_lsp', 'coc', 'ale', 'vim_lsp'
sources = nil, -- Or a function that returns a table like
-- {error=error_cnt, warning=warn_cnt, info=info_cnt, hint=hint_cnt}
sources = {},
-- displays diagnostics from defined severity -- displays diagnostics from defined severity
sections = {'error', 'warn', 'info', 'hint'}, sections = {'error', 'warn', 'info', 'hint'},
-- all colors are in format #rrggbb -- all colors are in format #rrggbb

View File

@ -229,8 +229,10 @@ Component specific local options~
{ {
'diagnostics', 'diagnostics',
-- table of diagnostic sources, available sources: -- table of diagnostic sources, available sources:
-- nvim_lsp, coc, ale, vim_lsp -- 'nvim_lsp', 'coc', 'ale', 'vim_lsp'
sources = nil, -- Or a function that returns a table like
-- {error=error_cnt, warning=warn_cnt, info=info_cnt, hint=hint_cnt}
sources = {},
-- displays diagnostics from defined severity -- displays diagnostics from defined severity
sections = {'error', 'warn', 'info', 'hint'}, sections = {'error', 'warn', 'info', 'hint'},
-- all colors are in format #rrggbb -- all colors are in format #rrggbb

View File

@ -1,8 +1,10 @@
-- Copyright (c) 2020-2021 hoob3rt -- Copyright (c) 2020-2021 hoob3rt
-- MIT license, see LICENSE for more details. -- MIT license, see LICENSE for more details.
local highlight = require('lualine.highlight') local modules = require('lualine.utils.lazy_require'){
local utils = require('lualine.utils.utils') highlight = 'lualine.highlight',
local utils_notices = require('lualine.utils.notices') utils = 'lualine.utils.utils',
utils_notices = 'lualine.utils.notices',
}
local Diagnostics = require('lualine.component'):new() local Diagnostics = require('lualine.component'):new()
@ -16,7 +18,7 @@ Diagnostics.default_colors = {
-- LuaFormatter on -- LuaFormatter on
local function color_deprecation_notice(color, opt_name) local function color_deprecation_notice(color, opt_name)
utils_notices.add_notice(string.format([[ modules.utils_notices.add_notice(string.format([[
### Diagnostics component ### Diagnostics component
Using option `%s` as string to set foreground color has been deprecated Using option `%s` as string to set foreground color has been deprecated
and will soon be removed. Now this option has same semantics as regular and will soon be removed. Now this option has same semantics as regular
@ -71,8 +73,8 @@ Diagnostics.new = function(self, options, child)
-- apply colors -- apply colors
if not new_diagnostics.options.color_error then if not new_diagnostics.options.color_error then
new_diagnostics.options.color_error = {fg = new_diagnostics.options.color_error = {fg =
utils.extract_highlight_colors('LspDiagnosticsDefaultError', 'fg') or modules.utils.extract_highlight_colors('LspDiagnosticsDefaultError', 'fg') or
utils.extract_highlight_colors('DiffDelete', 'fg') or modules.utils.extract_highlight_colors('DiffDelete', 'fg') or
Diagnostics.default_colors.error } Diagnostics.default_colors.error }
elseif type(new_diagnostics.options.color_error) == 'string' elseif type(new_diagnostics.options.color_error) == 'string'
and vim.fn.hlexists(new_diagnostics.options.color_error) == 0 then and vim.fn.hlexists(new_diagnostics.options.color_error) == 0 then
@ -81,8 +83,8 @@ Diagnostics.new = function(self, options, child)
end end
if not new_diagnostics.options.color_warn then if not new_diagnostics.options.color_warn then
new_diagnostics.options.color_warn = {fg = new_diagnostics.options.color_warn = {fg =
utils.extract_highlight_colors('LspDiagnosticsDefaultWarning', 'fg') or modules.utils.extract_highlight_colors('LspDiagnosticsDefaultWarning', 'fg') or
utils.extract_highlight_colors('DiffText', 'fg') or modules.utils.extract_highlight_colors('DiffText', 'fg') or
Diagnostics.default_colors.warn } Diagnostics.default_colors.warn }
elseif type(new_diagnostics.options.color_warn) == 'string' elseif type(new_diagnostics.options.color_warn) == 'string'
and vim.fn.hlexists(new_diagnostics.options.color_warn) == 0 then and vim.fn.hlexists(new_diagnostics.options.color_warn) == 0 then
@ -91,8 +93,8 @@ Diagnostics.new = function(self, options, child)
end end
if not new_diagnostics.options.color_info then if not new_diagnostics.options.color_info then
new_diagnostics.options.color_info = {fg = new_diagnostics.options.color_info = {fg =
utils.extract_highlight_colors('LspDiagnosticsDefaultInformation', 'fg') or modules.utils.extract_highlight_colors('LspDiagnosticsDefaultInformation', 'fg') or
utils.extract_highlight_colors('Normal', 'fg') or modules.utils.extract_highlight_colors('Normal', 'fg') or
Diagnostics.default_colors.info} Diagnostics.default_colors.info}
elseif type(new_diagnostics.options.color_info) == 'string' elseif type(new_diagnostics.options.color_info) == 'string'
and vim.fn.hlexists(new_diagnostics.options.color_info) == 0 then and vim.fn.hlexists(new_diagnostics.options.color_info) == 0 then
@ -101,8 +103,8 @@ Diagnostics.new = function(self, options, child)
end end
if not new_diagnostics.options.color_hint then if not new_diagnostics.options.color_hint then
new_diagnostics.options.color_hint = {fg = new_diagnostics.options.color_hint = {fg =
utils.extract_highlight_colors('LspDiagnosticsDefaultHint', 'fg') or modules.utils.extract_highlight_colors('LspDiagnosticsDefaultHint', 'fg') or
utils.extract_highlight_colors('DiffChange', 'fg') or modules.utils.extract_highlight_colors('DiffChange', 'fg') or
Diagnostics.default_colors.hint} Diagnostics.default_colors.hint}
elseif type(new_diagnostics.options.color_hint) == 'string' elseif type(new_diagnostics.options.color_hint) == 'string'
and vim.fn.hlexists(new_diagnostics.options.color_hint) == 0 then and vim.fn.hlexists(new_diagnostics.options.color_hint) == 0 then
@ -112,16 +114,16 @@ Diagnostics.new = function(self, options, child)
if new_diagnostics.options.colored then if new_diagnostics.options.colored then
new_diagnostics.highlight_groups = { new_diagnostics.highlight_groups = {
error = highlight.create_component_highlight_group( error = modules.highlight.create_component_highlight_group(
new_diagnostics.options.color_error, 'diagnostics_error', new_diagnostics.options.color_error, 'diagnostics_error',
new_diagnostics.options), new_diagnostics.options),
warn = highlight.create_component_highlight_group( warn = modules.highlight.create_component_highlight_group(
new_diagnostics.options.color_warn, 'diagnostics_warn', new_diagnostics.options.color_warn, 'diagnostics_warn',
new_diagnostics.options), new_diagnostics.options),
info = highlight.create_component_highlight_group( info = modules.highlight.create_component_highlight_group(
new_diagnostics.options.color_info, 'diagnostics_info', new_diagnostics.options.color_info, 'diagnostics_info',
new_diagnostics.options), new_diagnostics.options),
hint = highlight.create_component_highlight_group( hint = modules.highlight.create_component_highlight_group(
new_diagnostics.options.color_hint, 'diagnostics_hint', new_diagnostics.options.color_hint, 'diagnostics_hint',
new_diagnostics.options) new_diagnostics.options)
} }
@ -153,7 +155,7 @@ Diagnostics.update_status = function(self)
if self.options.colored then if self.options.colored then
local colors = {} local colors = {}
for name, hl in pairs(self.highlight_groups) do for name, hl in pairs(self.highlight_groups) do
colors[name] = highlight.component_format_highlight(hl) colors[name] = modules.highlight.component_format_highlight(hl)
end end
for _, section in ipairs(self.options.sections) do for _, section in ipairs(self.options.sections) do
if data[section] ~= nil and data[section] > 0 then if data[section] ~= nil and data[section] > 0 then
@ -204,6 +206,7 @@ Diagnostics.diagnostic_sources = {
Diagnostics.get_diagnostics = function(sources) Diagnostics.get_diagnostics = function(sources)
local result = {} local result = {}
for index, source in ipairs(sources) do for index, source in ipairs(sources) do
if type(source) == 'string' then
local error_count, warning_count, info_count, hint_count = local error_count, warning_count, info_count, hint_count =
Diagnostics.diagnostic_sources[source]() Diagnostics.diagnostic_sources[source]()
result[index] = { result[index] = {
@ -212,6 +215,16 @@ Diagnostics.get_diagnostics = function(sources)
info = info_count, info = info_count,
hint = hint_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
end end
return result return result
end end