Skip to content

Commit 6d2be37

Browse files
authored
fix: Default delete should work if find key is sane. (#331)
If the user has redefined `find = "‘.-’"`, then it makes more sense for default `delete` to honor that and trim quotes, rather than fail and do nothing.
1 parent de828d6 commit 6d2be37

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

lua/nvim-surround/config.lua

+4-1
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,9 @@ M.default_opts = {
206206
},
207207
},
208208
invalid_key_behavior = {
209+
-- By default, we ignore control characters for adding/finding because they are more likely typos than
210+
-- intentional. We choose NOT to for deletion, as users could have redefined the find key to something like
211+
-- ‘.-’. In this case we should still trim a character from each side, instead of early returning nil.
209212
add = function(char)
210213
if not char or char:find("%c") then
211214
return nil
@@ -221,7 +224,7 @@ M.default_opts = {
221224
})
222225
end,
223226
delete = function(char)
224-
if not char or char:find("%c") then
227+
if not char then
225228
return nil
226229
end
227230
return M.get_selections({

tests/configuration_spec.lua

+19
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,25 @@ describe("configuration", function()
9292
check_lines({ "hey! hello world" })
9393
end)
9494

95+
it("default deletes using invalid_key_behavior for an 'interpreted' multi-byte mapping", function()
96+
require("nvim-surround").setup({
97+
surrounds = {
98+
-- interpreted multi-byte
99+
["<C-q>"] = {
100+
add = { "", "" },
101+
find = "‘.-’",
102+
},
103+
},
104+
})
105+
local ctrl_q = vim.api.nvim_replace_termcodes("<C-q>", true, false, true)
106+
set_lines({ "hey! hello world" })
107+
set_curpos({ 1, 7 })
108+
vim.cmd("normal ysiw" .. ctrl_q)
109+
check_lines({ "hey! ‘hello’ world" })
110+
vim.cmd("normal ds" .. ctrl_q)
111+
check_lines({ "hey! hello world" })
112+
end)
113+
95114
it("can disable surrounds", function()
96115
require("nvim-surround").setup({
97116
surrounds = {

0 commit comments

Comments
 (0)