generated from mrcjkb/nvim-lua-nix-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathkeys.lua
173 lines (158 loc) · 5.02 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
local loader = require("lz.n.loader")
---@class lz.n.KeysHandler: lz.n.Handler
---@param value lz.n.KeysSpec
---@param mode? string
---@return lz.n.Keys
local function parse_keys_spec(value, mode)
local ret = vim.deepcopy(value) --[[@as lz.n.Keys]]
ret.lhs = ret[1] or ""
ret.rhs = ret[2]
ret[1] = nil
ret[2] = nil
ret.mode = mode or "n"
ret.id = vim.api.nvim_replace_termcodes(ret.lhs, true, true, true)
if ret.ft then
local ft = type(ret.ft) == "string" and { ret.ft } or ret.ft --[[@as string[] ]]
ret.id = ret.id .. " (" .. table.concat(ft, ", ") .. ")"
end
if ret.mode ~= "n" then
ret.id = ret.id .. " (" .. ret.mode .. ")"
end
return ret
end
---@param value string|lz.n.KeysSpec
---@return lz.n.Keys[]
local function parse(value)
value = type(value) == "string" and { value } or value --[[@as lz.n.KeysSpec]]
local modes = type(value.mode) == "string" and { value.mode } or value.mode --[[ @as string[] | nil ]]
if not modes then
return { parse_keys_spec(value) }
end
return vim.iter(modes)
:map(function(mode)
return parse_keys_spec(value, mode)
end)
:totable()
end
---@type lz.n.handler.State
local state = require("lz.n.handler.state").new()
---@type lz.n.KeysHandler
local M = {
spec_field = "keys",
---@param keys_spec? string|string[]|lz.n.KeysSpec[]
parse = function(plugin, keys_spec)
if keys_spec then
plugin.keys = {}
end
if type(keys_spec) == "string" then
local keys = parse(keys_spec)
vim.list_extend(plugin.keys, keys)
elseif type(keys_spec) == "table" then
---@param keys_spec_ string | lz.n.KeysSpec
vim.iter(keys_spec):each(function(keys_spec_)
local keys = parse(keys_spec_)
vim.list_extend(plugin.keys, keys)
end)
end
end,
}
---@param name string
---@return lz.n.Plugin?
function M.lookup(name)
return state.lookup_plugin(name)
end
local skip = { mode = true, id = true, ft = true, rhs = true, lhs = true }
---@param keys lz.n.Keys
---@return lz.n.KeysBase
local function get_opts(keys)
---@type lz.n.KeysBase
return vim.iter(keys):fold({}, function(acc, k, v)
if type(k) ~= "number" and not skip[k] then
acc[k] = v
end
return acc
end)
end
-- Create a mapping if it is managed by lz.n
---@param keys lz.n.Keys
---@param buf integer?
local function set(keys, buf)
if keys.rhs then
local opts = get_opts(keys)
---@diagnostic disable-next-line: inject-field
opts.buffer = buf
vim.keymap.set(keys.mode, keys.lhs, keys.rhs, opts)
end
end
-- Delete a mapping and create the real global
-- mapping when needed
---@param keys lz.n.Keys
local function del(keys)
pcall(vim.keymap.del, keys.mode, keys.lhs, {
-- NOTE: for buffer-local mappings, we only delete the mapping for the current buffer
-- So the mapping could still exist in other buffers
buffer = keys.ft and true or nil,
})
-- make sure to create global mappings when needed
-- buffer-local mappings are managed by lz.n
if not keys.ft then
set(keys)
end
end
---@param keys lz.n.Keys
local function add_keys(keys)
local lhs = keys.lhs
local opts = get_opts(keys)
---@param buf? number
local function add(buf)
vim.keymap.set(keys.mode, lhs, function()
-- always delete the mapping immediately to prevent recursive mappings
del(keys)
state.each_pending(keys.id, loader.load)
-- Create the real buffer-local mapping
if keys.ft then
set(keys, buf)
end
if keys.mode:sub(-1) == "a" then
lhs = lhs .. "<C-]>"
end
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,
nowait = opts.nowait,
-- we do not return anything, but this is still needed to make operator pending mappings work
expr = true,
buffer = buf,
})
end
-- buffer-local mappings
if keys.ft then
vim.api.nvim_create_autocmd("FileType", {
pattern = keys.ft,
nested = true,
callback = function(event)
if state.has_pending_plugins[keys.id] then
add(event.buf)
else
-- Only create the mapping if its managed by lz.n
-- otherwise the plugin is supposed to manage it
set(keys, event.buf)
end
end,
})
else
add()
end
end
---@param plugin lz.n.Plugin
function M.add(plugin)
---@param key lz.n.Keys
vim.iter(plugin.keys or {}):each(function(key)
state.insert(key.id, plugin)
add_keys(key)
end)
end
M.del = state.del
return M