Skip to content

Commit

Permalink
feat: Support multi-byte mappings for aliases.
Browse files Browse the repository at this point in the history
  • Loading branch information
kylechui committed Nov 28, 2024
1 parent dca2e99 commit 10c2d96
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 14 deletions.
43 changes: 29 additions & 14 deletions lua/nvim-surround/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -540,36 +540,51 @@ M.translate_invalid_key_behavior = function(invalid_surround)
return invalid
end

-- Translates `alias` into the internal form.
---@param user_alias false|string|string[] The user-provided `alias`.
---@return string|string[]|nil @The translated `alias`.
M.translate_alias = function(user_alias)
if not user_alias then
return nil
end
return user_alias
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 input = require("nvim-surround.input")
local opts = {}
for key, value in pairs(user_opts) do
if key == "surrounds" then
if key == "surrounds" or key == "aliases" 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
char = input.replace_termcodes(char)
-- 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)
if user_opts.surrounds then
opts.surrounds = {}
for char, user_surround in pairs(user_opts.surrounds) do
char = input.replace_termcodes(char)
-- 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
end
if user_opts.aliases then
opts.aliases = {}
for char, user_alias in pairs(user_opts.aliases) do
char = input.replace_termcodes(char)
opts.aliases[char] = M.translate_alias(user_alias)
end
end
return opts
end

Expand Down
16 changes: 16 additions & 0 deletions tests/configuration_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ describe("configuration", function()
delete = "^(„)().-(“)()$",
},
},
aliases = {
[""] = ")",
},
})

set_lines({ "hey! hello world" })
Expand All @@ -79,6 +82,11 @@ describe("configuration", function()
check_lines({ "hey! „hello“ world" })
vim.cmd("normal ds“")
check_lines({ "hey! hello world" })

vim.cmd("normal yss•")
check_lines({ "(hey! hello world)" })
vim.cmd("normal ds•")
check_lines({ "hey! hello world" })
end)

it("can define and use 'interpreted' multi-byte mappings", function()
Expand All @@ -91,6 +99,9 @@ describe("configuration", function()
delete = "^(%[%[)().-(%]%])()$",
},
},
aliases = {
["<CR>"] = ")",
},
})
local meta_close_bracket = vim.api.nvim_replace_termcodes("<M-]>", true, false, true)
set_lines({ "hey! hello world" })
Expand All @@ -99,6 +110,11 @@ describe("configuration", function()
check_lines({ "hey! [[hello]] world" })
vim.cmd("normal ds" .. meta_close_bracket)
check_lines({ "hey! hello world" })

vim.cmd("normal yss" .. cr)
check_lines({ "(hey! hello world)" })
vim.cmd("normal ds" .. cr)
check_lines({ "hey! hello world" })
end)

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

0 comments on commit 10c2d96

Please sign in to comment.