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
|
|
|
|
|
2022-12-05 13:45:50 +00:00
|
|
|
---@class LazyKeysHandler:LazyHandler
|
|
|
|
local M = {}
|
|
|
|
|
2023-01-04 06:54:35 +00:00
|
|
|
---@param feed string
|
|
|
|
function M.replace_special(feed)
|
|
|
|
for special, key in pairs({ leader = vim.g.mapleader, localleader = vim.g.maplocalleader }) do
|
|
|
|
local pattern = "<"
|
|
|
|
for i = 1, #special do
|
|
|
|
pattern = pattern .. "[" .. special:sub(i, i) .. special:upper():sub(i, i) .. "]"
|
|
|
|
end
|
|
|
|
pattern = pattern .. ">"
|
|
|
|
feed = feed:gsub(pattern, key)
|
|
|
|
end
|
|
|
|
return feed
|
|
|
|
end
|
|
|
|
|
2022-12-22 09:32:21 +00:00
|
|
|
function M.retrigger(keys)
|
|
|
|
local pending = ""
|
|
|
|
while true do
|
2022-12-26 14:55:40 +00:00
|
|
|
---@type number|string
|
2022-12-22 09:32:21 +00:00
|
|
|
local c = vim.fn.getchar(0)
|
|
|
|
if c == 0 then
|
|
|
|
break
|
|
|
|
end
|
2022-12-26 14:55:40 +00:00
|
|
|
c = type(c) == "number" and vim.fn.nr2char(c) or c
|
|
|
|
pending = pending .. c
|
2022-12-22 09:32:21 +00:00
|
|
|
end
|
2023-01-03 10:17:37 +00:00
|
|
|
local op = vim.v.operator
|
2023-01-03 15:16:49 +00:00
|
|
|
if op and op ~= "" and vim.api.nvim_get_mode().mode:find("o") then
|
2023-01-03 10:16:15 +00:00
|
|
|
keys = "<esc>" .. op .. keys
|
|
|
|
end
|
2023-01-04 06:54:35 +00:00
|
|
|
local feed = keys .. pending
|
|
|
|
feed = M.replace_special(feed)
|
2022-12-30 19:51:13 +00:00
|
|
|
if vim.v.count ~= 0 then
|
|
|
|
feed = vim.v.count .. feed
|
|
|
|
end
|
2023-01-04 06:54:35 +00:00
|
|
|
vim.notify(feed)
|
2023-01-03 15:16:49 +00:00
|
|
|
vim.api.nvim_input(feed)
|
2022-12-22 09:32:21 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
---@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"
|
|
|
|
return ret
|
2022-12-22 09:32:21 +00: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-21 18:01:58 +00:00
|
|
|
end
|
2022-12-22 09:32:21 +00:00
|
|
|
end
|
|
|
|
return opts
|
|
|
|
end
|
|
|
|
|
2022-12-22 20:58:01 +00: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 09:32:21 +00:00
|
|
|
---@param value string|LazyKeys
|
|
|
|
function M:_add(value)
|
|
|
|
local keys = M.parse(value)
|
|
|
|
local lhs = keys[1]
|
2022-12-22 16:49:32 +00:00
|
|
|
local opts = M.opts(keys)
|
|
|
|
vim.keymap.set(keys.mode, lhs, function()
|
2022-12-25 15:17:36 +00:00
|
|
|
local key = self:key(value)
|
|
|
|
local plugins = self.active[key]
|
|
|
|
|
|
|
|
-- always delete the mapping immediately to prevent recursive mappings
|
|
|
|
self:_del(value)
|
|
|
|
self.active[key] = nil
|
|
|
|
|
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-22 09:32:21 +00:00
|
|
|
M.retrigger(lhs)
|
2022-12-05 13:45:50 +00:00
|
|
|
Util.track()
|
2022-12-22 16:49:32 +00:00
|
|
|
end, opts)
|
2022-12-05 13:45:50 +00:00
|
|
|
end
|
|
|
|
|
2022-12-22 09:32:21 +00:00
|
|
|
---@param value string|LazyKeys
|
|
|
|
function M:_del(value)
|
|
|
|
local keys = M.parse(value)
|
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
|