local cursor = require("statusline.components.cursor") local cwd = require("statusline.components.cwd") local diagnostics = require("statusline.components.diagnostics") local filetype = require("statusline.components.filetype") local lsp = require("statusline.components.lsp") local macro = require("statusline.components.macro") local mode = require("statusline.components.mode") local M = {} local STATUSLINE_EXPRESSION = "%!v:lua.require('statusline').render()" local AUGROUP = "statusline.nvim" local highlight_groups = { "StatusCursorPos", "StatusCwd", "StatusFiletype", "StatusLsp", "StatusMacro", } vim.list_extend(highlight_groups, mode.highlight_groups) local function check_version() local version = vim.version() if version.major == 0 and version.minor < 12 then error( ("statusline.nvim requires Neovim 0.12 or newer (found %d.%d.%d)"):format( version.major, version.minor, version.patch ), 3 ) end end local function validate_string_map(name, values, validate_key) if values == nil then return {} end if type(values) ~= "table" then error(("statusline.setup %s must be a table (got %s)"):format(name, type(values)), 4) end local copy = {} for key, value in pairs(values) do if type(key) ~= "string" then error(("statusline.setup %s keys must be strings (got %s)"):format(name, type(key)), 4) end if validate_key and not validate_key(key) then error(("statusline.setup %s has unknown key %q"):format(name, key), 4) end if type(value) ~= "string" then error( ("statusline.setup %s[%q] must be a string (got %s)"):format(name, key, type(value)), 4 ) end copy[key] = value end return copy end local function validate_lsp_exclude(excluded) if excluded == nil then return {} end if type(excluded) ~= "table" then error(("statusline.setup lsp_exclude must be a table (got %s)"):format(type(excluded)), 4) end if not vim.islist(excluded) then error("statusline.setup lsp_exclude must be a list", 4) end local names = {} for index, name in ipairs(excluded) do if type(name) ~= "string" then error( ("statusline.setup lsp_exclude[%d] must be a string (got %s)"):format(index, type(name)), 4 ) end names[name] = true end return names end local function validate_options(options) if options ~= nil and type(options) ~= "table" then error(("statusline.setup options must be a table or nil (got %s)"):format(type(options)), 3) end options = options or {} local mode_text = validate_string_map("mode_text", options.mode_text, mode.is_text_key) local filetype_icons = validate_string_map("filetype_icons", options.filetype_icons) local excluded_lsp_names = validate_lsp_exclude(options.lsp_exclude) return mode_text, filetype_icons, excluded_lsp_names end local function statusline_buffer() local winid = vim.g.statusline_winid if type(winid) == "number" and vim.api.nvim_win_is_valid(winid) then return vim.api.nvim_win_get_buf(winid) end return vim.api.nvim_get_current_buf() end local function set_default_highlight(group, link) vim.api.nvim_set_hl(0, group, { default = true, link = link }) -- A colorscheme can leave an allocated group with an empty definition. In -- that case `default` considers the group to exist, so restore the missing -- link explicitly. Non-empty user and colorscheme definitions stay intact. local definition = vim.api.nvim_get_hl(0, { name = group, link = true, create = false }) if next(definition) == nil then vim.api.nvim_set_hl(0, group, { link = link }) end end local function set_default_highlights() for _, group in ipairs(highlight_groups) do set_default_highlight(group, "StatusLine") end for group, link in pairs(diagnostics.highlight_links) do set_default_highlight(group, link) end end local diagnostic_redraw_pending = false local function redraw_statusline() vim.cmd("redrawstatus") end local function schedule_diagnostic_redraw() if diagnostic_redraw_pending then return end diagnostic_redraw_pending = true vim.schedule(function() diagnostic_redraw_pending = false redraw_statusline() end) end ---Render the complete statusline for the current mode. ---@return string statusline function M.render() local directory = vim.fn.getcwd(-1, 0) local left = mode.render() .. " " .. cwd.render(directory) local diagnostic_component = diagnostics.render(directory) if diagnostic_component then left = left .. " " .. diagnostic_component end local buffer = statusline_buffer() local buftype = vim.api.nvim_get_option_value("buftype", { buf = buffer }) local buffer_filetype = vim.api.nvim_get_option_value("filetype", { buf = buffer }) local right = {} local macro_component = macro.render() if macro_component then right[#right + 1] = macro_component end local lsp_component = lsp.render(buffer) if lsp_component then right[#right + 1] = lsp_component end local filetype_component = filetype.render(buffer_filetype) if filetype_component then right[#right + 1] = filetype_component end if buftype ~= "terminal" then right[#right + 1] = cursor.render() end if #right == 0 then return left end return left .. "%=" .. table.concat(right, " ") end ---@class StatuslineModeText ---@field normal? string ---@field operator_pending? string ---@field insert? string ---@field visual? string ---@field visual_line? string ---@field visual_block? string ---@field select? string ---@field select_line? string ---@field select_block? string ---@field replace? string ---@field virtual_replace? string ---@field command? string ---@field ex? string ---@field prompt? string ---@field more? string ---@field confirm? string ---@field shell? string ---@field terminal? string ---@field terminal_normal? string ---@field unknown? string ---@class StatuslineSetupOptions ---@field mode_text? StatuslineModeText ---@field filetype_icons? table ---@field lsp_exclude? string[] ---Install and configure statusline.nvim. ---@param options? StatuslineSetupOptions function M.setup(options) check_version() local mode_text, filetype_icons, excluded_lsp_names = validate_options(options) set_default_highlights() local group = vim.api.nvim_create_augroup(AUGROUP, { clear = true }) vim.api.nvim_create_autocmd("ColorScheme", { group = group, desc = "Restore statusline.nvim default highlight links", callback = set_default_highlights, }) vim.api.nvim_create_autocmd({ "RecordingEnter", "RecordingLeave" }, { group = group, desc = "Redraw statuslines after macro recording changes", callback = function() vim.schedule(redraw_statusline) end, }) vim.api.nvim_create_autocmd({ "DiagnosticChanged", "BufFilePost", "BufDelete", "BufWipeout" }, { group = group, desc = "Refresh statusline.nvim diagnostic state", callback = function(args) if args.event == "DiagnosticChanged" then diagnostics.invalidate() else diagnostics.invalidate_buffer(args.buf) end schedule_diagnostic_redraw() end, }) vim.api.nvim_set_option_value("statusline", STATUSLINE_EXPRESSION, { scope = "global" }) mode.set_text(mode_text) filetype.set_icons(filetype_icons) lsp.set_excluded_names(excluded_lsp_names) diagnostics.invalidate() end return M