Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion lua/which-key/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 <C-r> in INSERT mode
registers = {
enabled = true, -- shows your registers on " in NORMAL or <C-r> 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 = {
Expand Down
21 changes: 21 additions & 0 deletions lua/which-key/plugins/registers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ""
Expand All @@ -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
Expand Down
Loading