Skip to content

Commit 246c646

Browse files
committed
(mini.operators) Update default_evaluate_func() to not modify input.
1 parent 8016270 commit 246c646

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

lua/mini/operators.lua

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -710,10 +710,11 @@ H.create_default_hl =
710710

711711
-- Evaluate -------------------------------------------------------------------
712712
H.eval_lua_lines = function(lines)
713-
local n = #lines
714-
lines[n] = (lines[n]:find('^%s*return%s+') == nil and 'return ' or '') .. lines[n]
713+
-- Copy to not modify input
714+
local lines_copy, n = vim.deepcopy(lines), #lines
715+
lines_copy[n] = (lines_copy[n]:find('^%s*return%s+') == nil and 'return ' or '') .. lines_copy[n]
715716

716-
local str_to_eval = table.concat(lines, '\n')
717+
local str_to_eval = table.concat(lines_copy, '\n')
717718

718719
-- Allow returning tuple with any value(s) being `nil`
719720
return H.inspect_objects(assert(loadstring(str_to_eval))())

tests/test_operators.lua

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,22 @@ T['default_evaluate_func()']['works for blockwise'] = function()
338338
validate({ '_G.y + 1' }, { '2' })
339339
end
340340

341+
T['default_evaluate_func()']['does not modify input'] = function()
342+
local validate = function(content)
343+
local lua_cmd = string.format(
344+
[[_G.content = %s
345+
MiniOperators.default_evaluate_func(_G.content)]],
346+
vim.inspect(content)
347+
)
348+
child.lua(lua_cmd)
349+
eq(child.lua_get('_G.content'), content)
350+
end
351+
352+
validate({ lines = { '1 + 1' }, submode = 'v' })
353+
validate({ lines = { '1 + 1' }, submode = 'V' })
354+
validate({ lines = { '1 + 1', '1 + 2' }, submode = '\22' })
355+
end
356+
341357
T['default_evaluate_func()']['validates arguments'] = function()
342358
expect.error(default_evaluate_func, '`content`', 1)
343359
expect.error(default_evaluate_func, '`content`', {})

0 commit comments

Comments
 (0)