local M = { highlight_links = {} } local ERROR = vim.diagnostic.severity.ERROR local WARN = vim.diagnostic.severity.WARN local INFO = vim.diagnostic.severity.INFO local HINT = vim.diagnostic.severity.HINT local severities = { { value = ERROR, icon = "󰅚", highlight = "StatusDiagnosticError", link = "DiagnosticError" }, { value = WARN, icon = "󰀪", highlight = "StatusDiagnosticWarn", link = "DiagnosticWarn" }, { value = INFO, icon = "󰋽", highlight = "StatusDiagnosticInfo", link = "DiagnosticInfo" }, { value = HINT, icon = "󰌶", highlight = "StatusDiagnosticHint", link = "DiagnosticHint" }, } for _, severity in ipairs(severities) do M.highlight_links[severity.highlight] = severity.link end local cached_names = {} local normalized_paths = {} local cached_directory local cached_workspace local render_cache_valid = false local render_cache_directory local render_cache_component local function normalize_workspace(directory) if directory ~= cached_directory then cached_directory, cached_workspace = directory, vim.fs.normalize(directory) end return cached_workspace end local function normalize_buffer_path(buffer, name) if cached_names[buffer] ~= name then normalized_paths[buffer], cached_names[buffer] = vim.fs.normalize(name), name end return normalized_paths[buffer] end ---Discard the cached rendered diagnostics component. function M.invalidate() render_cache_valid = false render_cache_directory = nil render_cache_component = nil end ---Discard the cached normalized path for a buffer and rendered component. ---@param buffer integer function M.invalidate_buffer(buffer) cached_names[buffer] = nil normalized_paths[buffer] = nil M.invalidate() end ---Count diagnostics in named buffers within a workspace. ---@param directory string ---@return table counts function M.count(directory) local workspace = normalize_workspace(directory) local workspace_prefix = workspace:sub(-1) == "/" and workspace or workspace .. "/" local counts = { [ERROR] = 0, [WARN] = 0, [INFO] = 0, [HINT] = 0, } for _, buffer in ipairs(vim.api.nvim_list_bufs()) do if vim.api.nvim_buf_is_valid(buffer) then local name = vim.api.nvim_buf_get_name(buffer) if name ~= "" then local path = normalize_buffer_path(buffer, name) if path == workspace or path:find(workspace_prefix, 1, true) == 1 then local buffer_counts = vim.diagnostic.count(buffer) counts[ERROR] = counts[ERROR] + (buffer_counts[ERROR] or 0) counts[WARN] = counts[WARN] + (buffer_counts[WARN] or 0) counts[INFO] = counts[INFO] + (buffer_counts[INFO] or 0) counts[HINT] = counts[HINT] + (buffer_counts[HINT] or 0) end end end end return counts end ---Render nonzero workspace diagnostic severity boxes. ---@param directory string ---@return string? component function M.render(directory) if render_cache_valid and render_cache_directory == directory then return render_cache_component end local counts = M.count(directory) local components = {} for _, severity in ipairs(severities) do local count = counts[severity.value] if count > 0 then components[#components + 1] = ("%%#%s# %s %d %%*"):format( severity.highlight, severity.icon, count ) end end render_cache_directory = directory render_cache_component = #components > 0 and table.concat(components, " ") or nil render_cache_valid = true return render_cache_component end return M