-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.lua
218 lines (171 loc) · 5.8 KB
/
init.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
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
require('user_api.types.user.maps')
local Value = require('user_api.check.value')
local Util = require('user_api.util')
local is_nil = Value.is_nil
local is_tbl = Value.is_tbl
local is_str = Value.is_str
local is_int = Value.is_int
local is_bool = Value.is_bool
local empty = Value.empty
local field = Value.fields
local strip_fields = Util.strip_fields
local MODES = { 'n', 'i', 'v', 't', 'o', 'x' }
---@type User.Maps
---@diagnostic disable-next-line:missing-fields
local M = {}
M.kmap = require('user_api.maps.kmap')
M.wk = require('user_api.maps.wk')
M.modes = MODES
function M.nop(T, opts, mode, prefix)
if not (is_str(T) or is_tbl(T)) then
error('(user_api.maps.nop): Argument is neither a string nor a table')
end
mode = (is_str(mode) and vim.tbl_contains(MODES, mode)) and mode or 'n'
opts = is_tbl(opts) and opts or {}
opts.silent = is_bool(opts.silent) and opts.silent or true
if is_int(opts.buffer) then
---@type User.Maps.Keymap.Opts
opts = strip_fields(opts, 'buffer')
end
prefix = is_str(prefix) and prefix or ''
---@type KeyMapFunction
local func = M.kmap[mode]
if is_str(T) then
func(prefix .. T, '<Nop>', opts)
return
end
for _, v in next, T do
func(prefix .. v, '<Nop>', opts)
end
end
function M.map_dict(T, map_func, dict_has_modes, mode, bufnr)
if not (is_tbl(T) and not empty(T)) then
vim.notify("Keys either aren't table or table is empty", 'error', {
timeout = 700,
title = '(user_api.maps.map_dict)',
})
return
end
if is_str(mode) and not vim.tbl_contains(MODES, mode) then
mode = 'n'
elseif is_tbl(mode) and not empty(mode) then
for _, v in next, mode do
if not vim.tbl_contains(MODES, v) then
error('(user_api.maps.map_dict): Bad modes table', vim.log.levels.ERROR)
end
end
elseif is_tbl(mode) and empty(mode) then
mode = 'n'
end
local map_choices = { 'kmap', 'wk.register' }
map_func = (is_str(map_func) and vim.tbl_contains(map_choices)) and map_func or 'wk.register'
map_func = M.wk.available() and map_func or 'kmap'
mode = (is_str(mode) and vim.tbl_contains(MODES, mode)) and mode or 'n'
dict_has_modes = is_bool(dict_has_modes) and dict_has_modes or false
bufnr = is_int(bufnr) and bufnr or nil
---@type KeyMapFunction
local func
if dict_has_modes then
for mode_choice, t in next, T do
if not vim.tbl_contains(MODES, mode_choice) then
goto continue
end
if map_func == 'kmap' then
---@type KeyMapFunction
func = M.kmap[mode_choice]
for lhs, v in next, t do
v[2] = is_tbl(v[2]) and v[2] or {}
func(lhs, v[1], v[2])
end
goto continue
end
for lhs, v in next, t do
local tbl = {}
if is_str(lhs) then
table.insert(tbl, lhs)
else
goto continue
end
if not is_nil(v[1]) then
table.insert(tbl, v[1])
end
tbl.mode = mode_choice
if is_int(bufnr) then
tbl.buffer = bufnr
end
if is_str(v.group) then
tbl.group = v.group
end
if is_tbl(v[2]) and is_str(v[2].desc) then
tbl.desc = v[2].desc
end
if is_tbl(v[2]) and is_bool(v[2].expr) then
tbl.expr = v[2].expr
end
if is_tbl(v[2]) and is_bool(v[2].noremap) then
tbl.noremap = v[2].noremap
end
if is_tbl(v[2]) and is_bool(v[2].nowait) then
tbl.nowait = v[2].nowait
end
if is_tbl(v[2]) and is_bool(v[2].silent) then
tbl.silent = v[2].silent
end
if is_bool(v.hidden) then
tbl.hidden = v.hidden
end
require('which-key').add(tbl)
::continue::
end
::continue::
end
elseif map_func == 'kmap' then
---@type KeyMapFunction
func = M.kmap[mode]
for lhs, v in next, T do
v[2] = is_tbl(v[2]) and v[2] or {}
func(lhs, v[1], v[2])
end
else
for lhs, v in next, T do
local tbl = {}
if is_str(lhs) then
table.insert(tbl, lhs)
else
goto continue
end
if not is_nil(v[1]) then
table.insert(tbl, v[1])
end
tbl.mode = mode
if not is_nil(bufnr) then
tbl.buffer = bufnr
end
if is_str(v.group) then
tbl.group = v.group
end
if is_tbl(v[2]) and is_str(v[2].desc) then
tbl.desc = v[2].desc
end
if is_tbl(v[2]) and is_bool(v[2].expr) then
tbl.expr = v[2].expr
end
if is_tbl(v[2]) and is_bool(v[2].noremap) then
tbl.noremap = v[2].noremap
end
if is_tbl(v[2]) and is_bool(v[2].nowait) then
tbl.nowait = v[2].nowait
end
if is_tbl(v[2]) and is_bool(v[2].silent) then
tbl.silent = v[2].silent
end
if is_bool(v.hidden) then
tbl.hidden = v.hidden
end
require('which-key').add(tbl)
::continue::
end
end
end
return M
--- vim:ts=4:sts=4:sw=4:et:ai:si:sta:noci:nopi: