@@ -57,10 +57,8 @@ function M.setup()
57
57
callback = function (ev )
58
58
Util .debug (ev .event )
59
59
if ev .event == " RecordingEnter" then
60
- Buf .clear ({ buf = ev .buf , check = false })
60
+ Buf .clear ({ buf = ev .buf })
61
61
M .stop ()
62
- else
63
- Buf .check ()
64
62
end
65
63
end ,
66
64
})
@@ -89,16 +87,11 @@ function M.setup()
89
87
return Config .defer (ctx )
90
88
end
91
89
92
- -- this prevents restarting which-key in the same tick
93
90
local cooldown = Util .cooldown ()
94
-
95
- -- cache the mode, since it can change outside of ModeChanged events
96
- Util .mode = vim .api .nvim_get_mode ().mode
97
-
91
+ -- this prevents restarting which-key in the same tick
98
92
vim .api .nvim_create_autocmd (" ModeChanged" , {
99
93
group = group ,
100
94
callback = function (ev )
101
- Util .mode = vim .api .nvim_get_mode ().mode
102
95
Util .trace (" ModeChanged(" .. ev .match .. " )" )
103
96
local mode = Buf .get ()
104
97
@@ -147,16 +140,32 @@ function M.setup()
147
140
end ,
148
141
})
149
142
143
+ local current_buf = vim .api .nvim_get_current_buf ()
150
144
vim .api .nvim_create_autocmd ({ " BufEnter" }, {
151
145
group = group ,
152
146
callback = function (ev )
147
+ current_buf = ev .buf --- @type number
153
148
Util .trace (ev .event .. " (" .. ev .buf .. " )" )
154
149
Buf .get ()
155
150
Util .trace ()
156
151
end ,
157
152
})
158
153
159
- Buf .check ()
154
+ -- HACK: ModeChanged does not always trigger, so we need to manually
155
+ -- check for mode changes. This seems to be due to the usage of `:norm` in autocmds.
156
+ -- See https://github.com/folke/which-key.nvim/issues/787
157
+ local last_mode = nil --- @type string ?
158
+ local last_buf = nil --- @type number ?
159
+ local timer = uv .new_timer ()
160
+ timer :start (0 , 50 , function ()
161
+ local mode = vim .api .nvim_get_mode ().mode
162
+ if mode == last_mode and last_buf == current_buf then
163
+ return
164
+ end
165
+ last_mode = mode
166
+ last_buf = current_buf
167
+ vim .schedule (Buf .get )
168
+ end )
160
169
end
161
170
162
171
function M .stop ()
0 commit comments