2022-12-05 21:45:50 +08:00
|
|
|
local Util = require("lazy.core.util")
|
|
|
|
local Loader = require("lazy.core.loader")
|
|
|
|
|
2022-12-22 17:32:21 +08: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
|
|
|
|
|
2022-12-05 21:45:50 +08:00
|
|
|
---@class LazyKeysHandler:LazyHandler
|
|
|
|
local M = {}
|
|
|
|
|
2022-12-22 17:32:21 +08:00
|
|
|
function M.retrigger(keys)
|
|
|
|
local pending = ""
|
|
|
|
while true do
|
|
|
|
local c = vim.fn.getchar(0)
|
|
|
|
if c == 0 then
|
|
|
|
break
|
|
|
|
end
|
|
|
|
pending = pending .. vim.fn.nr2char(c)
|
|
|
|
end
|
|
|
|
local feed = vim.api.nvim_replace_termcodes(keys .. pending, true, true, true)
|
|
|
|
vim.api.nvim_feedkeys(feed, "m", false)
|
|
|
|
end
|
|
|
|
|
|
|
|
---@param value string|LazyKeys
|
|
|
|
function M.parse(value)
|
2022-12-23 00:49:32 +08:00
|
|
|
local ret = vim.deepcopy(value)
|
2022-12-23 04:58:01 +08:00
|
|
|
ret = type(ret) == "string" and { ret } or ret --[[@as LazyKeys]]
|
2022-12-23 00:49:32 +08:00
|
|
|
ret.mode = ret.mode or "n"
|
|
|
|
return ret
|
2022-12-22 17:32:21 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
function M.opts(keys)
|
|
|
|
local opts = {}
|
|
|
|
for k, v in pairs(keys) do
|
|
|
|
if type(k) ~= "number" and k ~= "mode" then
|
|
|
|
opts[k] = v
|
2022-12-22 02:01:58 +08:00
|
|
|
end
|
2022-12-22 17:32:21 +08:00
|
|
|
end
|
|
|
|
return opts
|
|
|
|
end
|
|
|
|
|
2022-12-23 04:58:01 +08:00
|
|
|
---@return string
|
|
|
|
function M:key(value)
|
|
|
|
if type(value) == "string" then
|
|
|
|
return value
|
|
|
|
end
|
|
|
|
local mode = value.mode or { "n" }
|
|
|
|
if type(mode) == "string" then
|
|
|
|
mode = { mode }
|
|
|
|
end
|
|
|
|
---@type string
|
|
|
|
local ret = value[1]
|
|
|
|
if #mode > 0 then
|
|
|
|
ret = table.concat(mode, ",") .. ": " .. ret
|
|
|
|
end
|
|
|
|
return ret
|
|
|
|
end
|
|
|
|
|
2022-12-22 17:32:21 +08:00
|
|
|
---@param value string|LazyKeys
|
|
|
|
function M:_add(value)
|
|
|
|
local keys = M.parse(value)
|
|
|
|
local lhs = keys[1]
|
2022-12-23 00:49:32 +08:00
|
|
|
local opts = M.opts(keys)
|
2022-12-23 04:58:01 +08:00
|
|
|
opts.noremap = true
|
2022-12-23 00:49:32 +08:00
|
|
|
vim.keymap.set(keys.mode, lhs, function()
|
2022-12-23 04:58:01 +08:00
|
|
|
pcall(vim.keymap.del, keys.mode, lhs)
|
2022-12-22 17:32:21 +08:00
|
|
|
Util.track({ keys = lhs })
|
2022-12-23 04:58:01 +08:00
|
|
|
Loader.load(self.active[self:key(value)], { keys = lhs })
|
2022-12-22 17:32:21 +08:00
|
|
|
M.retrigger(lhs)
|
2022-12-05 21:45:50 +08:00
|
|
|
Util.track()
|
2022-12-23 00:49:32 +08:00
|
|
|
end, opts)
|
2022-12-05 21:45:50 +08:00
|
|
|
end
|
|
|
|
|
2022-12-22 17:32:21 +08:00
|
|
|
---@param value string|LazyKeys
|
|
|
|
function M:_del(value)
|
|
|
|
local keys = M.parse(value)
|
2022-12-23 00:49:32 +08:00
|
|
|
pcall(vim.keymap.del, keys.mode, keys[1])
|
2022-12-22 17:32:21 +08:00
|
|
|
if keys[2] then
|
2022-12-23 00:49:32 +08:00
|
|
|
vim.keymap.set(keys.mode, keys[1], keys[2], M.opts(keys))
|
2022-12-22 17:32:21 +08:00
|
|
|
end
|
2022-12-05 21:45:50 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
return M
|