-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaps.lua
More file actions
247 lines (219 loc) · 6.38 KB
/
maps.lua
File metadata and controls
247 lines (219 loc) · 6.38 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
---@module 'user_api.maps._meta'
local MODES = { 'n', 'i', 'v', 't', 'o', 'x' }
local ERROR = vim.log.levels.ERROR
local WARN = vim.log.levels.WARN
local in_list = vim.list_contains
local validate = require('user_api.check').validate
local Maps = {} ---@type User.Maps
Maps.modes = MODES
Maps.keymap = require('user_api.maps.keymap')
Maps.wk = require('user_api.maps.wk')
function Maps.desc(desc, silent, bufnr, noremap, nowait, expr)
validate({
desc = { desc, { 'string', 'nil' }, true },
silent = { silent, { 'boolean', 'nil' }, true },
bufnr = { bufnr, { 'number', 'nil' }, true },
noremap = { noremap, { 'boolean', 'nil' }, true },
nowait = { nowait, { 'boolean', 'nil' }, true },
expr = { expr, { 'boolean', 'nil' }, true },
})
desc = (desc and desc ~= '') and desc or 'Unnamed Key'
local res = require('user_api.maps.objects').new()
res:add({ desc = desc, silent = true, noremap = true })
if noremap ~= nil then
res:add({ noremap = noremap })
end
if silent ~= nil then
res:add({ silent = silent })
end
if nowait ~= nil then
res:add({ nowait = nowait })
end
if expr ~= nil then
res:add({ expr = expr })
end
if bufnr ~= nil then
res:add({ buffer = bufnr })
end
return res
end
function Maps.nop(T, opts, mode, prefix)
validate({
T = { T, { 'string', 'table' } },
opts = { opts, { 'table', 'nil' }, true },
mode = { mode, { 'string', 'nil' }, true },
prefix = { prefix, { 'string', 'nil' }, true },
})
local Value = require('user_api.check.value')
mode = (Value.is_str(mode) and in_list(MODES, mode)) and mode or 'n'
if mode == 'i' then
vim.notify(
'(user_api.maps.nop): Refusing to NO-OP these keys in Insert mode: ' .. vim.inspect(T),
WARN
)
return
end
opts = opts or {}
opts.silent = Value.is_bool(opts.silent) and opts.silent or true
if Value.is_int(opts.buffer) then
opts = require('user_api.util').strip_fields(opts, 'buffer') ---@type User.Maps.Opts
end
prefix = prefix or ''
local func = Maps.keymap[mode]
if Value.is_str(T) then
---@cast T string
func(prefix .. T, '<Nop>', opts)
return
end
---@cast T string[]
for _, v in ipairs(T) do
func(prefix .. v, '<Nop>', opts)
end
end
function Maps.map_dict(T, map_func, has_modes, mode, bufnr)
validate({
T = { T, { 'table' } },
map_func = { map_func, { 'string' } },
has_modes = { has_modes, { 'boolean', 'nil' }, true },
mode = { mode, { 'string', 'table', 'nil' }, true },
bufnr = { bufnr, { 'number', 'nil' }, true },
})
local Value = require('user_api.check.value')
if not Value.type_not_empty('table', T) then
error("(user_api.maps.map_dict): Keys either aren't table or table is empty", ERROR)
end
local map_choices = { 'keymap', 'wk.register' }
map_func = in_list(map_choices, map_func) and map_func or 'wk.register'
if not Maps.wk.available() then
map_func = 'keymap'
end
mode = (Value.is_str(mode) and in_list(MODES, mode)) and mode or 'n'
has_modes = Value.is_bool(has_modes) and has_modes or false
bufnr = Value.is_int(bufnr) and bufnr or nil
local func
if has_modes then
local keymap_ran = false
---@cast T AllModeMaps
for mode_choice, t in pairs(T) do
if in_list(MODES, mode_choice) then
if map_func == 'keymap' then
func = Maps.keymap[mode_choice]
for lhs, v in pairs(t) do
if v[2] and v[3] then
func(lhs, v[2], v[3] or {})
elseif v[1] then
func(lhs, v[1], v[2] or {})
end
end
keymap_ran = true
end
for lhs, v in pairs(t) do
if keymap_ran then
break
end
if Value.is_str(lhs) then
local tbl = {}
table.insert(tbl, lhs)
if v[1] ~= nil then
table.insert(tbl, v[1])
end
tbl.mode = mode_choice
if bufnr ~= nil then
tbl.buffer = bufnr
end
if Value.is_str(v.proxy) then
tbl.proxy = v.proxy
end
if Value.is_str(v.group) then
tbl.group = v.group
end
if Value.is_bool(v.hidden) then
tbl.hidden = v.hidden
end
if not Value.is_tbl(v[2]) then
v[2] = {}
end
if Value.is_str(v[2].desc) then
tbl.desc = v[2].desc
end
if Value.is_bool(v[2].expr) then
tbl.expr = v[2].expr
end
if Value.is_bool(v[2].noremap) then
tbl.noremap = v[2].noremap
end
if Value.is_bool(v[2].nowait) then
tbl.nowait = v[2].nowait
end
if Value.is_bool(v[2].silent) then
tbl.silent = v[2].silent
end
require('which-key').add(tbl)
end
end
end
end
return
end
if map_func == 'keymap' then
func = Maps.keymap[mode]
---@cast T AllMaps
for lhs, v in pairs(T) do
if v[2] and v[3] then
func(lhs, v[2], v[3])
elseif v[1] then
func(lhs, v[1], v[2] or {})
end
end
return
end
---@cast T AllMaps
for lhs, v in pairs(T) do
local tbl = {}
if Value.is_str(lhs) then
table.insert(tbl, lhs)
if v[1] ~= nil then
table.insert(tbl, v[1])
end
tbl.mode = mode
if bufnr ~= nil then
tbl.buffer = bufnr
end
if Value.is_str(v.proxy) then
tbl.proxy = v.proxy
end
if Value.is_str(v.group) then
tbl.group = v.group
end
if Value.is_bool(v.hidden) then
tbl.hidden = v.hidden
end
if Value.is_tbl(v[2]) then
if Value.is_str(v[2].desc) then
tbl.desc = v[2].desc
end
if Value.is_bool(v[2].expr) then
tbl.expr = v[2].expr
end
if Value.is_bool(v[2].noremap) then
tbl.noremap = v[2].noremap
end
if Value.is_bool(v[2].nowait) then
tbl.nowait = v[2].nowait
end
if Value.is_bool(v[2].silent) then
tbl.silent = v[2].silent
end
end
require('which-key').add(tbl)
end
end
end
local M = setmetatable(Maps, { ---@type User.Maps
__index = Maps,
__newindex = function()
vim.notify('User.Maps is Read-Only!', ERROR)
end,
})
return M
-- vim: set ts=2 sts=2 sw=2 et ai si sta: