| 1 |
-- Copyright (c) 2020-2021 hoob3rt |
| 2 |
-- MIT license, see LICENSE for more details. |
| 3 |
local lualine_require = require('lualine_require') |
| 4 |
local modules = lualine_require.lazy_require { |
| 5 |
default_config = 'lualine.components.diagnostics.config', |
| 6 |
sources = 'lualine.components.diagnostics.sources', |
| 7 |
highlight = 'lualine.highlight', |
| 8 |
utils = 'lualine.utils.utils', |
| 9 |
utils_notices = 'lualine.utils.notices', |
| 10 |
} |
| 11 |
|
| 12 |
local M = lualine_require.require('lualine.component'):extend() |
| 13 |
|
| 14 |
M.diagnostics_sources = modules.sources.sources |
| 15 |
M.get_diagnostics = modules.sources.get_diagnostics |
| 16 |
|
| 17 |
-- Initializer |
| 18 |
function M:init(options) |
| 19 |
-- Run super() |
| 20 |
M.super.init(self, options) |
| 21 |
-- Apply default options |
| 22 |
modules.default_config.apply_default_colors(self.options) |
| 23 |
self.options = vim.tbl_deep_extend('keep', self.options or {}, modules.default_config.options) |
| 24 |
-- Apply default symbols |
| 25 |
self.symbols = vim.tbl_extend( |
| 26 |
'keep', |
| 27 |
self.options.symbols or {}, |
| 28 |
self.options.icons_enabled ~= false and modules.default_config.symbols.icons |
| 29 |
or modules.default_config.symbols.no_icons |
| 30 |
) |
| 31 |
-- Initialize highlight groups |
| 32 |
if self.options.colored then |
| 33 |
self.highlight_groups = { |
| 34 |
error = self:create_hl(self.options.diagnostics_color.error, 'error'), |
| 35 |
warn = self:create_hl(self.options.diagnostics_color.warn, 'warn'), |
| 36 |
info = self:create_hl(self.options.diagnostics_color.info, 'info'), |
| 37 |
hint = self:create_hl(self.options.diagnostics_color.hint, 'hint'), |
| 38 |
} |
| 39 |
end |
| 40 |
|
| 41 |
-- Initialize variable to store last update so we can use it in insert |
| 42 |
-- mode for no update_in_insert |
| 43 |
self.last_diagnostics_count = {} |
| 44 |
|
| 45 |
-- Error out no source |
| 46 |
if #self.options.sources < 1 then |
| 47 |
modules.utils_notices.add_notice( |
| 48 |
'### diagnostics.sources\n\nno sources for diagnostics configured.\nPlease specify which diagnostics source you want lualine to use with `sources` option.\n' |
| 49 |
) |
| 50 |
end |
| 51 |
end |
| 52 |
|
| 53 |
function M:update_status() |
| 54 |
local bufnr = vim.api.nvim_get_current_buf() |
| 55 |
local diagnostics_count |
| 56 |
local result = {} |
| 57 |
if self.options.update_in_insert or vim.api.nvim_get_mode().mode:sub(1, 1) ~= 'i' then |
| 58 |
local error_count, warning_count, info_count, hint_count = 0, 0, 0, 0 |
| 59 |
local diagnostic_data = modules.sources.get_diagnostics(self.options.sources) |
| 60 |
-- sum all the counts |
| 61 |
for _, data in pairs(diagnostic_data) do |
| 62 |
error_count = error_count + data.error |
| 63 |
warning_count = warning_count + data.warn |
| 64 |
info_count = info_count + data.info |
| 65 |
hint_count = hint_count + data.hint |
| 66 |
end |
| 67 |
diagnostics_count = { |
| 68 |
error = error_count, |
| 69 |
warn = warning_count, |
| 70 |
info = info_count, |
| 71 |
hint = hint_count, |
| 72 |
} |
| 73 |
-- Save count for insert mode |
| 74 |
self.last_diagnostics_count[bufnr] = diagnostics_count |
| 75 |
else -- Use cached count in insert mode with update_in_insert disabled |
| 76 |
diagnostics_count = self.last_diagnostics_count[bufnr] or { error = 0, warn = 0, info = 0, hint = 0 } |
| 77 |
end |
| 78 |
|
| 79 |
local always_visible = false |
| 80 |
if type(self.options.always_visible) == 'boolean' then |
| 81 |
always_visible = self.options.always_visible |
| 82 |
elseif type(self.options.always_visible) == 'function' then |
| 83 |
always_visible = self.options.always_visible() |
| 84 |
end |
| 85 |
|
| 86 |
-- format the counts with symbols and highlights |
| 87 |
if self.options.colored then |
| 88 |
local colors, bgs = {}, {} |
| 89 |
for name, hl in pairs(self.highlight_groups) do |
| 90 |
colors[name] = self:format_hl(hl) |
| 91 |
bgs[name] = modules.utils.extract_highlight_colors(colors[name]:match('%%#(.-)#'), 'bg') |
| 92 |
end |
| 93 |
local previous_section, padding |
| 94 |
for _, section in ipairs(self.options.sections) do |
| 95 |
if diagnostics_count[section] ~= nil and (always_visible or diagnostics_count[section] > 0) then |
| 96 |
padding = previous_section and (bgs[previous_section] ~= bgs[section]) and ' ' or '' |
| 97 |
previous_section = section |
| 98 |
table.insert(result, colors[section] .. padding .. self.symbols[section] .. diagnostics_count[section]) |
| 99 |
end |
| 100 |
end |
| 101 |
else |
| 102 |
for _, section in ipairs(self.options.sections) do |
| 103 |
if diagnostics_count[section] ~= nil and (always_visible or diagnostics_count[section] > 0) then |
| 104 |
table.insert(result, self.symbols[section] .. diagnostics_count[section]) |
| 105 |
end |
| 106 |
end |
| 107 |
end |
| 108 |
return table.concat(result, ' ') |
| 109 |
end |
| 110 |
|
| 111 |
return M |