forked from nvim-mini/mini.nvim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpairs.lua
More file actions
526 lines (469 loc) · 19.8 KB
/
Copy pathpairs.lua
File metadata and controls
526 lines (469 loc) · 19.8 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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
-- MIT License Copyright (c) 2021 Evgeni Chasnovski
-- Documentation ==============================================================
---@brief [[
--- Custom minimal and fast autopairs Lua module. It provides functionality
--- to work with 'paired' characters conditional on cursor's neighborhood (two
--- characters to its left and right). Its usage should be through making
--- appropriate mappings using |MiniPairs.map| or in |MiniPairs.setup| (for
--- global mapping), |MiniPairs.map_buf| (for buffer mapping). Pairs get
--- automatically registered to be recognized by `<BS>` and `<CR>`.
---
--- What it doesn't do:
--- - It doesn't support multiple characters as "open" and "close" symbols. Use
--- snippets for that.
--- - It doesn't support dependency on filetype. Use |i_CTRL-V| to insert
--- single symbol or `autocmd` command or 'after/ftplugin' approach to:
--- - `lua MiniPairs.map_buf(0, 'i', <*>, <pair_info>)` : make new mapping
--- for '<*>' in current buffer.
--- - `lua MiniPairs.unmap_buf(0, 'i', <*>, <pair>)`: unmap key `<*>` while
--- unregistering `<pair>` pair in current buffer.
--- - Disable module for buffer (see 'Disabling' section).
---
--- # Setup
---
--- This module needs a setup with `require('mini.pairs').setup({})`
--- (replace `{}` with your `config` table). It will create global Lua table
--- `MiniPairs` which you can use for scripting or manually (with
--- `:lua MiniPairs.*`).
---
--- Default `config`:
--- <code>
--- {
--- -- In which modes mappings from this `config` should be created
--- modes = {insert = true, command = false, terminal = false}
---
--- -- Global mappings. Each right hand side should be a pair information, a
--- -- table with at least these fields (see more in |MiniPairs.map|):
--- -- - `action` - one of 'open', 'close', 'closeopen'.
--- -- - `pair` - two character string for pair to be used.
--- -- By default pair is not inserted after `\`, quotes are not recognized by
--- -- `<CR>`, `'` does not insert pair after a letter.
--- -- Only parts of the tables can be tweaked (others will use these defaults).
--- mappings = {
--- ['('] = { action = 'open', pair = '()', neigh_pattern = '[^\\].' },
--- ['['] = { action = 'open', pair = '[]', neigh_pattern = '[^\\].' },
--- ['{'] = { action = 'open', pair = '{}', neigh_pattern = '[^\\].' },
---
--- [')'] = { action = 'close', pair = '()', neigh_pattern = '[^\\].' },
--- [']'] = { action = 'close', pair = '[]', neigh_pattern = '[^\\].' },
--- ['}'] = { action = 'close', pair = '{}', neigh_pattern = '[^\\].' },
---
--- ['"'] = { action = 'closeopen', pair = '""', neigh_pattern = '[^\\].', register = { cr = false } },
--- ["'"] = { action = 'closeopen', pair = "''", neigh_pattern = '[^%a\\].', register = { cr = false } },
--- ['`'] = { action = 'closeopen', pair = '``', neigh_pattern = '[^\\].', register = { cr = false } },
--- },
--- }
--- </code>
---
--- # Example mappings
---
--- <pre>
--- - Register quotes inside `config` of |MiniPairs.setup|:
--- `mappings = {`
--- ` ['"'] = { register = { cr = true } },`
--- ` ["'"] = { register = { cr = true } },`
--- `}`
--- - Insert `<>` pair if `<` is typed as first character in line, don't register for `<CR>`:
--- `lua MiniPairs.map('i', '<', { action = 'open', pair = '<>', neigh_pattern = '\r.', register = { cr = false } })`
--- `lua MiniPairs.map('i', '>', { action = 'close', pair = '<>', register = { cr = false } })`
--- - Create symmetrical `$$` pair only in Tex files:
--- `au FileType tex lua MiniPairs.map_buf(0, 'i', '$', {action = 'closeopen', pair = '$$'})`
--- </pre>
---
--- # Notes
--- - Make sure to make proper mapping of `<CR>` in order to support completion
--- plugin of your choice.
--- - Having mapping in terminal mode can conflict with:
--- - Autopairing capabilities of interpretators (`ipython`, `radian`).
--- - Vim mode of terminal itself.
---
--- # Disabling
---
--- To disable, set `g:minipairs_disable` (globally) or `b:minipairs_disable`
--- (for a buffer) to `v:true`.
---@brief ]]
---@tag MiniPairs mini.pairs
-- Module definition ==========================================================
local MiniPairs = {}
local H = {}
--- Module setup
---
---@param config table: Module config table.
---@usage `require('mini.completion').setup({})` (replace `{}` with your `config` table)
function MiniPairs.setup(config)
-- Export module
_G.MiniPairs = MiniPairs
-- Setup config
config = H.setup_config(config)
-- Apply config
H.apply_config(config)
-- Module behavior
vim.api.nvim_exec(
[[augroup MiniPairs
au!
au FileType TelescopePrompt let b:minipairs_disable=v:true
au FileType fzf let b:minipairs_disable=v:true
augroup END]],
false
)
end
MiniPairs.config = {
-- In which modes mappings from this `config` should be created
modes = { insert = true, command = false, terminal = false },
-- Global mappings. Each right hand side should be a pair information, a
-- table with at least these fields (see more in |MiniPairs.map|):
-- - `action` - one of 'open', 'close', 'closeopen'.
-- - `pair` - two character string for pair to be used.
-- By default pair is not inserted after `\`, quotes are not recognized by
-- `<CR>`, `'` does not insert pair after a letter.
-- Only parts of the tables can be tweaked (others will use these defaults).
mappings = {
['('] = { action = 'open', pair = '()', neigh_pattern = '[^\\].' },
['['] = { action = 'open', pair = '[]', neigh_pattern = '[^\\].' },
['{'] = { action = 'open', pair = '{}', neigh_pattern = '[^\\].' },
[')'] = { action = 'close', pair = '()', neigh_pattern = '[^\\].' },
[']'] = { action = 'close', pair = '[]', neigh_pattern = '[^\\].' },
['}'] = { action = 'close', pair = '{}', neigh_pattern = '[^\\].' },
['"'] = { action = 'closeopen', pair = '""', neigh_pattern = '[^\\].', register = { cr = false } },
["'"] = { action = 'closeopen', pair = "''", neigh_pattern = '[^%a\\].', register = { cr = false } },
['`'] = { action = 'closeopen', pair = '``', neigh_pattern = '[^\\].', register = { cr = false } },
},
}
-- Module functionality =======================================================
--- Make global mapping
---
--- This is similar to |nvim_set_keymap()| but instead of right hand side of
--- mapping (as string) it expects table with pair information:
--- - `action` - one of 'open' (for |MiniPairs.open|), 'close' (for
--- |MiniPairs.close|), or 'closeopen' (for |MiniPairs.closeopen|).
--- - `pair` - two character string to be used as argument for action function.
--- - `neigh_pattern` - optional 'two character' neighborhood pattern to be
--- used as argument for action function. Default: '..' (no restriction from
--- neighborhood).
--- - `register` - optional table with information about whether this pair
--- should be recognized by `<BS>` (in |MiniPairs.bs|) and/or `<CR>` (in
--- |MiniPairs.cr|). Should have boolean elements `bs` and `cr` which are
--- both `true` by default (if not overriden explicitly).
---
--- Using this function instead of |nvim_set_keymap()| allows automatic
--- registration of pairs which will be recognized by `<BS>` and `<CR>`.
---
---@param mode string: `mode` for |nvim_set_keymap()|.
---@param lhs string: `lhs` for |nvim_set_keymap()|.
---@param pair_info table: Table with pair information.
---@param opts table: Optional table `opts` for |nvim_set_keymap()|. Elements `expr` and `noremap` won't be recognized (`true` by default).
function MiniPairs.map(mode, lhs, pair_info, opts)
pair_info = H.ensure_pair_info(pair_info)
opts = vim.tbl_deep_extend('force', opts or {}, { expr = true, noremap = true })
vim.api.nvim_set_keymap(mode, lhs, H.pair_info_to_map_rhs(pair_info), opts)
H.register_pair(pair_info, mode, 'all')
end
--- Make buffer mapping
---
--- This is similar to |nvim_buf_set_keymap()| but instead of string right hand
--- side of mapping it expects table with pair information similar to one in
--- |MiniPairs.map|.
---
--- Using this function instead of |nvim_buf_set_keymap()| allows automatic
--- registration of pairs which will be recognized by `<BS>` and `<CR>`.
---
---@param mode string: `mode` for |nvim_buf_set_keymap()|.
---@param lhs string: `lhs` for |nvim_buf_set_keymap()|.
---@param pair_info table: Table with pair information.
---@param opts table: Optional table `opts` for |nvim_buf_set_keymap()|. Elements `expr` and `noremap` won't be recognized (`true` by default).
function MiniPairs.map_buf(buffer, mode, lhs, pair_info, opts)
pair_info = H.ensure_pair_info(pair_info)
opts = vim.tbl_deep_extend('force', opts or {}, { expr = true, noremap = true })
vim.api.nvim_buf_set_keymap(buffer, mode, lhs, H.pair_info_to_map_rhs(pair_info), opts)
H.register_pair(pair_info, mode, buffer == 0 and vim.api.nvim_get_current_buf() or buffer)
end
--- Remove global mapping
---
--- Uses |nvim_del_keymap()| together with unregistering supplied `pair`.
---
---@param mode string: `mode` for |nvim_del_keymap()|.
---@param lhs string: `lhs` for |nvim_del_keymap()|.
---@param pair string: pair which should be unregistered. Supply `''` to not unregister pair.
function MiniPairs.unmap(mode, lhs, pair)
vim.api.nvim_del_keymap(mode, lhs)
if pair == nil then
vim.notify([[(mini.pairs) Supply `pair` argument to `MiniPairs.unmap`.]])
end
if (pair or '') ~= '' then
H.unregister_pair(pair, mode, 'all')
end
end
--- Remove buffer mapping
---
--- Uses |nvim_buf_del_keymap()| together with unregistering supplied `pair`.
---
---@param mode string: `mode` for |nvim_buf_del_keymap()|.
---@param lhs string: `lhs` for |nvim_buf_del_keymap()|.
---@param pair string: pair which should be unregistered. Supply `''` to not unregister pair.
function MiniPairs.unmap_buf(buffer, mode, lhs, pair)
vim.api.nvim_buf_del_keymap(buffer, mode, lhs)
if pair == nil then
vim.notify([[(mini.pairs) Supply `pair` argument to `MiniPairs.unmap_buf`.]])
end
if (pair or '') ~= '' then
H.unregister_pair(pair, mode, buffer == 0 and vim.api.nvim_get_current_buf() or buffer)
end
end
--- Process 'open' symbols
---
--- Used as |map-expr| mapping for 'open' symbols in asymmetric pair ('(', '[',
--- etc.). If neighborhood doesn't match supplied pattern, function results
--- into 'open' symbol. Otherwise, it pastes whole pair and moves inside pair
--- with |<Left>|.
---
--- Used inside |MiniPairs.map| and |MiniPairs.map_buf| for an actual mapping.
---
---@param pair string: String with two characters representing pair.
---@param neigh_pattern string: Pattern for two neighborhood characters ("\r" line start, "\n" - line end).
function MiniPairs.open(pair, neigh_pattern)
if H.is_disabled() or not H.neigh_match(neigh_pattern) then
return pair:sub(1, 1)
end
return ('%s%s'):format(pair, H.get_arrow_key('left'))
end
--- Process 'close' symbols
---
--- Used as |map-expr| mapping for 'close' symbols in asymmetric pair (')',
--- ']', etc.). If neighborhood doesn't match supplied pattern, function
--- results into 'close' symbol. Otherwise it jumps over symbol to the right of
--- cursor (with |<Right>|) if it is equal to 'close' one and inserts it
--- otherwise.
---
--- Used inside |MiniPairs.map| and |MiniPairs.map_buf| for an actual mapping.
---
---@param pair string: String with two characters representing pair.
---@param neigh_pattern string: Pattern for two neighborhood characters ("\r" line start, "\n" - line end).
function MiniPairs.close(pair, neigh_pattern)
if H.is_disabled() or not H.neigh_match(neigh_pattern) then
return pair:sub(2, 2)
end
local close = pair:sub(2, 2)
if H.get_cursor_neigh(1, 1) == close then
return H.get_arrow_key('right')
else
return close
end
end
--- Process 'closeopen' symbols
---
--- Used as |map-expr| mapping for 'symmetrical' symbols (from pairs '""',
--- '\'\'', '``'). It tries to perform 'closeopen action': move over right
--- character (with |<Right>|) if it is equal to second character from pair or
--- conditionally paste pair otherwise (with |MiniPairs.open()|).
---
--- Used inside |MiniPairs.map| and |MiniPairs.map_buf| for an actual mapping.
---
---@param pair string: String with two characters representing pair.
---@param neigh_pattern string: Pattern for two neighborhood characters ("\r" line start, "\n" - line end).
function MiniPairs.closeopen(pair, neigh_pattern)
if H.is_disabled() or not (H.get_cursor_neigh(1, 1) == pair:sub(2, 2)) then
return MiniPairs.open(pair, neigh_pattern)
else
return H.get_arrow_key('right')
end
end
--- Process |<BS>|
---
--- Used as |map-expr| mapping for `<BS>`. It removes whole pair (via
--- `<BS><Del>`) if neighborhood is equal to a whole pair recognized for
--- current buffer. Pair is recognized for current buffer if it is registered
--- for global or current buffer mapping. Pair is registered as a result of
--- calling |MiniPairs.map| or |MiniPairs.map_buf|.
---
--- Mapped by default inside |MiniPairs.setup|.
function MiniPairs.bs()
local res = H.keys.bs
local neigh = H.get_cursor_neigh(0, 1)
if not H.is_disabled() and H.is_pair_registered(neigh, vim.fn.mode(), 0, 'bs') then
res = ('%s%s'):format(res, H.keys.del)
end
return res
end
--- Process |i_<CR>|
---
--- Used as |map-expr| mapping for `<CR>` in insert mode. It puts "close"
--- symbol on next line (via `<CR><C-o>O`) if neighborhood is equal to a whole
--- pair recognized for current buffer. Pair is recognized for current buffer
--- if it is registered for global or current buffer mapping. Pair is
--- registered as a result of calling |MiniPairs.map| or |MiniPairs.map_buf|.
---
--- Mapped by default inside |MiniPairs.setup|.
function MiniPairs.cr()
local res = H.keys.cr
local neigh = H.get_cursor_neigh(0, 1)
if not H.is_disabled() and H.is_pair_registered(neigh, vim.fn.mode(), 0, 'cr') then
res = ('%s%s'):format(res, H.keys.above)
end
return res
end
-- Helper data ================================================================
-- Module default config
H.default_config = MiniPairs.config
-- Default value of `pair_info` for mapping functions
H.default_pair_info = { neigh_pattern = '..', register = { bs = true, cr = true } }
-- Pair sets registered *per mode-buffer-key*. Buffer `'all'` contains pairs
-- registered for all buffers.
H.registered_pairs = {
i = { all = { bs = {}, cr = {} } },
c = { all = { bs = {}, cr = {} } },
t = { all = { bs = {}, cr = {} } },
}
-- Precomputed keys to increase speed
-- stylua: ignore start
local function escape(s) return vim.api.nvim_replace_termcodes(s, true, true, true) end
H.keys = {
above = escape('<C-o>O'),
bs = escape('<bs>'),
cr = escape('<cr>'),
del = escape('<del>'),
keep_undo = escape('<C-g>U'),
-- NOTE: use `get_arrow_key()` instead of `H.keys.left` or `H.keys.right`
left = escape('<left>'),
right = escape('<right>')
}
-- stylua: ignore end
-- Helper functionality =======================================================
-- Settings -------------------------------------------------------------------
function H.setup_config(config)
-- General idea: if some table elements are not present in user-supplied
-- `config`, take them from default config
vim.validate({ config = { config, 'table', true } })
config = vim.tbl_deep_extend('force', H.default_config, config or {})
vim.validate({
modes = { config.modes, 'table' },
['modes.insert'] = { config.modes.insert, 'boolean' },
['modes.command'] = { config.modes.command, 'boolean' },
['modes.terminal'] = { config.modes.terminal, 'boolean' },
mappings = { config.mappings, 'table' },
})
return config
end
function H.apply_config(config)
MiniPairs.config = config
-- Setup mappings in supplied modes
local mode_ids = { insert = 'i', command = 'c', terminal = 't' }
-- Compute in which modes mapping should be set up
local mode_array = {}
for name, to_set in pairs(config.modes) do
if to_set then
table.insert(mode_array, mode_ids[name])
end
end
for _, mode in pairs(mode_array) do
for key, pair_info in pairs(config.mappings) do
MiniPairs.map(mode, key, pair_info)
end
vim.api.nvim_set_keymap(mode, '<BS>', [[v:lua.MiniPairs.bs()]], { expr = true, noremap = true })
if mode == 'i' then
vim.api.nvim_set_keymap('i', '<CR>', [[v:lua.MiniPairs.cr()]], { expr = true, noremap = true })
end
end
end
function H.is_disabled()
return vim.g.minipairs_disable == true or vim.b.minipairs_disable == true
end
-- Pair registration ----------------------------------------------------------
function H.register_pair(pair_info, mode, buffer)
-- Process new mode
H.registered_pairs[mode] = H.registered_pairs[mode] or { all = { bs = {}, cr = {} } }
local mode_pairs = H.registered_pairs[mode]
-- Process new buffer
mode_pairs[buffer] = mode_pairs[buffer] or { bs = {}, cr = {} }
-- Register pair if it is not already registered
local register, pair = pair_info.register, pair_info.pair
if register.bs and not vim.tbl_contains(mode_pairs[buffer].bs, pair) then
table.insert(mode_pairs[buffer].bs, pair)
end
if register.cr and not vim.tbl_contains(mode_pairs[buffer].cr, pair) then
table.insert(mode_pairs[buffer].cr, pair)
end
end
function H.unregister_pair(pair, mode, buffer)
local mode_pairs = H.registered_pairs[mode]
if not (mode_pairs and mode_pairs[buffer]) then
return
end
local buf_pairs = mode_pairs[buffer]
for _, key in ipairs({ 'bs', 'cr' }) do
for i, p in ipairs(buf_pairs[key]) do
if p == pair then
table.remove(buf_pairs[key], i)
break
end
end
end
end
function H.is_pair_registered(pair, mode, buffer, key)
local mode_pairs = H.registered_pairs[mode]
if not mode_pairs then
return false
end
if vim.tbl_contains(mode_pairs['all'][key], pair) then
return true
end
buffer = buffer == 0 and vim.api.nvim_get_current_buf() or buffer
local buf_pairs = mode_pairs[buffer]
if not buf_pairs then
return false
end
return vim.tbl_contains(buf_pairs[key], pair)
end
-- Work with pair_info --------------------------------------------------------
function H.ensure_pair_info(pair_info)
vim.validate({ pair_info = { pair_info, 'table' } })
pair_info = vim.tbl_deep_extend('force', H.default_pair_info, pair_info)
vim.validate({
action = { pair_info.action, 'string' },
pair = { pair_info.pair, 'string' },
neigh_pattern = { pair_info.neigh_pattern, 'string' },
register = { pair_info.register, 'table' },
['register.bs'] = { pair_info.register.bs, 'boolean' },
['register.cr'] = { pair_info.register.cr, 'boolean' },
})
return pair_info
end
function H.pair_info_to_map_rhs(pair_info)
return ('v:lua.MiniPairs.%s(%s, %s)'):format(
pair_info.action,
vim.inspect(pair_info.pair),
vim.inspect(pair_info.neigh_pattern)
)
end
-- Utilities ------------------------------------------------------------------
function H.map(mode, key, command)
vim.api.nvim_set_keymap(mode, key, command, { expr = true, noremap = true })
end
function H.get_cursor_neigh(start, finish)
local line, col
if vim.fn.mode() == 'c' then
line = vim.fn.getcmdline()
col = vim.fn.getcmdpos()
-- Adjust start and finish because output of `getcmdpos()` starts counting
-- columns from 1
start = start - 1
finish = finish - 1
else
line = vim.api.nvim_get_current_line()
col = vim.api.nvim_win_get_cursor(0)[2]
end
-- Add '\r' and '\n' to always return 2 characters
return string.sub(('%s%s%s'):format('\r', line, '\n'), col + 1 + start, col + 1 + finish)
end
function H.neigh_match(pattern)
return (pattern == nil) or (H.get_cursor_neigh(0, 1):find(pattern) ~= nil)
end
function H.get_arrow_key(key)
if vim.fn.mode() == 'i' then
-- Using left/right keys in insert mode breaks undo sequence and, more
-- importantly, dot-repeat. To avoid this, use 'i_CTRL-G_U' mapping.
return H.keys.keep_undo .. H.keys[key]
else
return H.keys[key]
end
end
return MiniPairs