From 10c2d96b2df9a8c6ef6cb8f209453b6fd2cfe812 Mon Sep 17 00:00:00 2001 From: Kyle Chui Date: Thu, 28 Nov 2024 10:22:03 -0800 Subject: [PATCH] feat: Support multi-byte mappings for aliases. --- lua/nvim-surround/config.lua | 43 ++++++++++++++++++++++++------------ tests/configuration_spec.lua | 16 ++++++++++++++ 2 files changed, 45 insertions(+), 14 deletions(-) diff --git a/lua/nvim-surround/config.lua b/lua/nvim-surround/config.lua index 313bc5e..05458b1 100644 --- a/lua/nvim-surround/config.lua +++ b/lua/nvim-surround/config.lua @@ -540,6 +540,16 @@ 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. @@ -547,29 +557,34 @@ 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 diff --git a/tests/configuration_spec.lua b/tests/configuration_spec.lua index 37d6d17..586f557 100644 --- a/tests/configuration_spec.lua +++ b/tests/configuration_spec.lua @@ -71,6 +71,9 @@ describe("configuration", function() delete = "^(„)().-(“)()$", }, }, + aliases = { + ["•"] = ")", + }, }) set_lines({ "hey! hello world" }) @@ -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() @@ -91,6 +99,9 @@ describe("configuration", function() delete = "^(%[%[)().-(%]%])()$", }, }, + aliases = { + [""] = ")", + }, }) local meta_close_bracket = vim.api.nvim_replace_termcodes("", true, false, true) set_lines({ "hey! hello world" }) @@ -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()