Skip to content

Commit a696aec

Browse files
committed
fix(Layer): nvim failing to set keymap
1 parent 0786a9f commit a696aec

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

lua/libmodal/src/Layer.lua

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,8 @@ function Layer:map(mode, lhs, rhs, options)
134134
end
135135
end
136136

137-
-- map the `lhs` to `rhs` in `mode` with `options` for the current buffer.
138-
vim.keymap.set(mode, lhs, rhs, options)
137+
-- WARN: nvim can fail to map the `lhs` to `rhs` in `mode` with `options` unless scheduled.
138+
vim.schedule(function() vim.keymap.set(mode, lhs, rhs, options) end)
139139
end
140140

141141
-- add the new mapping to the layer's keymap
@@ -157,16 +157,18 @@ function Layer:unmap(buffer, mode, lhs)
157157
if self.existing_keymaps_by_mode then
158158
if self.existing_keymaps_by_mode[mode][lhs] then -- there is an older keymap to go back to, so undo this layer_keymaps_by_mode
159159
local rhs, options = unpack_keymap_rhs(self.existing_keymaps_by_mode[mode][lhs])
160-
vim.keymap.set(mode, lhs, rhs, options)
160+
161+
-- WARN: nvim can fail to set the keybinding here unless `schedule`d
162+
vim.schedule(function() vim.keymap.set(mode, lhs, rhs, options) end)
161163
else
162-
-- just make the keymap go back to default
163-
local no_errors, err = pcall(function()
164+
-- WARN: nvim can fail to unmap unless scheduled here
165+
local no_errors, err = pcall(vim.schedule_wrap(function()
164166
if buffer then
165167
vim.api.nvim_buf_del_keymap(buffer, mode, lhs)
166168
else
167169
vim.api.nvim_del_keymap(mode, lhs)
168170
end
169-
end)
171+
end))
170172

171173
if not (no_errors or err:match 'E31: No such mapping') then
172174
require('libmodal/src/utils').notify_error('nvim-libmodal encountered an error while unmapping from layer', err)

0 commit comments

Comments
 (0)