-
Notifications
You must be signed in to change notification settings - Fork 418
/
Copy pathkeys.lua
95 lines (84 loc) · 2.31 KB
/
keys.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
local Util = require("lazy.core.util")
local Loader = require("lazy.core.loader")
---@class LazyKeys
---@field [1] string lhs
---@field [2]? string|fun()|false rhs
---@field desc? string
---@field mode? string|string[]
---@field noremap? boolean
---@field remap? boolean
---@field expr? boolean
---@field id string
---@class LazyKeysHandler:LazyHandler
local M = {}
---@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"
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
return ret
end
---@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)
if keys[2] == vim.NIL or keys[2] == false then
values[keys.id] = nil
else
values[keys.id] = keys
end
end
return values
end
function M.opts(keys)
local opts = {}
for k, v in pairs(keys) do
if type(k) ~= "number" and k ~= "mode" and k ~= "id" then
opts[k] = v
end
end
return opts
end
---@param keys LazyKeys
function M:_add(keys)
local lhs = keys[1]
local opts = M.opts(keys)
vim.keymap.set(keys.mode, lhs, function()
local plugins = self.active[keys.id]
-- always delete the mapping immediately to prevent recursive mappings
self:_del(keys)
self.active[keys.id] = nil
Util.track({ keys = lhs })
Loader.load(plugins, { keys = lhs })
Util.track()
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,
-- we do not return anything, but this is still needed to make operator pending mappings work
expr = true,
})
end
---@param keys LazyKeys
function M:_del(keys)
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