Skip to content

Commit

Permalink
fix(util): clear cache when leader changes
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Apr 17, 2023
1 parent f6bb21c commit df3597f
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lua/which-key/util.lua
Expand Up @@ -6,6 +6,17 @@ local strsub = string.sub
local cache = {}
---@type table<string,string>
local tcache = {}
local cache_leaders = ""

function M.check_cache()
---@type string
local leaders = vim.g.mapleader .. ":" .. vim.g.maplocalleader

This comment has been minimized.

Copy link
@andrewferrier

andrewferrier Apr 17, 2023

FYI, I think this change fails if vim.g.maplocalleader is not set (I don't use a local leader, for example). I get an error like this on startup:

Error detected while processing VimEnter Autocommands for "*":
E5108: Error executing lua ...al/share/nvim/lazy/which-key.nvim/lua/which-key/util.lua:13: attempt to concatenate field 'maplocalleader' (a ni
l value)
stack traceback:
        ...al/share/nvim/lazy/which-key.nvim/lua/which-key/util.lua:13: in function 'check_cache'
        ...al/share/nvim/lazy/which-key.nvim/lua/which-key/util.lua:78: in function 'parse_keys'
        ...hare/nvim/lazy/which-key.nvim/lua/which-key/mappings.lua:183: in function <...hare/nvim/lazy/which-key.nvim/lua/which-key/mappings.
lua:171>
        vim/shared.lua: in function 'parse'
        ...al/share/nvim/lazy/which-key.nvim/lua/which-key/keys.lua:203: in function 'register'
        .../nvim/lazy/which-key.nvim/lua/which-key/plugins/init.lua:32: in function '_setup'
        .../nvim/lazy/which-key.nvim/lua/which-key/plugins/init.lua:19: in function 'setup'
        ...al/share/nvim/lazy/which-key.nvim/lua/which-key/init.lua:85: in function 'load'
        [string ":lua"]:1: in main chunk

Let me know if you need a formal issue to recreate...

This comment has been minimized.

Copy link
@folke

folke Apr 17, 2023

Author Owner

fixed!

if leaders ~= cache_leaders then
cache = {}
tcache = {}
cache_leaders = leaders
end
end

function M.count(tab)
local ret = 0
Expand All @@ -27,6 +38,7 @@ function M.is_empty(tab)
end

function M.t(str)
M.check_cache()
if not tcache[str] then
-- https://github.com/neovim/neovim/issues/17369
tcache[str] = vim.api.nvim_replace_termcodes(str, false, true, true):gsub("\128\254X", "\128")
Expand Down Expand Up @@ -63,6 +75,7 @@ local Tokens = {
}
---@return KeyCodes
function M.parse_keys(keystr)
M.check_cache()
if cache[keystr] then
return cache[keystr]
end
Expand Down

0 comments on commit df3597f

Please sign in to comment.