Skip to content

Commit

Permalink
perf(util): cache parse_keys
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Apr 17, 2023
1 parent eaa8027 commit 8649bf5
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions lua/which-key/util.lua
Expand Up @@ -2,6 +2,8 @@
local M = {}
local strbyte = string.byte
local strsub = string.sub
---@type table<string, KeyCodes>
local cache = {}
---@type table<string,string>
local tcache = {}

Expand Down Expand Up @@ -61,13 +63,19 @@ local Tokens = {
}
---@return KeyCodes
function M.parse_keys(keystr)
if cache[keystr] then
return cache[keystr]
end

local keys = M.t(keystr)
local internal = M.parse_internal(keys)

if #internal == 0 then
return { keys = keys, internal = internal, notation = {} }
local ret = { keys = keys, internal = {}, notation = {} }
cache[keystr] = ret

This comment has been minimized.

Copy link
@zeertzjq

zeertzjq Apr 17, 2023

Contributor

A return statement is missing here.

This comment has been minimized.

Copy link
@folke

folke Apr 17, 2023

Author Owner

good catch! Fixed

end

local keystr_orig = keystr
keystr = keystr:gsub("<lt>", "<")
local notation = {}
---@alias ParseState
Expand Down Expand Up @@ -106,11 +114,15 @@ function M.parse_keys(keystr)
error(vim.inspect({ keystr = keystr, internal = internal, notation = notation }))
end

return {
local ret = {
keys = keys,
internal = internal,
notation = notation,
}

cache[keystr_orig] = ret

return ret
end

-- @return string[]
Expand Down

0 comments on commit 8649bf5

Please sign in to comment.