Skip to content

Commit 87725ab

Browse files
committed
feat: Repeat change.
1 parent e1aaa3b commit 87725ab

File tree

4 files changed

+55
-18
lines changed

4 files changed

+55
-18
lines changed

lua/nvim-surround/cache.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ local M = {}
66
M.normal = {}
77
---@type { char: string, count: integer }
88
M.delete = {}
9-
---@type { del_char: string, add_delimiters: add_func, line_mode: boolean }
9+
---@type { del_char: string, add_delimiters: add_func, line_mode: boolean, count: integer }
1010
M.change = {}
1111

1212
-- Sets the callback function for dot-repeating.

lua/nvim-surround/init.lua

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ M.change_surround = function(args)
213213
-- Call the operatorfunc if it has not been called yet
214214
if not args.del_char or not args.add_delimiters then
215215
-- Clear the change cache (since it was user-called)
216-
cache.change = { line_mode = args.line_mode }
216+
cache.change = { line_mode = args.line_mode, count = vim.v.count1 }
217217

218218
vim.go.operatorfunc = "v:lua.require'nvim-surround'.change_callback"
219219
return "g@l"
@@ -358,13 +358,16 @@ M.change_callback = function()
358358
local cache = require("nvim-surround.cache")
359359
local input = require("nvim-surround.input")
360360
local utils = require("nvim-surround.utils")
361-
-- Save the current position of the cursor
362-
local curpos = buffer.get_curpos()
363-
if not cache.change.del_char or not cache.change.add_delimiters then
364-
local del_char = config.get_alias(input.get_char())
365-
local change = config.get_change(del_char)
361+
362+
local del_char = cache.change.del_char or config.get_alias(input.get_char())
363+
local change = config.get_change(del_char)
364+
if not (del_char and change) then
365+
return
366+
end
367+
368+
for _ = 1, cache.change.count do
366369
local selections = utils.get_nearest_selections(del_char, "change")
367-
if not (del_char and change and selections) then
370+
if not selections then
368371
return
369372
end
370373

@@ -378,13 +381,15 @@ M.change_callback = function()
378381
end
379382
end
380383

381-
-- Get the new surrounding pair, querying the user for more input if no replacement is provided
382-
local ins_char, delimiters
383-
if change and change.replacement then
384-
delimiters = change.replacement()
385-
else
386-
ins_char = input.get_char()
387-
delimiters = config.get_delimiters(ins_char, cache.change.line_mode)
384+
-- Get the new surrounding delimiter pair, prioritizing any delimiters in the cache
385+
local delimiters = cache.change.add_delimiters and cache.change.add_delimiters()
386+
if not delimiters then
387+
if change and change.replacement then
388+
delimiters = delimiters or change.replacement()
389+
else
390+
local ins_char = input.get_char()
391+
delimiters = delimiters or config.get_delimiters(ins_char, cache.change.line_mode)
392+
end
388393
end
389394

390395
-- Clear the highlights after getting the replacement surround
@@ -400,11 +405,18 @@ M.change_callback = function()
400405
return delimiters
401406
end,
402407
line_mode = cache.change.line_mode,
408+
count = cache.change.count,
403409
}
410+
M.change_surround({
411+
del_char = del_char,
412+
add_delimiters = function()
413+
return delimiters
414+
end,
415+
line_mode = cache.change.line_mode,
416+
count = cache.change.count,
417+
curpos = buffer.get_curpos(),
418+
})
404419
end
405-
local args = vim.deepcopy(cache.change)
406-
args.curpos = curpos
407-
M.change_surround(args) ---@diagnostic disable-line: param-type-mismatch
408420
end
409421

410422
return M

tests/basics_spec.lua

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -885,4 +885,23 @@ describe("nvim-surround", function()
885885
vim.cmd("normal 3dsb")
886886
check_lines({ "some more placeholder text" })
887887
end)
888+
889+
it("can handle number prefixing for changing surrounds", function()
890+
set_lines({ "some {{{{more placeholder}}}} text" })
891+
set_curpos({ 1, 11 })
892+
vim.cmd("normal 2csBa")
893+
check_lines({ "some {{<<more placeholder>>}} text" })
894+
vim.cmd("normal .")
895+
check_lines({ "some <<<<more placeholder>>>> text" })
896+
897+
set_lines({ "((foo) bar (baz))" })
898+
set_curpos({ 1, 9 })
899+
vim.cmd("normal 2csbB")
900+
check_lines({ "{{foo} bar (baz)}" })
901+
902+
set_lines({ "some ((more placeholder)) text" })
903+
set_curpos({ 1, 6 })
904+
vim.cmd("normal 3csbr")
905+
check_lines({ "some [[more placeholder]] text" })
906+
end)
888907
end)

tests/configuration_spec.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,5 +614,11 @@ describe("configuration", function()
614614
vim.cmd("normal 2dsb")
615615
check_lines({ "(foo) bar baz" })
616616
check_curpos({ 1, 8 })
617+
618+
set_lines({ "((foo) bar (baz))" })
619+
set_curpos({ 1, 9 })
620+
vim.cmd("normal 2csbr")
621+
check_lines({ "[(foo) bar [baz]]" })
622+
check_curpos({ 1, 9 })
617623
end)
618624
end)

0 commit comments

Comments
 (0)