Something went wrong. Try again.
Neovim Statusline
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117local M = { highlight_links = {} }
local ERROR = vim.diagnostic.severity.ERRORlocal WARN = vim.diagnostic.severity.WARNlocal INFO = vim.diagnostic.severity.INFOlocal 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.linkend
local cached_names = {}local normalized_paths = {}local cached_directorylocal cached_workspacelocal render_cache_valid = falselocal render_cache_directorylocal 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_workspaceend
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 = nilend
---Discard the cached normalized path for a buffer and rendered component.---@param buffer integerfunction 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<integer, integer> countsfunction 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 countsend
---Render nonzero workspace diagnostic severity boxes.---@param directory string---@return string? componentfunction 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_componentend
return M