Skip to content

Commit ab94192

Browse files
committed
(mini.operators) FEATURE: Add config.multiply.func.
1 parent 79f9a8d commit ab94192

File tree

4 files changed

+61
-1
lines changed

4 files changed

+61
-1
lines changed

doc/mini-operators.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,9 @@ Default values:
190190
-- Multiply (duplicate) text
191191
multiply = {
192192
prefix = 'gm',
193+
194+
-- Function which can modify text before multiplying
195+
func = nil,
193196
},
194197
195198
-- Replace text with register
@@ -242,6 +245,12 @@ regions are exchanged preserving their indents; if `true` - without them.
242245
`multiply.prefix` is a string used to automatically infer operator mappings keys
243246
during |MiniOperators.setup()|. See |MiniOperators-mappings|.
244247

248+
`multiply.func` is a function used to optionally update multiplied text.
249+
If `nil` (default), text used as is.
250+
251+
Takes content table as input (see "Evaluate" section) and should return
252+
array of lines as output.
253+
245254
# Replace ~
246255

247256
`replace.prefix` is a string used to automatically infer operator mappings keys

lua/mini/operators.lua

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,12 @@ end
218218
--- `multiply.prefix` is a string used to automatically infer operator mappings keys
219219
--- during |MiniOperators.setup()|. See |MiniOperators-mappings|.
220220
---
221+
--- `multiply.func` is a function used to optionally update multiplied text.
222+
--- If `nil` (default), text used as is.
223+
---
224+
--- Takes content table as input (see "Evaluate" section) and should return
225+
--- array of lines as output.
226+
---
221227
--- # Replace ~
222228
---
223229
--- `replace.prefix` is a string used to automatically infer operator mappings keys
@@ -275,6 +281,9 @@ MiniOperators.config = {
275281
-- Multiply (duplicate) text
276282
multiply = {
277283
prefix = 'gm',
284+
285+
-- Function which can modify text before multiplying
286+
func = nil,
278287
},
279288

280289
-- Replace text with register
@@ -404,6 +413,12 @@ MiniOperators.multiply = function(mode)
404413
local yank_data = { mark_from = mark_from, mark_to = mark_to, submode = submode, mode = mode, register = 'x' }
405414
H.do_between_marks('y', yank_data)
406415

416+
-- Modify lines in "x" register
417+
local func = H.get_config().multiply.func or function(content) return content.lines end
418+
local x_reginfo = vim.fn.getreginfo('x')
419+
x_reginfo.regcontents = func({ lines = x_reginfo.regcontents, submode = submode })
420+
vim.fn.setreg('x', x_reginfo)
421+
407422
-- Adjust cursor for a proper paste
408423
local ref_coords = H.multiply_get_ref_coords(mark_from, mark_to, submode)
409424
vim.api.nvim_win_set_cursor(0, ref_coords)
@@ -664,6 +679,7 @@ H.setup_config = function(config)
664679
['exchange.reindent_linewise'] = { config.exchange.reindent_linewise, 'boolean' },
665680

666681
['multiply.prefix'] = { config.multiply.prefix, 'string' },
682+
['multiply.func'] = { config.multiply.func, 'function', true },
667683

668684
['replace.prefix'] = { config.replace.prefix, 'string' },
669685
['replace.reindent_linewise'] = { config.replace.reindent_linewise, 'boolean' },

readmes/mini-operators.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ If you want to help this project grow but don't know where to start, check out [
2222

2323
## Demo
2424

25-
https://github.com/echasnovski/mini.nvim/assets/24854248/aaa9df18-fe39-4556-89d4-a0591edf31a3
25+
https://github.com/echasnovski/mini.nvim/assets/24854248/8a3656c4-c92a-4d9f-9711-8d6a751b3e5a
2626

2727
## Features
2828

@@ -178,6 +178,9 @@ Here are code snippets for some common installation methods (use only one):
178178
-- Multiply (duplicate) text
179179
multiply = {
180180
prefix = 'gm',
181+
182+
-- Function which can modify text before multiplying
183+
func = nil,
181184
},
182185

183186
-- Replace text with register

tests/test_operators.lua

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ T['setup()']['creates `config` field'] = function()
7575
expect_config('exchange.prefix', 'gx')
7676
expect_config('exchange.reindent_linewise', true)
7777

78+
expect_config('multiply.prefix', 'gm')
79+
expect_config('multiply.func', vim.NIL)
80+
7881
expect_config('replace.prefix', 'gr')
7982
expect_config('replace.reindent_linewise', true)
8083

@@ -103,6 +106,10 @@ T['setup()']['validates `config` argument'] = function()
103106
expect_config_error({ exchange = { prefix = 1 } }, 'exchange.prefix', 'string')
104107
expect_config_error({ exchange = { reindent_linewise = 'a' } }, 'exchange.reindent_linewise', 'boolean')
105108

109+
expect_config_error({ multiply = 'a' }, 'multiply', 'table')
110+
expect_config_error({ multiply = { prefix = 1 } }, 'multiply.prefix', 'string')
111+
expect_config_error({ multiply = { func = 'a' } }, 'multiply.func', 'function')
112+
106113
expect_config_error({ replace = 'a' }, 'replace', 'table')
107114
expect_config_error({ replace = { prefix = 1 } }, 'replace.prefix', 'string')
108115
expect_config_error({ replace = { reindent_linewise = 'a' } }, 'replace.reindent_linewise', 'boolean')
@@ -1286,6 +1293,20 @@ T['Multiply']['works in edge cases'] = function()
12861293
validate_edit({ 'aa', 'bb' }, { 2, 0 }, { 'gm_' }, { 'aa', 'bb', 'bb' }, { 3, 0 })
12871294
end
12881295

1296+
T['Multiply']['respects `config.multiply.func`'] = function()
1297+
-- Indent by two spaces only for linewise content
1298+
child.lua([[MiniOperators.config.multiply.func = function(content)
1299+
if content.submode ~= 'V' then return content.lines end
1300+
return vim.tbl_map(function(l) return ' ' .. l end, content.lines)
1301+
end]])
1302+
1303+
validate_edit1d('aa bb', 0, { 'gmiw' }, 'aaaa bb', 2)
1304+
validate_edit({ 'aa', 'bb', '', 'cc' }, { 1, 0 }, { 'gmip' }, { 'aa', 'bb', ' aa', ' bb', '', 'cc' }, { 3, 2 })
1305+
1306+
if child.fn.has('nvim-0.9') == 0 then MiniTest.skip('Blockwise selection has core issues on Neovim<0.9.') end
1307+
validate_edit({ 'ab', 'cd' }, { 1, 0 }, { '<C-v>j', 'gm' }, { 'aab', 'ccd' }, { 1, 1 })
1308+
end
1309+
12891310
T['Multiply']["works with 'y' in 'cpoptions'"] = function()
12901311
child.cmd('set cpoptions+=y')
12911312

@@ -1422,6 +1443,17 @@ T['Multiply']['respects `vim.{g,b}.minioperators_disable`'] = new_set({
14221443
end,
14231444
})
14241445

1446+
T['Multiply']['respects `vim.b.minioperators_config`'] = function()
1447+
-- Indent by two spaces
1448+
child.lua([[_G.multiply_func = function(content)
1449+
return vim.tbl_map(function(l) return ' ' .. l end, content.lines)
1450+
end
1451+
vim.b.minioperators_config = { multiply = { func = _G.multiply_func } }
1452+
]])
1453+
1454+
validate_edit({ 'aa', 'bb', '', 'cc' }, { 1, 0 }, { 'gmip' }, { 'aa', 'bb', ' aa', ' bb', '', 'cc' }, { 3, 2 })
1455+
end
1456+
14251457
T['Replace'] = new_set()
14261458

14271459
T['Replace']['works charwise in Normal mode'] = function()

0 commit comments

Comments
 (0)