Something went wrong. Try again.
Neovim Statusline
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264local 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 ) endend
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 copyend
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 namesend
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_namesend
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 }) endend
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) endend
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 statuslinefunction 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<string, string>---@field lsp_exclude? string[]
---Install and configure statusline.nvim.---@param options? StatuslineSetupOptionsfunction 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