feat(diagnostics): accept functions as symbols

This commit is contained in:
Rob Watson 2023-09-20 20:39:59 +02:00
parent 2248ef254d
commit 46131d0a41
2 changed files with 60 additions and 2 deletions

View File

@ -50,6 +50,14 @@ function M:init(options)
end
end
local function render_diagnostic(symbol_or_function, count)
if type(symbol_or_function) == 'function' then
return symbol_or_function(count)
else
return symbol_or_function .. count
end
end
function M:update_status()
local bufnr = vim.api.nvim_get_current_buf()
local diagnostics_count
@ -95,13 +103,16 @@ function M:update_status()
if diagnostics_count[section] ~= nil and (always_visible or diagnostics_count[section] > 0) then
padding = previous_section and (bgs[previous_section] ~= bgs[section]) and ' ' or ''
previous_section = section
table.insert(result, colors[section] .. padding .. self.symbols[section] .. diagnostics_count[section])
table.insert(
result,
colors[section] .. padding .. render_diagnostic(self.symbols[section], diagnostics_count[section])
)
end
end
else
for _, section in ipairs(self.options.sections) do
if diagnostics_count[section] ~= nil and (always_visible or diagnostics_count[section] > 0) then
table.insert(result, self.symbols[section] .. diagnostics_count[section])
table.insert(result, render_diagnostic(self.symbols[section], diagnostics_count[section]))
end
end
end

View File

@ -820,3 +820,50 @@ describe('Branch component', function()
assert_component('branch', opts, rev)
end)
end)
describe('Diagnostics component', function()
local assert_comp_ins = helpers.assert_component_instance
local sources = {
function()
return { error = 1, warn = 2, info = 3, hint = 4 }
end,
}
it('renders the diagnostics with static symbols', function()
local opts = build_component_opts {
sources = sources,
colored = false,
symbols = {
error = 'E',
warn = 'W',
info = 'I',
hint = 'H',
},
}
local dc = helpers.init_component('diagnostics', opts)
assert_comp_ins(dc, ' E1 W2 I3 H4 ')
end)
it('renders the diagnostics with dynamic symbols', function()
local opts = build_component_opts {
sources = sources,
colored = false,
symbols = {
error = function(c)
return c .. ' foo'
end,
warn = function(c)
return c .. ' bar'
end,
info = function(c)
return c .. ' baz'
end,
hint = function(c)
return c .. ' qux'
end,
},
}
local dc = helpers.init_component('diagnostics', opts)
assert_comp_ins(dc, ' 1 foo 2 bar 3 baz 4 qux ')
end)
end)