Skip to content

Instantly share code, notes, and snippets.

@pianocomposer321
Created January 22, 2021 15:03
Show Gist options
  • Save pianocomposer321/dd16eca88a588a233b7cbcb57039ed6e to your computer and use it in GitHub Desktop.
Save pianocomposer321/dd16eca88a588a233b7cbcb57039ed6e to your computer and use it in GitHub Desktop.
Lualine Diagnostics
local lualine = require("lualine")
local M = {}
vim.cmd('highlight my_hl_warn guifg=#FABD2F guibg=#3C3836')
vim.cmd('highlight my_hl_error guifg=#FB4934 guibg=#3C3836')
local function diagnostics()
local errors = 0
local warnings = 0
local cur_buf_errors = 0
local cur_buf_warnings = 0
for _, buffer in ipairs(vim.fn['getbufinfo']()) do -- Loop through buffers
if buffer.listed == 1 and buffer.name ~= '' then -- If the buffer is listed and it is not a no-name buffer
local bufnr = buffer.bufnr
local buf_errors = vim.lsp.diagnostic.get_count(bufnr, [[Error]])
local buf_warnings = vim.lsp.diagnostic.get_count(bufnr, [[Warning]])
errors = errors + buf_errors -- Add this buffer's errors to the total errors
warnings = warnings + buf_warnings -- Same with warnings
if bufnr == vim.fn.bufnr() then -- If this buffer is the currently open buffer
cur_buf_errors = buf_errors
cur_buf_warnings = buf_warnings
end
end
end
if errors ~= 0 or warnings ~= 0 then -- If there is at least one error or warning
local to_return = ''
local error_group = "%#my_hl_error# E "..tostring(cur_buf_errors)..":"..tostring(errors).." "
local warning_group = "%#my_hl_warn# W "..tostring(cur_buf_warnings)..":"..tostring(warnings).." "
local normal_group = "%#lualine_c_normal#"
if warnings ~= 0 then
to_return = to_return..warning_group
end
if errors ~= 0 then
to_return = to_return..error_group
end
to_return = to_return..normal_group
return to_return
else
return '' -- Otherwise return empty string
end
end
function M.config()
local new_section = vim.list_extend({ diagnostics }, lualine.sections.lualine_x)
lualine.sections.lualine_x = new_section
lualine.status()
end
return M
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment