diff --git a/lua/which-key/config.lua b/lua/which-key/config.lua index 474a2943..5a3a4e96 100644 --- a/lua/which-key/config.lua +++ b/lua/which-key/config.lua @@ -39,7 +39,20 @@ local defaults = { end, plugins = { marks = true, -- shows a list of your marks on ' and ` - registers = true, -- shows your registers on " in NORMAL or in INSERT mode + registers = { + enabled = true, -- shows your registers on " in NORMAL or in INSERT mode + -- Format function for register values. Accepts a string and returns a formatted string. + -- Useful for truncating long register contents to prevent UI freezing. + -- The default truncates at 100 characters to balance readability and performance. + ---@type fun(value: string): string + format = function(value) + local max_len = 100 -- Truncate after 100 characters to prevent UI freezing + if #value > max_len then + return value:sub(1, max_len) .. "…" + end + return value + end, + }, -- the presets plugin, adds help for a bunch of default keybindings in Neovim -- No actual key bindings are created spelling = { diff --git a/lua/which-key/plugins/registers.lua b/lua/which-key/plugins/registers.lua index 4464a4ef..1eb1c440 100644 --- a/lua/which-key/plugins/registers.lua +++ b/lua/which-key/plugins/registers.lua @@ -37,12 +37,29 @@ M.replace = { ["\r"] = "", } +-- Cache the format function to avoid repeated lookups +local format_fn = nil +local format_fn_initialized = false + +local function get_format_fn() + if not format_fn_initialized then + local Config = require("which-key.config") + if type(Config.plugins.registers) == "table" and type(Config.plugins.registers.format) == "function" then + format_fn = Config.plugins.registers.format + end + format_fn_initialized = true + end + return format_fn +end + function M.expand() local items = {} ---@type wk.Plugin.item[] local is_osc52 = vim.g.clipboard and vim.g.clipboard.name == "OSC 52" local has_clipboard = vim.g.loaded_clipboard_provider == 2 + local format_func = get_format_fn() + for i = 1, #M.registers, 1 do local key = M.registers:sub(i, i) local value = "" @@ -57,6 +74,10 @@ function M.expand() for k, v in pairs(M.replace) do value = value:gsub(k, v) --[[@as string]] end + -- Apply format function if configured + if format_func then + value = format_func(value) + end table.insert(items, { key = key, desc = labels[key] or "", value = value }) end end