2022-12-05 13:45:50 +00:00
|
|
|
local Loader = require("lazy.core.loader")
|
2023-10-09 10:25:42 +01:00
|
|
|
local Util = require("lazy.core.util")
|
2022-12-05 13:45:50 +00:00
|
|
|
|
2023-10-08 09:11:25 +01:00
|
|
|
---@class LazyKeysBase
|
2022-12-22 09:32:21 +00:00
|
|
|
---@field desc? string
|
|
|
|
---@field noremap? boolean
|
|
|
|
---@field remap? boolean
|
|
|
|
---@field expr? boolean
|
2023-10-08 09:11:25 +01:00
|
|
|
---@field nowait? boolean
|
2023-10-05 13:00:08 +01:00
|
|
|
---@field ft? string|string[]
|
2023-10-08 09:11:25 +01:00
|
|
|
|
|
|
|
---@class LazyKeysSpec: LazyKeysBase
|
|
|
|
---@field [1] string lhs
|
|
|
|
---@field [2]? string|fun()|false rhs
|
|
|
|
---@field mode? string|string[]
|
|
|
|
|
|
|
|
---@class LazyKeys: LazyKeysBase
|
|
|
|
---@field lhs string lhs
|
|
|
|
---@field rhs? string|fun() rhs
|
|
|
|
---@field mode? string
|
2023-01-16 09:16:35 +00:00
|
|
|
---@field id string
|
2022-12-22 09:32:21 +00:00
|
|
|
|
2022-12-05 13:45:50 +00:00
|
|
|
---@class LazyKeysHandler:LazyHandler
|
|
|
|
local M = {}
|
|
|
|
|
2023-10-08 09:11:25 +01:00
|
|
|
---@param value string|LazyKeysSpec
|
|
|
|
---@param mode? string
|
|
|
|
---@return LazyKeys
|
|
|
|
function M.parse(value, mode)
|
|
|
|
value = type(value) == "string" and { value } or value --[[@as LazyKeysSpec]]
|
|
|
|
local ret = vim.deepcopy(value) --[[@as LazyKeys]]
|
|
|
|
ret.lhs = ret[1] or ""
|
|
|
|
ret.rhs = ret[2]
|
|
|
|
---@diagnostic disable-next-line: no-unknown
|
|
|
|
ret[1] = nil
|
|
|
|
---@diagnostic disable-next-line: no-unknown
|
|
|
|
ret[2] = nil
|
|
|
|
ret.mode = mode or "n"
|
|
|
|
ret.id = vim.api.nvim_replace_termcodes(ret.lhs, true, true, true)
|
|
|
|
if ret.mode ~= "n" then
|
|
|
|
ret.id = ret.id .. " (" .. ret.mode .. ")"
|
2023-01-16 09:16:35 +00:00
|
|
|
end
|
2022-12-22 16:49:32 +00:00
|
|
|
return ret
|
2022-12-22 09:32:21 +00:00
|
|
|
end
|
|
|
|
|
2023-10-08 09:11:25 +01:00
|
|
|
---@param lhs string
|
|
|
|
---@param mode? string
|
|
|
|
function M:have(lhs, mode)
|
|
|
|
local keys = M.parse(lhs, mode)
|
2023-10-08 18:14:33 +01:00
|
|
|
return self.managed[keys.id] ~= nil
|
2023-10-08 09:11:25 +01:00
|
|
|
end
|
|
|
|
|
2023-01-16 09:16:35 +00:00
|
|
|
---@param plugin LazyPlugin
|
|
|
|
function M:values(plugin)
|
2023-10-16 14:05:16 +01:00
|
|
|
local Plugin = require("lazy.core.plugin")
|
|
|
|
return M.resolve(Plugin.values(plugin, "keys", true))
|
2023-10-08 09:11:25 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
---@param spec? (string|LazyKeysSpec)[]
|
|
|
|
function M.resolve(spec)
|
|
|
|
---@type LazyKeys[]
|
2023-01-16 09:16:35 +00:00
|
|
|
local values = {}
|
|
|
|
---@diagnostic disable-next-line: no-unknown
|
2023-10-08 09:11:25 +01:00
|
|
|
for _, value in ipairs(spec or {}) do
|
|
|
|
value = type(value) == "string" and { value } or value --[[@as LazyKeysSpec]]
|
|
|
|
value.mode = value.mode or "n"
|
|
|
|
local modes = (type(value.mode) == "table" and value.mode or { value.mode }) --[=[@as string[]]=]
|
|
|
|
for _, mode in ipairs(modes) do
|
|
|
|
local keys = M.parse(value, mode)
|
|
|
|
if keys.rhs == vim.NIL or keys.rhs == false then
|
|
|
|
values[keys.id] = nil
|
|
|
|
else
|
|
|
|
values[keys.id] = keys
|
|
|
|
end
|
2023-01-16 09:16:35 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
return values
|
|
|
|
end
|
|
|
|
|
2023-10-08 09:11:25 +01:00
|
|
|
---@param keys LazyKeys
|
2022-12-22 09:32:21 +00:00
|
|
|
function M.opts(keys)
|
2023-10-08 09:11:25 +01:00
|
|
|
local skip = { mode = true, id = true, ft = true, rhs = true, lhs = true }
|
|
|
|
local opts = {} ---@type LazyKeysBase
|
|
|
|
---@diagnostic disable-next-line: no-unknown
|
2022-12-22 09:32:21 +00:00
|
|
|
for k, v in pairs(keys) do
|
2023-10-08 09:11:25 +01:00
|
|
|
if type(k) ~= "number" and not skip[k] then
|
|
|
|
---@diagnostic disable-next-line: no-unknown
|
2022-12-22 09:32:21 +00:00
|
|
|
opts[k] = v
|
2022-12-21 18:01:58 +00:00
|
|
|
end
|
2022-12-22 09:32:21 +00:00
|
|
|
end
|
|
|
|
return opts
|
|
|
|
end
|
|
|
|
|
2023-01-16 09:16:35 +00:00
|
|
|
---@param keys LazyKeys
|
|
|
|
function M:_add(keys)
|
2023-10-08 09:11:25 +01:00
|
|
|
local lhs = keys.lhs
|
2022-12-22 16:49:32 +00:00
|
|
|
local opts = M.opts(keys)
|
2022-12-25 15:17:36 +00:00
|
|
|
|
2023-10-05 13:00:08 +01:00
|
|
|
---@param buf? number
|
|
|
|
local function add(buf)
|
|
|
|
vim.keymap.set(keys.mode, lhs, function()
|
|
|
|
local plugins = self.active[keys.id]
|
2022-12-25 15:17:36 +00:00
|
|
|
|
2023-10-05 13:00:08 +01:00
|
|
|
-- always delete the mapping immediately to prevent recursive mappings
|
2023-10-07 19:41:06 +01:00
|
|
|
self:_del(keys)
|
2023-10-05 13:00:08 +01:00
|
|
|
self.active[keys.id] = nil
|
2023-02-08 15:00:56 +00:00
|
|
|
|
2023-10-05 13:00:08 +01:00
|
|
|
if plugins then
|
|
|
|
Util.track({ keys = lhs })
|
|
|
|
Loader.load(plugins, { keys = lhs })
|
|
|
|
Util.track()
|
|
|
|
end
|
|
|
|
|
2023-10-07 19:41:06 +01:00
|
|
|
-- Create the real buffer-local mapping
|
|
|
|
if keys.ft then
|
|
|
|
self:_set(keys, buf)
|
|
|
|
end
|
|
|
|
|
2023-10-05 13:00:08 +01:00
|
|
|
local feed = vim.api.nvim_replace_termcodes("<Ignore>" .. lhs, true, true, 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
|
|
|
|
|
2023-10-07 19:41:06 +01:00
|
|
|
-- buffer-local mappings
|
2023-10-05 13:00:08 +01:00
|
|
|
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
|
2023-10-07 19:41:06 +01:00
|
|
|
self:_set(keys, event.buf)
|
2023-10-05 13:00:08 +01:00
|
|
|
end
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
else
|
|
|
|
add()
|
|
|
|
end
|
2022-12-05 13:45:50 +00:00
|
|
|
end
|
|
|
|
|
2023-10-07 19:41:06 +01:00
|
|
|
-- Delete a mapping and create the real global
|
|
|
|
-- mapping when needed
|
|
|
|
---@param keys LazyKeys
|
|
|
|
function M:_del(keys)
|
2023-10-08 09:11:25 +01:00
|
|
|
pcall(vim.keymap.del, keys.mode, keys.lhs)
|
2023-10-07 19:41:06 +01:00
|
|
|
-- make sure to create global mappings when needed
|
|
|
|
-- buffer-local mappings are managed by lazy
|
|
|
|
if not keys.ft then
|
|
|
|
self:_set(keys)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Create a mapping if it is managed by lazy
|
2023-01-16 09:16:35 +00:00
|
|
|
---@param keys LazyKeys
|
2023-10-05 13:00:08 +01:00
|
|
|
---@param buf number?
|
2023-10-07 19:41:06 +01:00
|
|
|
function M:_set(keys, buf)
|
2023-10-08 09:11:25 +01:00
|
|
|
if keys.rhs then
|
2023-10-05 13:00:08 +01:00
|
|
|
local opts = M.opts(keys)
|
2023-10-08 09:11:25 +01:00
|
|
|
---@diagnostic disable-next-line: inject-field
|
2023-10-05 13:00:08 +01:00
|
|
|
opts.buffer = buf
|
2023-10-08 09:11:25 +01:00
|
|
|
vim.keymap.set(keys.mode, keys.lhs, keys.rhs, opts)
|
2022-12-22 09:32:21 +00:00
|
|
|
end
|
2022-12-05 13:45:50 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return M
|