feat(diagnostics): accept functions as symbols
This commit is contained in:
parent
2248ef254d
commit
46131d0a41
@ -50,6 +50,14 @@ function M:init(options)
|
|||||||
end
|
end
|
||||||
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()
|
function M:update_status()
|
||||||
local bufnr = vim.api.nvim_get_current_buf()
|
local bufnr = vim.api.nvim_get_current_buf()
|
||||||
local diagnostics_count
|
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
|
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 ''
|
padding = previous_section and (bgs[previous_section] ~= bgs[section]) and ' ' or ''
|
||||||
previous_section = section
|
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
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
for _, section in ipairs(self.options.sections) do
|
for _, section in ipairs(self.options.sections) do
|
||||||
if diagnostics_count[section] ~= nil and (always_visible or diagnostics_count[section] > 0) then
|
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
|
end
|
||||||
end
|
end
|
||||||
|
@ -820,3 +820,50 @@ describe('Branch component', function()
|
|||||||
assert_component('branch', opts, rev)
|
assert_component('branch', opts, rev)
|
||||||
end)
|
end)
|
||||||
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)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user