Skip to content

Commit 10c2d96

Browse files
committed
feat: Support multi-byte mappings for aliases.
1 parent dca2e99 commit 10c2d96

File tree

2 files changed

+45
-14
lines changed

2 files changed

+45
-14
lines changed

lua/nvim-surround/config.lua

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -540,36 +540,51 @@ M.translate_invalid_key_behavior = function(invalid_surround)
540540
return invalid
541541
end
542542

543+
-- Translates `alias` into the internal form.
544+
---@param user_alias false|string|string[] The user-provided `alias`.
545+
---@return string|string[]|nil @The translated `alias`.
546+
M.translate_alias = function(user_alias)
547+
if not user_alias then
548+
return nil
549+
end
550+
return user_alias
551+
end
552+
543553
-- Translates the user-provided configuration into the internal form.
544554
---@param user_opts user_options The user-provided options.
545555
---@return options @The translated options.
546556
M.translate_opts = function(user_opts)
547557
local input = require("nvim-surround.input")
548558
local opts = {}
549559
for key, value in pairs(user_opts) do
550-
if key == "surrounds" then
560+
if key == "surrounds" or key == "aliases" then
551561
elseif key == "indent_lines" then
552562
opts[key] = value or function() end
553563
else
554564
opts[key] = value
555565
end
556566
end
557-
if not user_opts.surrounds then
558-
return opts
559-
end
560-
561-
opts.surrounds = {}
562-
for char, user_surround in pairs(user_opts.surrounds) do
563-
char = input.replace_termcodes(char)
564-
-- Special case translation for `invalid_key_behavior`
565-
if type(user_surround) ~= "nil" then
566-
if char == "invalid_key_behavior" then
567-
opts.surrounds[char] = M.translate_invalid_key_behavior(user_surround)
568-
else
569-
opts.surrounds[char] = M.translate_surround(char, user_surround)
567+
if user_opts.surrounds then
568+
opts.surrounds = {}
569+
for char, user_surround in pairs(user_opts.surrounds) do
570+
char = input.replace_termcodes(char)
571+
-- Special case translation for `invalid_key_behavior`
572+
if type(user_surround) ~= "nil" then
573+
if char == "invalid_key_behavior" then
574+
opts.surrounds[char] = M.translate_invalid_key_behavior(user_surround)
575+
else
576+
opts.surrounds[char] = M.translate_surround(char, user_surround)
577+
end
570578
end
571579
end
572580
end
581+
if user_opts.aliases then
582+
opts.aliases = {}
583+
for char, user_alias in pairs(user_opts.aliases) do
584+
char = input.replace_termcodes(char)
585+
opts.aliases[char] = M.translate_alias(user_alias)
586+
end
587+
end
573588
return opts
574589
end
575590

tests/configuration_spec.lua

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ describe("configuration", function()
7171
delete = "^(„)().-(“)()$",
7272
},
7373
},
74+
aliases = {
75+
[""] = ")",
76+
},
7477
})
7578

7679
set_lines({ "hey! hello world" })
@@ -79,6 +82,11 @@ describe("configuration", function()
7982
check_lines({ "hey! „hello“ world" })
8083
vim.cmd("normal ds“")
8184
check_lines({ "hey! hello world" })
85+
86+
vim.cmd("normal yss•")
87+
check_lines({ "(hey! hello world)" })
88+
vim.cmd("normal ds•")
89+
check_lines({ "hey! hello world" })
8290
end)
8391

8492
it("can define and use 'interpreted' multi-byte mappings", function()
@@ -91,6 +99,9 @@ describe("configuration", function()
9199
delete = "^(%[%[)().-(%]%])()$",
92100
},
93101
},
102+
aliases = {
103+
["<CR>"] = ")",
104+
},
94105
})
95106
local meta_close_bracket = vim.api.nvim_replace_termcodes("<M-]>", true, false, true)
96107
set_lines({ "hey! hello world" })
@@ -99,6 +110,11 @@ describe("configuration", function()
99110
check_lines({ "hey! [[hello]] world" })
100111
vim.cmd("normal ds" .. meta_close_bracket)
101112
check_lines({ "hey! hello world" })
113+
114+
vim.cmd("normal yss" .. cr)
115+
check_lines({ "(hey! hello world)" })
116+
vim.cmd("normal ds" .. cr)
117+
check_lines({ "hey! hello world" })
102118
end)
103119

104120
it("default deletes using invalid_key_behavior for an 'interpreted' multi-byte mapping", function()

0 commit comments

Comments
 (0)