-
-
Notifications
You must be signed in to change notification settings - Fork 61
/
config.lua
665 lines (632 loc) · 24.4 KB
/
config.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
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
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
local M = {}
M.default_opts = {
keymaps = {
insert = "<C-g>s",
insert_line = "<C-g>S",
normal = "ys",
normal_cur = "yss",
normal_line = "yS",
normal_cur_line = "ySS",
visual = "S",
visual_line = "gS",
delete = "ds",
change = "cs",
},
surrounds = {
["("] = {
add = { "( ", " )" },
find = function()
return M.get_selection({ motion = "a(" })
end,
delete = "^(. ?)().-( ?.)()$",
},
[")"] = {
add = { "(", ")" },
find = function()
return M.get_selection({ motion = "a)" })
end,
delete = "^(.)().-(.)()$",
},
["{"] = {
add = { "{ ", " }" },
find = function()
return M.get_selection({ motion = "a{" })
end,
delete = "^(. ?)().-( ?.)()$",
},
["}"] = {
add = { "{", "}" },
find = function()
return M.get_selection({ motion = "a}" })
end,
delete = "^(.)().-(.)()$",
},
["<"] = {
add = { "< ", " >" },
find = function()
return M.get_selection({ motion = "a<" })
end,
delete = "^(. ?)().-( ?.)()$",
},
[">"] = {
add = { "<", ">" },
find = function()
return M.get_selection({ motion = "a>" })
end,
delete = "^(.)().-(.)()$",
},
["["] = {
add = { "[ ", " ]" },
find = function()
return M.get_selection({ motion = "a[" })
end,
delete = "^(. ?)().-( ?.)()$",
},
["]"] = {
add = { "[", "]" },
find = function()
return M.get_selection({ motion = "a]" })
end,
delete = "^(.)().-(.)()$",
},
["'"] = {
add = { "'", "'" },
find = function()
return M.get_selection({ motion = "a'" })
end,
delete = "^(.)().-(.)()$",
},
['"'] = {
add = { '"', '"' },
find = function()
return M.get_selection({ motion = 'a"' })
end,
delete = "^(.)().-(.)()$",
},
["`"] = {
add = { "`", "`" },
find = function()
return M.get_selection({ motion = "a`" })
end,
delete = "^(.)().-(.)()$",
},
["i"] = { -- TODO: Add find/delete/change functions
add = function()
local left_delimiter = M.get_input("Enter the left delimiter: ")
local right_delimiter = left_delimiter and M.get_input("Enter the right delimiter: ")
if right_delimiter then
return { { left_delimiter }, { right_delimiter } }
end
end,
find = function() end,
delete = function() end,
},
["t"] = {
add = function()
local input = M.get_input("Enter the HTML tag: ")
if input then
local element = input:match("^<?([^%s>]*)")
local attributes = input:match("^<?[^%s>]*%s+(.-)>?$")
local open = attributes and element .. " " .. attributes or element
local close = element
return { { "<" .. open .. ">" }, { "</" .. close .. ">" } }
end
end,
find = function()
return M.get_selection({ motion = "at" })
end,
delete = "^(%b<>)().-(%b<>)()$",
change = {
target = "^<([^%s<>]*)().-([^/]*)()>$",
replacement = function()
local input = M.get_input("Enter the HTML tag: ")
if input then
local element = input:match("^<?([^%s>]*)")
local attributes = input:match("^<?[^%s>]*%s+(.-)>?$")
local open = attributes and element .. " " .. attributes or element
local close = element
return { { open }, { close } }
end
end,
},
},
["T"] = {
add = function()
local input = M.get_input("Enter the HTML tag: ")
if input then
local element = input:match("^<?([^%s>]*)")
local attributes = input:match("^<?[^%s>]*%s+(.-)>?$")
local open = attributes and element .. " " .. attributes or element
local close = element
return { { "<" .. open .. ">" }, { "</" .. close .. ">" } }
end
end,
find = function()
return M.get_selection({ motion = "at" })
end,
delete = "^(%b<>)().-(%b<>)()$",
change = {
target = "^<([^>]*)().-([^/]*)()>$",
replacement = function()
local input = M.get_input("Enter the HTML tag: ")
if input then
local element = input:match("^<?([^%s>]*)")
local attributes = input:match("^<?[^%s>]*%s+(.-)>?$")
local open = attributes and element .. " " .. attributes or element
local close = element
return { { open }, { close } }
end
end,
},
},
["f"] = {
add = function()
local result = M.get_input("Enter the function name: ")
if result then
return { { result .. "(" }, { ")" } }
end
end,
find = "[^=%s%(%)]+%b()",
delete = "^([^=%s%(%)]+%()().-(%))()$",
change = {
target = "^.-([%w_]+)()%(.-%)()()$",
replacement = function()
local result = M.get_input("Enter the function name: ")
if result then
return { { result }, { "" } }
end
end,
},
},
invalid_key_behavior = {
add = function(char)
return { { char }, { char } }
end,
find = function(char)
return M.get_selection({
pattern = vim.pesc(char) .. ".-" .. vim.pesc(char),
})
end,
delete = function(char)
return M.get_selections({
char = char,
pattern = "^(.)().-(.)()$",
})
end,
change = {
target = function(char)
return M.get_selections({
char = char,
pattern = "^(.)().-(.)()$",
})
end,
},
},
},
aliases = {
["a"] = ">",
["b"] = ")",
["B"] = "}",
["r"] = "]",
["q"] = { '"', "'", "`" },
["s"] = { "}", "]", ")", ">", '"', "'", "`" },
},
highlight = {
duration = 0,
},
move_cursor = "begin",
indent_lines = function(start, stop)
local b = vim.bo
-- Only re-indent the selection if a formatter is set up already
if start <= stop and (b.equalprg ~= "" or b.indentexpr ~= "" or b.cindent or b.smartindent or b.lisp) then
vim.cmd(string.format("silent normal! %dG=%dG", start, stop))
end
end,
}
--[====================================================================================================================[
Configuration Helper Functions
--]====================================================================================================================]
-- Gets input from the user.
---@param prompt string The input prompt.
---@return string? @The user input.
M.get_input = function(prompt)
-- Since `vim.fn.input()` does not handle keyboard interrupts, we use a protected call to detect <C-c>
local ok, result = pcall(vim.fn.input, { prompt = prompt, cancelreturn = vim.NIL })
if ok and result ~= vim.NIL then
return result
end
end
-- Gets a selection from the buffer based on some heuristic.
---@param args { char: string?, pattern: string?, motion: string?, node: string? }
---@return selection? The retrieved selection.
M.get_selection = function(args)
if args.pattern then
return require("nvim-surround.patterns").get_selection(args.pattern)
elseif args.motion then
return require("nvim-surround.textobjects").get_selection(args.motion)
elseif args.node then
return require("nvim-surround.treesitter").get_selection(args.node)
---[=[ DEPRECATION WARNING
---@diagnostic disable-next-line: undefined-field
elseif args.textobject then
local textobject_warning = {
"The `textobject` key has been deprecated in favor of the `motion` key.",
"Please pre-pend text-object keys with 'a'. See :h nvim-surround.config.get_selection() for details",
}
vim.notify_once(table.concat(textobject_warning, "\n"), vim.log.levels.ERROR)
--]=]
else
vim.notify("Invalid key provided for `:h nvim-surround.config.get_selection()`.", vim.log.levels.ERROR)
end
end
-- Gets a pair of selections from the buffer based on some heuristic.
---@param args { char: string?, pattern: string? }
M.get_selections = function(args)
if args.char and args.pattern then
return require("nvim-surround.utils").get_selections(args.char, args.pattern)
end
end
--[====================================================================================================================[
End of Helper Functions
--]====================================================================================================================]
-- Stores the global user-set options for the plugin.
M.user_opts = nil
-- Returns the buffer-local options for the plugin, or global options if buffer-local does not exist.
---@return options @The buffer-local options.
M.get_opts = function()
return vim.b[0].nvim_surround_buffer_opts or M.user_opts or {}
end
-- Returns the add key for the surround associated with a given character, if one exists.
---@param char string? The input character.
---@return string|string[]|fun(string?): string[][]? @The function to get the delimiters to be added.
M.get_add = function(char)
char = require("nvim-surround.utils").get_alias(char)
local key = M.get_opts().surrounds[char] or M.get_opts().surrounds.invalid_key_behavior
return key.add
end
-- Returns the delete key for the surround associated with a given character, if one exists.
---@param char string? The input character.
---@return string|fun(string?): selections? @The function to get the selections to be deleted.
M.get_delete = function(char)
char = require("nvim-surround.utils").get_alias(char)
local key = M.get_opts().surrounds[char] or M.get_opts().surrounds.invalid_key_behavior
return key.delete
end
-- Returns the change key for the surround associated with a given character, if one exists.
---@param char string? The input character.
---@return { target: function, replacement: function? }? @A table holding the target/replacment functions.
M.get_change = function(char)
char = require("nvim-surround.utils").get_alias(char)
local key = M.get_opts().surrounds[char] or M.get_opts().surrounds.invalid_key_behavior
return key.change
end
-- Returns a set of opts, with missing keys filled in by the invalid_key_behavior key.
---@param opts options? The provided options.
---@return options? @The modified options.
M.fill_missing_surrounds = function(opts)
-- If there are no surrounds, then no modification is necessary
if not (opts and opts.surrounds) then
return opts
end
for char, val in pairs(opts.surrounds) do
if val then
-- For each surround, if a key is missing, fill it in using the correspnding key from `invalid_key_behavior`
local add, find, delete, change = val.add, val.find, val.delete, val.change
local invalid = M.get_opts().surrounds.invalid_key_behavior
if not add then
opts.surrounds[char].add = function()
return invalid.add(char)
end
end
if not find then
opts.surrounds[char].find = function()
return invalid.find(char)
end
end
if not delete then
opts.surrounds[char].delete = function()
return invalid.delete(char)
end
end
if not (change and change.target) then
opts.surrounds[char].change = {
target = function()
return invalid.change.target(char)
end,
}
end
end
end
return opts
end
-- Translates the user-provided configuration into the internal form.
---@param opts options? The user-provided options.
---@return options? @The translated options.
M.translate_opts = function(opts)
---[=[ DEPRECATION WARNINGS
---@diagnostic disable-next-line: undefined-field
if opts and opts.highlight_motion then
local highlight_warning = {
"The `highlight_motion` table has been renamed to `highlight`.",
"See :h nvim-surround.config.highlight for details",
}
vim.notify_once(table.concat(highlight_warning, "\n"), vim.log.levels.ERROR)
end
---@diagnostic disable-next-line: undefined-field
if opts and opts.delimiters then
local delimiter_warning = {
"The `delimiters` table has been renamed to `surrounds`.",
"See :h nvim-surround.config.surrounds for details",
}
vim.notify_once(table.concat(delimiter_warning, "\n"), vim.log.levels.ERROR)
end
--]=]
if not (opts and opts.surrounds) then
return opts
end
for char, val in pairs(opts.surrounds) do
---[=[ DEPRECATION WARNINGS
if char == "pairs" or char == "separators" then
local delimiter_warning = {
"The `pairs` and `separators` tables have been deprecated; configuration for surrounds",
"goes in `surrounds`. See :h nvim-surround.config.surrounds for details.",
}
vim.notify_once(table.concat(delimiter_warning, "\n"), vim.log.levels.ERROR)
end
if vim.tbl_islist(val) then
local add_warning = {
"The old configuration for defining surrounds has been deprecated; see",
":h nvim-surround.config.surrounds for details.",
}
vim.notify_once(table.concat(add_warning, "\n"), vim.log.levels.ERROR)
end
--]=]
-- Validate that the delimiter has not been disabled
if val then
local add, find, delete, change = val.add, val.find, val.delete, val.change
-- Handle `add` key translation
if add and vim.tbl_islist(add) then -- Check if the add key is a table instead of a function
-- Wrap the left/right delimiters in a table if they are strings (single line)
if type(add[1]) == "string" then
add[1] = { add[1] }
end
if type(add[2]) == "string" then
add[2] = { add[2] }
end
-- Wrap the delimiter pair in a function
opts.surrounds[char].add = function()
return add
end
end
-- Handle `find` key translation
if find and type(find) == "string" then
-- Treat the string as a Lua pattern, and find the selection
opts.surrounds[char].find = function()
return M.get_selection({ pattern = find })
end
end
-- Handle `delete` key translation
if delete and type(delete) == "string" then
-- Wrap delete in a function
opts.surrounds[char].delete = function()
return M.get_selections({ char = char, pattern = delete })
end
end
-- Handle `change` key translation
if change then
local target, replacement = change.target, change.replacement
-- Wrap target in a function
if target and type(target) == "string" then
opts.surrounds[char].change.target = function()
return M.get_selections({ char = char, pattern = target })
end
end
-- Check if the replacement key is a table instead of a function
if replacement and vim.tbl_islist(replacement) then
-- Wrap the left/right delimiters in a table if they are strings (single line)
if type(replacement[1]) == "string" then
replacement[1] = { replacement[1] }
end
if type(replacement[2]) == "string" then
replacement[2] = { replacement[2] }
end
-- Wrap the delimiter pair in a function
opts.surrounds[char].change.replacement = function()
return replacement
end
end
else
if M.get_opts().surrounds and M.get_opts().surrounds[char] then
opts.surrounds[char].change = M.get_opts().surrounds[char].change
else
opts.surrounds[char].change = {
target = opts.surrounds[char].delete,
}
end
end
end
end
return opts
end
-- Updates the buffer-local options for the plugin based on the input.
---@param base_opts options The base options that will be used for configuration.
---@param new_opts options? The new options to potentially override the base options.
---@return options The merged options.
M.merge_opts = function(base_opts, new_opts)
return new_opts and vim.tbl_deep_extend("force", base_opts, M.translate_opts(new_opts)) or base_opts
end
-- Check if a keymap should be added before setting it.
---@param args table The arguments to set the keymap.
M.set_keymap = function(args)
-- If the keymap is disabled
if not args.lhs then
-- If the mapping is disabled globally, do nothing
if not M.user_opts.keymaps[args.name] then
return
end
-- Otherwise disable the global keymap
args.lhs = M.user_opts.keymaps[args.name]
args.rhs = "<NOP>"
end
vim.keymap.set(args.mode, args.lhs, args.rhs, args.opts)
end
-- Set up user-configured keymaps, globally or for the buffer.
---@param buffer boolean Whether the keymaps should be set for the buffer or not.
M.set_keymaps = function(buffer)
M.set_keymap({
name = "insert",
mode = "i",
lhs = M.get_opts().keymaps.insert,
rhs = require("nvim-surround").insert_surround,
opts = {
buffer = buffer,
desc = "Add a surrounding pair around the cursor (insert mode).",
silent = true,
},
})
M.set_keymap({
name = "insert_line",
mode = "i",
lhs = M.get_opts().keymaps.insert_line,
rhs = function()
require("nvim-surround").insert_surround(true)
end,
opts = {
buffer = buffer,
desc = "Add a surrounding pair around the cursor, on new lines (insert mode).",
silent = true,
},
})
M.set_keymap({
name = "normal",
mode = "n",
lhs = M.get_opts().keymaps.normal,
rhs = require("nvim-surround").normal_surround,
opts = {
buffer = buffer,
desc = "Add a surrounding pair around a motion (normal mode).",
expr = true,
silent = true,
},
})
M.set_keymap({
name = "normal_cur",
mode = "n",
lhs = M.get_opts().keymaps.normal_cur,
rhs = function()
return "^" .. tostring(vim.v.count1) .. M.get_opts().keymaps.normal .. "g_"
end,
opts = {
buffer = buffer,
desc = "Add a surrounding pair around the current line (normal mode).",
expr = true,
remap = true,
silent = true,
},
})
M.set_keymap({
name = "normal_line",
mode = "n",
lhs = M.get_opts().keymaps.normal_line,
rhs = function()
return require("nvim-surround").normal_surround(nil, true)
end,
opts = {
buffer = buffer,
desc = "Add a surrounding pair around a motion, on new lines (normal mode).",
expr = true,
silent = true,
},
})
M.set_keymap({
name = "normal_cur_line",
mode = "n",
lhs = M.get_opts().keymaps.normal_cur_line,
rhs = function()
return "^" .. tostring(vim.v.count1) .. M.get_opts().keymaps.normal_line .. "g_"
end,
opts = {
buffer = buffer,
desc = "Add a surrounding pair around the current line, on new lines (normal mode).",
expr = true,
remap = true,
silent = true,
},
})
M.set_keymap({
name = "visual",
mode = "x",
lhs = M.get_opts().keymaps.visual,
rhs = "<Esc><Cmd>lua require'nvim-surround'.visual_surround()<CR>",
opts = {
buffer = buffer,
desc = "Add a surrounding pair around a visual selection.",
silent = true,
},
})
M.set_keymap({
name = "visual_line",
mode = "x",
lhs = M.get_opts().keymaps.visual_line,
rhs = "<Esc><Cmd>lua require'nvim-surround'.visual_surround(true)<CR>",
opts = {
buffer = buffer,
desc = "Add a surrounding pair around a visual selection, on new lines.",
silent = true,
},
})
M.set_keymap({
name = "delete",
mode = "n",
lhs = M.get_opts().keymaps.delete,
rhs = require("nvim-surround").delete_surround,
opts = {
buffer = buffer,
desc = "Delete a surrounding pair.",
expr = true,
silent = true,
},
})
M.set_keymap({
name = "change",
mode = "n",
lhs = M.get_opts().keymaps.change,
rhs = require("nvim-surround").change_surround,
opts = {
buffer = buffer,
desc = "Change a surrounding pair.",
expr = true,
silent = true,
},
})
end
-- Setup the global user options for all files.
---@param user_opts options? The user-defined options to be merged with default_opts.
M.setup = function(user_opts)
-- Overwrite default options with user-defined options, if they exist
---@diagnostic disable-next-line
M.user_opts = M.merge_opts(M.translate_opts(M.default_opts), user_opts)
-- Filling in missing keys must occur after, since the user might have defined their own `invalid_key_behavior`
M.user_opts = M.fill_missing_surrounds(M.user_opts)
-- Configure global keymaps
M.set_keymaps(false)
-- TODO: Configure filetype settings
-- require("nvim-surround.filetype").setup()
-- Configure highlight group, if necessary
if M.user_opts.highlight.duration then
vim.cmd([[
highlight default link NvimSurroundHighlight Visual
]])
end
end
-- Setup the user options for the current buffer.
---@param buffer_opts table? The buffer-local options to be merged with the global user_opts.
M.buffer_setup = function(buffer_opts)
-- Merge the given table into the existing buffer-local options, or global options otherwise
vim.b[0].nvim_surround_buffer_opts = M.merge_opts(M.get_opts(), buffer_opts)
-- Filling in missing keys must occur after, since the user might have defined their own `invalid_key_behavior`
vim.b[0].nvim_surround_buffer_opts = M.fill_missing_surrounds(vim.b[0].nvim_surround_buffer_opts)
-- Configure buffer-local keymaps
M.set_keymaps(true)
end
return M