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
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
|
local input = require("nvim-surround.input")
local functional = require("nvim-surround.functional")
local M = {}
---@type user_options
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",
change_line = "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 user_input = M.get_input("Enter the HTML tag: ")
if user_input then
local element = user_input:match("^<?([^%s>]*)")
local attributes = user_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 user_input = M.get_input("Enter the HTML tag: ")
if user_input then
local element = user_input:match("^<?([^%s>]*)")
local attributes = user_input:match("^<?[^%s>]*%s+(.-)>?$")
local open = attributes and element .. " " .. attributes or element
local close = element
return { { open }, { close } }
end
end,
},
},
["T"] = {
add = function()
local user_input = M.get_input("Enter the HTML tag: ")
if user_input then
local element = user_input:match("^<?([^%s>]*)")
local attributes = user_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 user_input = M.get_input("Enter the HTML tag: ")
if user_input then
local element = user_input:match("^<?([^%s>]*)")
local attributes = user_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 = function()
if vim.g.loaded_nvim_treesitter then
local selection = M.get_selection({
query = {
capture = "@call.outer",
type = "textobjects",
},
})
if selection then
return selection
end
end
return M.get_selection({ pattern = "[^=%s%(%){}]+%b()" })
end,
delete = "^(.-%()().-(%))()$",
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)
if not char or char:find("%c") then
return nil
end
return { { char }, { char } }
end,
find = function(char)
if not char or char:find("%c") then
return nil
end
return M.get_selection({
pattern = vim.pesc(char) .. ".-" .. vim.pesc(char),
})
end,
delete = function(char)
if not char or char:find("%c") then
return nil
end
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))
require("nvim-surround.cache").set_callback("")
end
end,
}
--[====================================================================================================================[
Configuration Helper Functions
--]====================================================================================================================]
-- Gets input from the user.
---@param prompt string The input prompt.
---@return string|nil @The user input.
---@nodiscard
M.get_input = function(prompt)
return input.get_input(prompt)
end
-- Gets a selection from the buffer based on some heuristic.
---@param args { char: string|nil, motion: string|nil, pattern: string|nil, node: string|nil, query: { capture: string, type: string }|nil }
---@return selection|nil @The retrieved selection.
---@nodiscard
M.get_selection = function(args)
if args.char then
return M.get_find(args.char)(args.char)
elseif args.motion then
return require("nvim-surround.motions").get_selection(args.motion)
elseif args.node then
return require("nvim-surround.treesitter").get_selection(args.node)
elseif args.pattern then
return require("nvim-surround.patterns").get_selection(args.pattern)
elseif args.query then
return require("nvim-surround.queries").get_selection(args.query.capture, args.query.type)
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|nil, exclude: function|nil }
---@nodiscard
M.get_selections = function(args)
local selection = M.get_selection({ char = args.char })
if not selection then
return nil
end
if args.pattern then
return require("nvim-surround.patterns").get_selections(selection, args.pattern)
elseif args.exclude then
local outer_selection = M.get_opts().surrounds[args.char].find()
if not outer_selection then
return nil
end
vim.fn.cursor(outer_selection.first_pos)
local inner_selection = args.exclude()
if not inner_selection then
return nil
end
-- Properly exclude the inner selection from the outer selection
local selections = {
left = {
first_pos = outer_selection.first_pos,
last_pos = { inner_selection.first_pos[1], inner_selection.first_pos[2] - 1 },
},
right = {
first_pos = { inner_selection.last_pos[1], inner_selection.last_pos[2] + 1 },
last_pos = outer_selection.last_pos,
},
}
return selections
else
vim.notify("Invalid key provided for `:h nvim-surround.config.get_selections()`.", vim.log.levels.ERROR)
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.
---@nodiscard
M.get_opts = function()
return vim.b[0].nvim_surround_buffer_opts or M.user_opts or {}
end
-- Returns the value that the input is aliased to, or the character if no alias exists.
---@param char string|nil The input character.
---@return string|nil @The aliased character if it exists, or the original if none exists.
---@nodiscard
M.get_alias = function(char)
local aliases = M.get_opts().aliases
if type(aliases[char]) == "string" then
return aliases[char]
end
return char
end
-- Gets a delimiter pair for a user-inputted character.
---@param char string|nil The user-given character.
---@param line_mode boolean Whether or not the delimiters should be put on new lines.
---@return delimiter_pair|nil @A pair of delimiters for the given input, or nil if not applicable.
---@nodiscard
M.get_delimiters = function(char, line_mode)
char = M.get_alias(char)
-- Get the delimiters, using invalid_key_behavior if the add function is undefined for the character
local delimiters = M.get_add(char)(char)
-- Add new lines if the addition is done line-wise
if delimiters and line_mode then
local lhs = delimiters[1]
local rhs = delimiters[2]
-- Trim whitespace after the leading delimiter and before the trailing delimiter
lhs[#lhs] = lhs[#lhs]:gsub("%s+$", "")
rhs[1] = rhs[1]:gsub("^%s+", "")
table.insert(rhs, 1, "")
table.insert(lhs, "")
end
return delimiters
end
-- Returns the add key for the surround associated with a given character, if one exists.
---@param char string|nil The input character.
---@return add_func @The function to get the delimiters to be added.
---@nodiscard
M.get_add = function(char)
char = M.get_alias(char)
if M.get_opts().surrounds[char] and M.get_opts().surrounds[char].add then
return M.get_opts().surrounds[char].add
end
return M.get_opts().surrounds.invalid_key_behavior.add
end
-- Returns the find key for the surround associated with a given character, if one exists.
---@param char string|nil The input character.
---@return find_func @The function to get the selection.
---@nodiscard
M.get_find = function(char)
char = M.get_alias(char)
if M.get_opts().surrounds[char] and M.get_opts().surrounds[char].find then
return M.get_opts().surrounds[char].find
end
return M.get_opts().surrounds.invalid_key_behavior.find
end
-- Returns the delete key for the surround associated with a given character, if one exists.
---@param char string|nil The input character.
---@return delete_func @The function to get the selections to be deleted.
---@nodiscard
M.get_delete = function(char)
char = M.get_alias(char)
if M.get_opts().surrounds[char] and M.get_opts().surrounds[char].delete then
return M.get_opts().surrounds[char].delete
end
return M.get_opts().surrounds.invalid_key_behavior.delete
end
-- Returns the change key for the surround associated with a given character, if one exists.
---@param char string|nil The input character.
---@return change_table @A table holding the target/replacement functions.
---@nodiscard
M.get_change = function(char)
char = M.get_alias(char)
if M.get_opts().surrounds[char] then
if M.get_opts().surrounds[char].change then
return M.get_opts().surrounds[char].change
else
return {
target = M.get_delete(char),
}
end
end
return M.get_change("invalid_key_behavior")
end
-- Translates the user-provided surround.add into the internal form.
---@param user_add user_add The user-provided add key.
---@return false|add_func|nil @The translated add key.
M.translate_add = function(user_add)
if type(user_add) ~= "table" then
return user_add
end
-- If the input is given as a pair of strings, or pair of string lists, wrap it in a function
return function()
return {
functional.to_list(user_add[1]),
functional.to_list(user_add[2]),
}
end
end
-- Translates the user-provided surround.find into the internal form.
---@param user_find user_find The user-provided find key.
---@return false|find_func|nil @The translated find key.
M.translate_find = function(user_find)
if type(user_find) ~= "string" then
return user_find
end
-- Treat the string as a Lua pattern, and find the selection
return function()
return M.get_selection({ pattern = user_find })
end
end
-- Translates the user-provided surround.delete into the internal form.
---@param char string The character used to activate the surround.
---@param user_delete user_delete The user-provided delete key.
---@return false|delete_func|nil @The translated delete key.
M.translate_delete = function(char, user_delete)
if type(user_delete) ~= "string" then
return user_delete
end
-- Treat the string as a Lua pattern, and find the selection
return function()
return M.get_selections({ char = char, pattern = user_delete })
end
end
-- Translates the user-provided surround.change into the internal form.
---@param char string The character used to activate the surround.
---@param user_change user_change|nil The user-provided change key.
---@return false|change_table|nil @The translated change key.
M.translate_change = function(char, user_change)
if type(user_change) ~= "table" then
return user_change
end
return {
target = M.translate_delete(char, user_change.target),
replacement = M.translate_add(user_change.replacement),
}
end
-- Translates the user-provided surround into the internal form.
---@param char string The character used to activate the surround.
---@param user_surround false|user_surround The user-provided surround.
---@return false|surround @The translated surround.
M.translate_surround = function(char, user_surround)
if not user_surround then
return false
end
return {
add = M.translate_add(user_surround.add),
find = M.translate_find(user_surround.find),
delete = M.translate_delete(char, user_surround.delete),
change = M.translate_change(char, user_surround.change),
}
end
-- Translates `invalid_key_behavior` into the internal form.
---@param invalid_surround false|user_surround The user-provided `invalid_key_behavior`.
---@return surround @The translated `invalid_key_behavior`.
M.translate_invalid_key_behavior = function(invalid_surround)
local noop_surround = {
add = function() end,
find = function() end,
delete = function() end,
change = {
target = function() end,
},
}
local invalid = M.translate_surround("invalid_key_behavior", invalid_surround)
if invalid == false then
return noop_surround
end
if invalid.add == false then
invalid.add = noop_surround.add
end
if invalid.find == false then
invalid.find = noop_surround.find
end
if invalid.delete == false then
invalid.delete = noop_surround.delete
end
if invalid.change == false then
invalid.change = noop_surround.change
elseif invalid.change ~= nil then
if invalid.change.target == false then
invalid.change.target = noop_surround.change.target
end
end
return invalid
end
-- Translates the user-provided configuration into the internal form.
---@param user_opts user_options The user-provided options.
---@return options @The translated options.
M.translate_opts = function(user_opts)
local opts = {}
for key, value in pairs(user_opts) do
if key == "surrounds" then
elseif key == "indent_lines" then
opts[key] = value or function() end
else
opts[key] = value
end
end
if not user_opts.surrounds then
return opts
end
opts.surrounds = {}
for char, user_surround in pairs(user_opts.surrounds) do
-- Support Vim's notation for special characters
char = vim.api.nvim_replace_termcodes(char, true, true, true)
-- Special case translation for `invalid_key_behavior`
if type(user_surround) ~= "nil" then
if char == "invalid_key_behavior" then
opts.surrounds[char] = M.translate_invalid_key_behavior(user_surround)
else
opts.surrounds[char] = M.translate_surround(char, user_surround)
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 user_options|nil The new options to potentially override the base options.
---@return options The merged options.
M.merge_opts = function(base_opts, new_opts)
new_opts = new_opts or {}
local opts = vim.tbl_deep_extend("force", base_opts, M.translate_opts(new_opts))
return 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 args { buffer: boolean } Whether the keymaps should be set for the buffer or not.
M.set_keymaps = function(args)
-- Set up <Plug> keymaps
M.set_keymap({
mode = "i",
lhs = "<Plug>(nvim-surround-insert)",
rhs = function()
require("nvim-surround").insert_surround({ line_mode = false })
end,
opts = {
buffer = args.buffer,
desc = "Add a surrounding pair around the cursor (insert mode)",
silent = true,
},
})
M.set_keymap({
mode = "i",
lhs = "<Plug>(nvim-surround-insert-line)",
rhs = function()
require("nvim-surround").insert_surround({ line_mode = true })
end,
opts = {
buffer = args.buffer,
desc = "Add a surrounding pair around the cursor, on new lines (insert mode)",
silent = true,
},
})
M.set_keymap({
mode = "n",
lhs = "<Plug>(nvim-surround-normal)",
rhs = function()
return require("nvim-surround").normal_surround({ line_mode = false })
end,
opts = {
buffer = args.buffer,
desc = "Add a surrounding pair around a motion (normal mode)",
expr = true,
silent = true,
},
})
M.set_keymap({
mode = "n",
lhs = "<Plug>(nvim-surround-normal-cur)",
rhs = function()
return "<Plug>(nvim-surround-normal)Vg_"
end,
opts = {
buffer = args.buffer,
desc = "Add a surrounding pair around the current line (normal mode)",
expr = true,
silent = true,
},
})
M.set_keymap({
mode = "n",
lhs = "<Plug>(nvim-surround-normal-line)",
rhs = function()
return require("nvim-surround").normal_surround({ line_mode = true })
end,
opts = {
buffer = args.buffer,
desc = "Add a surrounding pair around a motion, on new lines (normal mode)",
expr = true,
silent = true,
},
})
M.set_keymap({
mode = "n",
lhs = "<Plug>(nvim-surround-normal-cur-line)",
rhs = function()
return "^" .. tostring(vim.v.count1) .. "<Plug>(nvim-surround-normal-line)g_"
end,
opts = {
buffer = args.buffer,
desc = "Add a surrounding pair around the current line, on new lines (normal mode)",
expr = true,
silent = true,
},
})
M.set_keymap({
mode = "x",
lhs = "<Plug>(nvim-surround-visual)",
rhs = function()
local curpos = require("nvim-surround.buffer").get_curpos()
return string.format(
":lua require'nvim-surround'.visual_surround({ line_mode = false, curpos = { %d, %d } })<CR>",
curpos[1],
curpos[2]
)
end,
opts = {
buffer = args.buffer,
desc = "Add a surrounding pair around a visual selection",
silent = true,
expr = true,
},
})
M.set_keymap({
mode = "x",
lhs = "<Plug>(nvim-surround-visual-line)",
rhs = function()
local curpos = require("nvim-surround.buffer").get_curpos()
return string.format(
":lua require'nvim-surround'.visual_surround({ line_mode = true, curpos = { %d, %d } })<CR>",
curpos[1],
curpos[2]
)
end,
opts = {
buffer = args.buffer,
desc = "Add a surrounding pair around a visual selection, on new lines",
silent = true,
expr = true,
},
})
M.set_keymap({
mode = "n",
lhs = "<Plug>(nvim-surround-delete)",
rhs = require("nvim-surround").delete_surround,
opts = {
buffer = args.buffer,
desc = "Delete a surrounding pair",
expr = true,
silent = true,
},
})
M.set_keymap({
mode = "n",
lhs = "<Plug>(nvim-surround-change)",
rhs = function()
return require("nvim-surround").change_surround({ line_mode = false })
end,
opts = {
buffer = args.buffer,
desc = "Change a surrounding pair",
expr = true,
silent = true,
},
})
M.set_keymap({
mode = "n",
lhs = "<Plug>(nvim-surround-change-line)",
rhs = function()
return require("nvim-surround").change_surround({ line_mode = true })
end,
opts = {
buffer = args.buffer,
desc = "Change a surrounding pair, putting replacements on new lines",
expr = true,
silent = true,
},
})
-- Set up user-defined keymaps
M.set_keymap({
name = "insert",
mode = "i",
lhs = M.get_opts().keymaps.insert,
rhs = "<Plug>(nvim-surround-insert)",
opts = {
desc = "Add a surrounding pair around the cursor (insert mode)",
},
})
M.set_keymap({
name = "insert_line",
mode = "i",
lhs = M.get_opts().keymaps.insert_line,
rhs = "<Plug>(nvim-surround-insert-line)",
opts = {
desc = "Add a surrounding pair around the cursor, on new lines (insert mode)",
},
})
M.set_keymap({
name = "normal",
mode = "n",
lhs = M.get_opts().keymaps.normal,
rhs = "<Plug>(nvim-surround-normal)",
opts = {
desc = "Add a surrounding pair around a motion (normal mode)",
},
})
M.set_keymap({
name = "normal_cur",
mode = "n",
lhs = M.get_opts().keymaps.normal_cur,
rhs = "<Plug>(nvim-surround-normal-cur)",
opts = {
desc = "Add a surrounding pair around the current line (normal mode)",
},
})
M.set_keymap({
name = "normal_line",
mode = "n",
lhs = M.get_opts().keymaps.normal_line,
rhs = "<Plug>(nvim-surround-normal-line)",
opts = {
desc = "Add a surrounding pair around a motion, on new lines (normal mode)",
},
})
M.set_keymap({
name = "normal_cur_line",
mode = "n",
lhs = M.get_opts().keymaps.normal_cur_line,
rhs = "<Plug>(nvim-surround-normal-cur-line)",
opts = {
desc = "Add a surrounding pair around the current line, on new lines (normal mode)",
},
})
M.set_keymap({
name = "visual",
mode = "x",
lhs = M.get_opts().keymaps.visual,
rhs = "<Plug>(nvim-surround-visual)",
opts = {
desc = "Add a surrounding pair around a visual selection",
},
})
M.set_keymap({
name = "visual_line",
mode = "x",
lhs = M.get_opts().keymaps.visual_line,
rhs = "<Plug>(nvim-surround-visual-line)",
opts = {
desc = "Add a surrounding pair around a visual selection, on new lines",
},
})
M.set_keymap({
name = "delete",
mode = "n",
lhs = M.get_opts().keymaps.delete,
rhs = "<Plug>(nvim-surround-delete)",
opts = {
desc = "Delete a surrounding pair",
},
})
M.set_keymap({
name = "change",
mode = "n",
lhs = M.get_opts().keymaps.change,
rhs = "<Plug>(nvim-surround-change)",
opts = {
desc = "Change a surrounding pair",
},
})
M.set_keymap({
name = "change_line",
mode = "n",
lhs = M.get_opts().keymaps.change_line,
rhs = "<Plug>(nvim-surround-change-line)",
opts = {
desc = "Change a surrounding pair, putting replacements on new lines",
},
})
end
-- Setup the global user options for all files.
---@param user_opts user_options|nil 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
M.user_opts = M.merge_opts(M.translate_opts(M.default_opts), user_opts)
M.set_keymaps({ buffer = false })
-- Configure highlight group, if necessary
if M.user_opts.highlight.duration then
vim.cmd.highlight("default link NvimSurroundHighlight Visual")
end
-- Intercept dot repeat action, remembering cursor position
local buffer = require("nvim-surround.buffer")
local nvim_surround = require("nvim-surround")
vim.on_key(function(key)
if key == "." and not nvim_surround.pending_surround then
nvim_surround.normal_curpos = buffer.get_curpos()
end
end)
end
-- Setup the user options for the current buffer.
---@param buffer_opts user_options|nil 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)
M.set_keymaps({ buffer = true })
end
return M
|