mirror of https://github.com/folke/lazy.nvim.git
feat(keys): you can now create buffer-local filetype keymaps by specifying `ft=`. Fixes #1076
This commit is contained in:
parent
19d1b3aa72
commit
c42e63c198
|
@ -147,6 +147,7 @@ can be a `LazyKeys` table with the following key-value pairs:
|
|||
- **[1]**: (`string`) lhs **_(required)_**
|
||||
- **[2]**: (`string|fun()`) rhs **_(optional)_**
|
||||
- **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`
|
||||
|
||||
Key mappings will load the plugin the first time they get executed.
|
||||
|
|
|
@ -9,6 +9,7 @@ local Loader = require("lazy.core.loader")
|
|||
---@field noremap? boolean
|
||||
---@field remap? boolean
|
||||
---@field expr? boolean
|
||||
---@field ft? string|string[]
|
||||
---@field id string
|
||||
|
||||
---@class LazyKeysHandler:LazyHandler
|
||||
|
@ -53,7 +54,7 @@ end
|
|||
function M.opts(keys)
|
||||
local opts = {}
|
||||
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
|
||||
end
|
||||
end
|
||||
|
@ -64,16 +65,21 @@ end
|
|||
function M:_add(keys)
|
||||
local lhs = keys[1]
|
||||
local opts = M.opts(keys)
|
||||
|
||||
---@param buf? number
|
||||
local function add(buf)
|
||||
vim.keymap.set(keys.mode, lhs, function()
|
||||
local plugins = self.active[keys.id]
|
||||
|
||||
-- always delete the mapping immediately to prevent recursive mappings
|
||||
self:_del(keys)
|
||||
self:_del(keys, buf)
|
||||
self.active[keys.id] = nil
|
||||
|
||||
if plugins then
|
||||
Util.track({ keys = lhs })
|
||||
Loader.load(plugins, { keys = lhs })
|
||||
Util.track()
|
||||
end
|
||||
|
||||
local feed = vim.api.nvim_replace_termcodes("<Ignore>" .. lhs, true, true, true)
|
||||
-- insert instead of append the lhs
|
||||
|
@ -83,14 +89,38 @@ function M:_add(keys)
|
|||
nowait = opts.nowait,
|
||||
-- we do not return anything, but this is still needed to make operator pending mappings work
|
||||
expr = true,
|
||||
buffer = buf,
|
||||
})
|
||||
end
|
||||
|
||||
---@param keys LazyKeys
|
||||
function M:_del(keys)
|
||||
pcall(vim.keymap.del, keys.mode, keys[1])
|
||||
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
|
||||
vim.keymap.set(keys.mode, keys[1], keys[2], M.opts(keys))
|
||||
self:_del(keys, event.buf)
|
||||
end
|
||||
end
|
||||
end,
|
||||
})
|
||||
else
|
||||
add()
|
||||
end
|
||||
end
|
||||
|
||||
---@param keys LazyKeys
|
||||
---@param buf number?
|
||||
function M:_del(keys, buf)
|
||||
pcall(vim.keymap.del, keys.mode, keys[1], { buffer = buf })
|
||||
if keys[2] then
|
||||
local opts = M.opts(keys)
|
||||
opts.buffer = buf
|
||||
vim.keymap.set(keys.mode, keys[1], keys[2], opts)
|
||||
end
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in New Issue