Skip to content

Commit 8b47db6

Browse files
authored
feat: add labels to surrounds (#449)
Add labels to surrounds. This will be useful to plugins that show key hints. For discussion, see #447.
1 parent 2e93e15 commit 8b47db6

3 files changed

Lines changed: 22 additions & 0 deletions

File tree

doc/nvim-surround.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,7 @@ surrounds, for example setting up a `$` surround, but only in bash files:
401401
add = { "${", "}" },
402402
find = "$%b{}",
403403
delete = "^(..)().-(.)()$",
404+
label = "${…}",
404405
},
405406
},
406407
})
@@ -540,6 +541,9 @@ containing the following keys:
540541
are directly used as the replacement pair. For example, when changing HTML
541542
tag types, only `cst` is needed, instead of `cstt`.
542543

544+
*nvim-surround.setup.surrounds.label*
545+
label: ~
546+
An optional string that represents the label for the surround.
543547

544548
*nvim-surround.setup.surrounds.invalid_key_behavior*
545549
`invalid_key_behavior` is a special key in the `surrounds` table that defines

lua/nvim-surround/annotations.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
---@field find find_func
3030
---@field delete delete_func
3131
---@field change change_table
32+
---@field label? string
3233

3334
---@class options
3435
---@field surrounds table<string, surround>
@@ -51,6 +52,7 @@
5152
---@field find? user_find
5253
---@field delete? user_delete
5354
---@field change? user_change
55+
---@field label? string
5456

5557
---@class user_options
5658
---@field surrounds? table<string, false|user_surround>

lua/nvim-surround/config.lua

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,76 +9,87 @@ M.default_opts = {
99
return M.get_selection({ motion = "a(" })
1010
end,
1111
delete = "^(. ?)().-( ?.)()$",
12+
label = "( ... )",
1213
},
1314
[")"] = {
1415
add = { "(", ")" },
1516
find = function()
1617
return M.get_selection({ motion = "a)" })
1718
end,
1819
delete = "^(.)().-(.)()$",
20+
label = "(...)",
1921
},
2022
["{"] = {
2123
add = { "{ ", " }" },
2224
find = function()
2325
return M.get_selection({ motion = "a{" })
2426
end,
2527
delete = "^(. ?)().-( ?.)()$",
28+
label = "{ ... }",
2629
},
2730
["}"] = {
2831
add = { "{", "}" },
2932
find = function()
3033
return M.get_selection({ motion = "a}" })
3134
end,
3235
delete = "^(.)().-(.)()$",
36+
label = "{...}",
3337
},
3438
["<"] = {
3539
add = { "< ", " >" },
3640
find = function()
3741
return M.get_selection({ motion = "a<" })
3842
end,
3943
delete = "^(. ?)().-( ?.)()$",
44+
label = "< ... >",
4045
},
4146
[">"] = {
4247
add = { "<", ">" },
4348
find = function()
4449
return M.get_selection({ motion = "a>" })
4550
end,
4651
delete = "^(.)().-(.)()$",
52+
label = "<...>",
4753
},
4854
["["] = {
4955
add = { "[ ", " ]" },
5056
find = function()
5157
return M.get_selection({ motion = "a[" })
5258
end,
5359
delete = "^(. ?)().-( ?.)()$",
60+
label = "[ ... ]",
5461
},
5562
["]"] = {
5663
add = { "[", "]" },
5764
find = function()
5865
return M.get_selection({ motion = "a]" })
5966
end,
6067
delete = "^(.)().-(.)()$",
68+
label = "[...]",
6169
},
6270
["'"] = {
6371
add = { "'", "'" },
6472
find = function()
6573
return M.get_selection({ motion = "a'" })
6674
end,
6775
delete = "^(.)().-(.)()$",
76+
label = "'...'",
6877
},
6978
['"'] = {
7079
add = { '"', '"' },
7180
find = function()
7281
return M.get_selection({ motion = 'a"' })
7382
end,
7483
delete = "^(.)().-(.)()$",
84+
label = '"..."',
7585
},
7686
["`"] = {
7787
add = { "`", "`" },
7888
find = function()
7989
return M.get_selection({ motion = "a`" })
8090
end,
8191
delete = "^(.)().-(.)()$",
92+
label = "`...`",
8293
},
8394
["i"] = { -- TODO: Add find/delete/change functions
8495
add = function()
@@ -90,6 +101,7 @@ M.default_opts = {
90101
end,
91102
find = function() end,
92103
delete = function() end,
104+
label = "?...?",
93105
},
94106
["t"] = {
95107
add = function()
@@ -123,6 +135,7 @@ M.default_opts = {
123135
end
124136
end,
125137
},
138+
label = "<tag>...</tag>",
126139
},
127140
["T"] = {
128141
add = function()
@@ -156,6 +169,7 @@ M.default_opts = {
156169
end
157170
end,
158171
},
172+
label = "<tag>...</tag>",
159173
},
160174
["f"] = {
161175
add = function()
@@ -188,6 +202,7 @@ M.default_opts = {
188202
end
189203
end,
190204
},
205+
label = "function(...)",
191206
},
192207
invalid_key_behavior = {
193208
-- By default, we ignore control characters for adding/finding because they are more likely typos than
@@ -485,6 +500,7 @@ M.translate_surround = function(char, user_surround)
485500
find = M.translate_find(user_surround.find),
486501
delete = M.translate_delete(char, user_surround.delete),
487502
change = M.translate_change(char, user_surround.change),
503+
label = user_surround.label,
488504
}
489505
end
490506

0 commit comments

Comments
 (0)