mirror of https://github.com/folke/lazy.nvim.git
89 lines
1.9 KiB
Lua
89 lines
1.9 KiB
Lua
local Util = require("lazy.core.util")
|
|
local Loader = require("lazy.core.loader")
|
|
|
|
---@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
|
|
|
|
---@class LazyKeysHandler:LazyHandler
|
|
local M = {}
|
|
|
|
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)
|
|
local ret = vim.deepcopy(value)
|
|
ret = type(ret) == "string" and { ret } or ret --[[@as LazyKeys]]
|
|
ret.mode = ret.mode or "n"
|
|
return ret
|
|
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
|
|
end
|
|
end
|
|
return opts
|
|
end
|
|
|
|
---@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
|
|
|
|
---@param value string|LazyKeys
|
|
function M:_add(value)
|
|
local keys = M.parse(value)
|
|
local lhs = keys[1]
|
|
local opts = M.opts(keys)
|
|
opts.noremap = true
|
|
vim.keymap.set(keys.mode, lhs, function()
|
|
pcall(vim.keymap.del, keys.mode, lhs)
|
|
Util.track({ keys = lhs })
|
|
Loader.load(self.active[self:key(value)], { keys = lhs })
|
|
M.retrigger(lhs)
|
|
Util.track()
|
|
end, opts)
|
|
end
|
|
|
|
---@param value string|LazyKeys
|
|
function M:_del(value)
|
|
local keys = M.parse(value)
|
|
pcall(vim.keymap.del, keys.mode, keys[1])
|
|
if keys[2] then
|
|
vim.keymap.set(keys.mode, keys[1], keys[2], M.opts(keys))
|
|
end
|
|
end
|
|
|
|
return M
|