Skip to content

Commit

Permalink
feat(mode): allow certain modes to start hidden and only show after k…
Browse files Browse the repository at this point in the history
…eypress. See #690
  • Loading branch information
folke committed Jul 14, 2024
1 parent d36f722 commit b4fa48f
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 3 deletions.
7 changes: 7 additions & 0 deletions lua/which-key/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ local defaults = {
o = true, -- Operator pending mode
t = true, -- Terminal mode
c = true, -- Command mode
-- Start hidden and wait for a key to be pressed before showing the popup
-- Only used by xo mapping modes.
-- Set to false to show the popup immediately.
defer = {
["<C-V>"] = true,
V = true,
},
},
plugins = {
marks = true, -- shows a list of your marks on ' and `
Expand Down
4 changes: 3 additions & 1 deletion lua/which-key/health.lua
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,10 @@ function M.check()
---@type table<string, boolean>
local reported = {}

local mapmodes = vim.split("nixsotc", "")

for _, buf in pairs(Buf.bufs) do
for mapmode in pairs(Config.modes) do
for _, mapmode in ipairs(mapmodes) do
local mode = buf:get({ mode = mapmode })
if mode then
mode.tree:walk(function(node)
Expand Down
21 changes: 19 additions & 2 deletions lua/which-key/state.lua
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,18 @@ function M.setup()
end,
})

local defer_modes = {} ---@type table<string, boolean>
for k, v in pairs(Config.modes.defer) do
if v then
defer_modes[Util.norm(k)] = true
end
end

local function defer()
local mode_keys = Util.keys(vim.api.nvim_get_mode().mode)
return mode_keys[1] and defer_modes[mode_keys[1]]
end

local cooldown = Util.cooldown()
-- this prevents restarting which-key in the same tick
vim.api.nvim_create_autocmd("ModeChanged", {
Expand All @@ -78,7 +90,7 @@ function M.setup()
-- make sure the buffer mode exists
elseif Buf.get() and Util.xo() then
if not M.state then
M.start()
M.start({ defer = defer() })
end
elseif not ev.match:find("c") then
M.stop()
Expand Down Expand Up @@ -233,12 +245,17 @@ function M.start(opts)

local exit = false

local show = opts.defer ~= true

while M.state do
mode = Buf.get(opts)
if not mode or mode.mode ~= mapmode then
break
end
View.update(opts)
if show then
View.update(opts)
end
show = true
local child, _exit = M.step(M.state)
if child and M.state then
M.state.node = child
Expand Down
1 change: 1 addition & 0 deletions lua/which-key/types.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
---@field update? boolean
---@field delay? number
---@field loop? boolean
---@field defer? boolean don't show the popup immediately. Wait for the first key to be pressed

---@class wk.Icon
---@field icon? string
Expand Down

0 comments on commit b4fa48f

Please sign in to comment.