2022-12-05 13:45:50 +00:00
|
|
|
local Util = require("lazy.core.util")
|
|
|
|
local Loader = require("lazy.core.loader")
|
|
|
|
|
2022-12-22 09:32:21 +00:00
|
|
|
---@class LazyKeys
|
|
|
|
---@field [1] string lhs
|
|
|
|
---@field [2]? string|fun() rhs
|
|
|
|
---@field desc? string
|
|
|
|
---@field mode? string|string[]
|
|
|
|
---@field noremap? boolean
|
|
|
|
---@field remap? boolean
|
|
|
|
---@field expr? boolean
|
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 = {}
|
|
|
|
|
2022-12-22 09:32:21 +00:00
|
|
|
---@param value string|LazyKeys
|
|
|
|
function M.parse(value)
|
2022-12-22 16:49:32 +00:00
|
|
|
local ret = vim.deepcopy(value)
|
2022-12-22 20:58:01 +00:00
|
|
|
ret = type(ret) == "string" and { ret } or ret --[[@as LazyKeys]]
|
2022-12-22 16:49:32 +00:00
|
|
|
ret.mode = ret.mode or "n"
|
2023-01-16 09:16:35 +00:00
|
|
|
ret.id = (ret[1] or "")
|
|
|
|
if ret.mode then
|
|
|
|
local mode = ret.mode
|
|
|
|
if type(mode) == "table" then
|
|
|
|
---@cast mode string[]
|
|
|
|
table.sort(mode)
|
|
|
|
ret.id = ret.id .. " (" .. table.concat(mode, ", ") .. ")"
|
|
|
|
elseif mode ~= "n" then
|
|
|
|
ret.id = ret.id .. " (" .. mode .. ")"
|
|
|
|
end
|
|
|
|
end
|
2022-12-22 16:49:32 +00:00
|
|
|
return ret
|
2022-12-22 09:32:21 +00:00
|
|
|
end
|
|
|
|
|
2023-01-16 09:16:35 +00:00
|
|
|
---@param plugin LazyPlugin
|
|
|
|
function M:values(plugin)
|
|
|
|
---@type table<string,any>
|
|
|
|
local values = {}
|
|
|
|
---@diagnostic disable-next-line: no-unknown
|
|
|
|
for _, value in ipairs(plugin[self.type] or {}) do
|
|
|
|
local keys = M.parse(value)
|
2023-01-16 20:27:55 +00:00
|
|
|
if keys[2] == vim.NIL or keys[2] == false then
|
2023-01-16 09:16:35 +00:00
|
|
|
values[keys.id] = nil
|
|
|
|
else
|
|
|
|
values[keys.id] = keys
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return values
|
|
|
|
end
|
|
|
|
|
2022-12-22 09:32:21 +00:00
|
|
|
function M.opts(keys)
|
|
|
|
local opts = {}
|
|
|
|
for k, v in pairs(keys) do
|
2023-01-16 09:16:35 +00:00
|
|
|
if type(k) ~= "number" and k ~= "mode" and k ~= "id" then
|
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)
|
2022-12-22 09:32:21 +00:00
|
|
|
local lhs = keys[1]
|
2022-12-22 16:49:32 +00:00
|
|
|
local opts = M.opts(keys)
|
2023-02-07 23:45:28 +00:00
|
|
|
opts.remap = true
|
|
|
|
opts.expr = true
|
2022-12-22 16:49:32 +00:00
|
|
|
vim.keymap.set(keys.mode, lhs, function()
|
2023-01-16 09:16:35 +00:00
|
|
|
local plugins = self.active[keys.id]
|
2022-12-25 15:17:36 +00:00
|
|
|
|
|
|
|
-- always delete the mapping immediately to prevent recursive mappings
|
2023-01-16 09:16:35 +00:00
|
|
|
self:_del(keys)
|
|
|
|
self.active[keys.id] = nil
|
2022-12-25 15:17:36 +00:00
|
|
|
|
2022-12-22 09:32:21 +00:00
|
|
|
Util.track({ keys = lhs })
|
2022-12-25 15:17:36 +00:00
|
|
|
Loader.load(plugins, { keys = lhs })
|
2022-12-05 13:45:50 +00:00
|
|
|
Util.track()
|
2023-02-07 23:45:28 +00:00
|
|
|
|
|
|
|
return "<Ignore>" .. lhs
|
2022-12-22 16:49:32 +00:00
|
|
|
end, opts)
|
2022-12-05 13:45:50 +00:00
|
|
|
end
|
|
|
|
|
2023-01-16 09:16:35 +00:00
|
|
|
---@param keys LazyKeys
|
|
|
|
function M:_del(keys)
|
2022-12-22 16:49:32 +00:00
|
|
|
pcall(vim.keymap.del, keys.mode, keys[1])
|
2022-12-22 09:32:21 +00:00
|
|
|
if keys[2] then
|
2022-12-22 16:49:32 +00:00
|
|
|
vim.keymap.set(keys.mode, keys[1], keys[2], M.opts(keys))
|
2022-12-22 09:32:21 +00:00
|
|
|
end
|
2022-12-05 13:45:50 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return M
|