From 4cc727581cfb090dd1842caa478d23afc0af516f Mon Sep 17 00:00:00 2001 From: Peter Morgenstern Date: Wed, 20 Dec 2023 06:23:15 +0100 Subject: [PATCH] feat(Subs): adjustable name for substitude command --- README.md | 5 +++- lua/textcase/plugin/presets.lua | 14 ++++++++-- .../options/substitue_command_name_spec.lua | 26 +++++++++++++++++++ 3 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 tests/textcase/plugin/options/substitue_command_name_spec.lua diff --git a/README.md b/README.md index 7915f9c..83a7dbe 100644 --- a/README.md +++ b/README.md @@ -91,7 +91,7 @@ use { "johmsalas/text-case.nvim", { "johmsalas/text-case.nvim", dependencies = { "nvim-telescope/telescope.nvim" }, - -- Author's Note: If default keymappings fail to register (possible config issue in my local setup), + -- Author's Note: If default keymappings fail to register (possible config issue in my local setup), -- verify lazy loading functionality. On failure, disable lazy load and report issue -- lazy = false, config = function() @@ -114,6 +114,9 @@ use { "johmsalas/text-case.nvim", -- of the keymappings, e.g. `gau ` executes the `current_word` method with `to_upper_case` -- and `gaou` executes the `operator` method with `to_upper_case`. prefix = "ga", + -- If `substitude_command_name` is not nil, an additional command with the passed in name + -- will be created that does the same thing as "Subs" does. + substitude_command_name = nil, -- By default, all methods are enabled. If you set this option with some methods omitted, -- these methods will not be registered in the default keymappings. The methods will still -- be accessible when calling the exact lua function e.g.: diff --git a/lua/textcase/plugin/presets.lua b/lua/textcase/plugin/presets.lua index b09195b..bd5c414 100644 --- a/lua/textcase/plugin/presets.lua +++ b/lua/textcase/plugin/presets.lua @@ -60,14 +60,18 @@ local function setup_default_keymappings() end end -M.Initialize = function() +local function register_replace_command(substitude_command_name) local replace_command_methods = {} for _, method_name in ipairs(all_methods) do plugin.register_methods(api[method_name]) table.insert(replace_command_methods, api[method_name]) end - plugin.register_replace_command("Subs", replace_command_methods) + plugin.register_replace_command(substitude_command_name, replace_command_methods) +end + +M.Initialize = function() + register_replace_command("Subs") end -- Set all methods as default in case the setup function is not called. @@ -76,6 +80,12 @@ M.enabled_methods_set = all_methods M.setup = function(opts) M.options.prefix = opts and opts.prefix or "ga" + if opts and opts.substitude_command_name ~= nil then + -- Register the substitude command with the passed in name again. + -- This is needed because we don't require the user to call the setup function. + register_replace_command(opts.substitude_command_name) + end + M.options.default_keymappings_enabled = true if opts and opts.default_keymappings_enabled ~= nil then M.options.default_keymappings_enabled = opts.default_keymappings_enabled diff --git a/tests/textcase/plugin/options/substitue_command_name_spec.lua b/tests/textcase/plugin/options/substitue_command_name_spec.lua new file mode 100644 index 0000000..def9d19 --- /dev/null +++ b/tests/textcase/plugin/options/substitue_command_name_spec.lua @@ -0,0 +1,26 @@ +local textcase = require("textcase") +local test_helpers = require("tests.test_helpers") + +describe("plugin options substitude_command_name", function() + before_each(function() + textcase.setup({ + substitude_command_name = "Magic", + }) + + local buf = vim.api.nvim_create_buf(false, true) + vim.api.nvim_command("buffer " .. buf) + vim.api.nvim_buf_set_lines(0, 0, -1, true, { "LoremIpsum", "LoremNunc" }) + end) + + it("should execute the custom substitude command with the passed in name", function() + test_helpers.execute_keys("Vj:Magic/lorem/dolor") + + assert.are.same({ "DolorIpsum", "DolorNunc" }, test_helpers.get_buf_lines()) + end) + + it("should execute the default substitude command", function() + test_helpers.execute_keys("Vj:Subs/lorem/dolor") + + assert.are.same({ "DolorIpsum", "DolorNunc" }, test_helpers.get_buf_lines()) + end) +end)