From f67e381442c50e044310100bee07b505e174100e Mon Sep 17 00:00:00 2001 From: gbprod Date: Mon, 13 Nov 2023 09:02:40 +0100 Subject: [PATCH] wip --- lua/substitute.lua | 10 +++- spec/substitute/wrappers/config_spec.lua | 59 ++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 2 deletions(-) create mode 100644 spec/substitute/wrappers/config_spec.lua diff --git a/lua/substitute.lua b/lua/substitute.lua index 3615945..8bab08f 100644 --- a/lua/substitute.lua +++ b/lua/substitute.lua @@ -56,8 +56,14 @@ function substitute.operator_callback(vmode) end end - local wrapper = substitute.state.wrappers and require("substitute.wrappers").build(substitute.state.wrappers) - or doSubstitution + local wrapper + if type(substitute.state.wrappers) == "function" then + wrapper = require("substitute.wrappers").build(substitute.state.wrappers(substitute.state)) + elseif type(substitute.state.wrappers) == "table" then + wrapper = require("substitute.wrappers").build(substitute.state.wrappers) + else + wrapper = doSubstitution + end wrapper(substitute.state, doSubstitution) diff --git a/spec/substitute/wrappers/config_spec.lua b/spec/substitute/wrappers/config_spec.lua new file mode 100644 index 0000000..aa6b12b --- /dev/null +++ b/spec/substitute/wrappers/config_spec.lua @@ -0,0 +1,59 @@ +local substitute = require("substitute") + +local function execute_keys(feedkeys) + local keys = vim.api.nvim_replace_termcodes(feedkeys, true, false, true) + vim.api.nvim_feedkeys(keys, "x", false) +end + +local function get_buf_lines() + return vim.api.nvim_buf_get_lines(0, 0, -1, true) +end + +local buf +describe("Substitute wrappers", function() + before_each(function() + substitute.setup() + + buf = vim.api.nvim_create_buf(false, true) + vim.api.nvim_command("buffer " .. buf) + end) + + it("should be taken from a function", function() + vim.api.nvim_buf_set_lines(buf, 0, -1, true, { "Lorem", "ipsum", "dolor", "sit", "amet" }) + + vim.keymap.set({ "n", "x" }, "]s", function() + require("substitute").operator({ + wrappers = function(_) + return { "linewise" } + end, + }) + end, { noremap = true }) + + execute_keys("lly2l") + execute_keys("j") + execute_keys("]s2l") + + assert.are.same({ "Lorem", "ip", "re", "m", "dolor", "sit", "amet" }, get_buf_lines()) + end) + + it("could be conditionnal", function() + vim.api.nvim_buf_set_lines(buf, 0, -1, true, { " Lorem ", "ipsum", "dolor", "sit", "amet" }) + + vim.keymap.set({ "n", "x" }, "]s", function() + require("substitute").operator({ + wrappers = function(state) + return state.vmode == "char" and { "trim" } or { "linewise" } + end, + }) + end, { noremap = true }) + + execute_keys("yy") + execute_keys("jll") + execute_keys("]s2l") + + execute_keys("jV") + execute_keys("]s") + + assert.are.same({ " Lorem ", "ipLoremm", " Lorem ", "sit", "amet" }, get_buf_lines()) + end) +end)