feat(keys): you can now create buffer-local filetype keymaps by specifying `ft=`. Fixes #1076

This commit is contained in:
Folke Lemaitre 2023-10-05 14:00:08 +02:00
parent 19d1b3aa72
commit c42e63c198
No known key found for this signature in database
GPG Key ID: 41F8B1FBACAE2040
2 changed files with 52 additions and 21 deletions

View File

@ -147,6 +147,7 @@ can be a `LazyKeys` table with the following key-value pairs:
- **[1]**: (`string`) lhs **_(required)_** - **[1]**: (`string`) lhs **_(required)_**
- **[2]**: (`string|fun()`) rhs **_(optional)_** - **[2]**: (`string|fun()`) rhs **_(optional)_**
- **mode**: (`string|string[]`) mode **_(optional, defaults to `"n"`)_** - **mode**: (`string|string[]`) mode **_(optional, defaults to `"n"`)_**
- **ft**: (`string|string[]`) `filetype` for buffer-local keymaps **_(optional)_**
- any other option valid for `vim.keymap.set` - any other option valid for `vim.keymap.set`
Key mappings will load the plugin the first time they get executed. Key mappings will load the plugin the first time they get executed.

View File

@ -9,6 +9,7 @@ local Loader = require("lazy.core.loader")
---@field noremap? boolean ---@field noremap? boolean
---@field remap? boolean ---@field remap? boolean
---@field expr? boolean ---@field expr? boolean
---@field ft? string|string[]
---@field id string ---@field id string
---@class LazyKeysHandler:LazyHandler ---@class LazyKeysHandler:LazyHandler
@ -53,7 +54,7 @@ end
function M.opts(keys) function M.opts(keys)
local opts = {} local opts = {}
for k, v in pairs(keys) do for k, v in pairs(keys) do
if type(k) ~= "number" and k ~= "mode" and k ~= "id" then if type(k) ~= "number" and k ~= "mode" and k ~= "id" and k ~= "ft" then
opts[k] = v opts[k] = v
end end
end end
@ -64,33 +65,62 @@ end
function M:_add(keys) function M:_add(keys)
local lhs = keys[1] local lhs = keys[1]
local opts = M.opts(keys) local opts = M.opts(keys)
vim.keymap.set(keys.mode, lhs, function()
local plugins = self.active[keys.id]
-- always delete the mapping immediately to prevent recursive mappings ---@param buf? number
self:_del(keys) local function add(buf)
self.active[keys.id] = nil vim.keymap.set(keys.mode, lhs, function()
local plugins = self.active[keys.id]
Util.track({ keys = lhs }) -- always delete the mapping immediately to prevent recursive mappings
Loader.load(plugins, { keys = lhs }) self:_del(keys, buf)
Util.track() self.active[keys.id] = nil
local feed = vim.api.nvim_replace_termcodes("<Ignore>" .. lhs, true, true, true) if plugins then
-- insert instead of append the lhs Util.track({ keys = lhs })
vim.api.nvim_feedkeys(feed, "i", false) Loader.load(plugins, { keys = lhs })
end, { Util.track()
desc = opts.desc, end
nowait = opts.nowait,
-- we do not return anything, but this is still needed to make operator pending mappings work local feed = vim.api.nvim_replace_termcodes("<Ignore>" .. lhs, true, true, true)
expr = true, -- insert instead of append the lhs
}) vim.api.nvim_feedkeys(feed, "i", false)
end, {
desc = opts.desc,
nowait = opts.nowait,
-- we do not return anything, but this is still needed to make operator pending mappings work
expr = true,
buffer = buf,
})
end
if keys.ft then
vim.api.nvim_create_autocmd("FileType", {
pattern = keys.ft,
callback = function(event)
if self.active[keys.id] then
add(event.buf)
else
-- Only create the mapping if its managed by lazy
-- otherwise the plugin is supposed to manage it
if keys[2] then
self:_del(keys, event.buf)
end
end
end,
})
else
add()
end
end end
---@param keys LazyKeys ---@param keys LazyKeys
function M:_del(keys) ---@param buf number?
pcall(vim.keymap.del, keys.mode, keys[1]) function M:_del(keys, buf)
pcall(vim.keymap.del, keys.mode, keys[1], { buffer = buf })
if keys[2] then if keys[2] then
vim.keymap.set(keys.mode, keys[1], keys[2], M.opts(keys)) local opts = M.opts(keys)
opts.buffer = buf
vim.keymap.set(keys.mode, keys[1], keys[2], opts)
end end
end end