Skip to content

Commit 9744a8e

Browse files
committed
feat: Repeat delete.
1 parent f081cd0 commit 9744a8e

File tree

4 files changed

+51
-9
lines changed

4 files changed

+51
-9
lines changed

lua/nvim-surround/cache.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ local M = {}
44

55
---@type { delimiters: string[][]|nil, line_mode: boolean, count: integer }
66
M.normal = {}
7-
---@type { char: string }
7+
---@type { char: string, count: integer }
88
M.delete = {}
99
---@type { del_char: string, add_delimiters: add_func, line_mode: boolean }
1010
M.change = {}
@@ -13,7 +13,7 @@ M.change = {}
1313
---@param func_name string A string representing the callback function's name.
1414
M.set_callback = function(func_name)
1515
vim.go.operatorfunc = "v:lua.require'nvim-surround.utils'.NOOP"
16-
vim.cmd.normal({ "g@l", bang = true })
16+
vim.cmd.normal({ [1] = "g@l", bang = true })
1717
vim.go.operatorfunc = func_name
1818
end
1919

lua/nvim-surround/init.lua

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ M.delete_surround = function(args)
176176
-- Call the operatorfunc if it has not been called yet
177177
if not args then
178178
-- Clear the delete cache (since it was user-called)
179-
cache.delete = {}
179+
cache.delete = { count = vim.v.count1 }
180180

181181
vim.go.operatorfunc = "v:lua.require'nvim-surround'.delete_callback"
182182
return "g@l"
@@ -338,18 +338,18 @@ M.delete_callback = function()
338338
local buffer = require("nvim-surround.buffer")
339339
local cache = require("nvim-surround.cache")
340340
local input = require("nvim-surround.input")
341-
-- Save the current position of the cursor
342-
local curpos = buffer.get_curpos()
343341
-- Get a character input if not cached
344342
cache.delete.char = cache.delete.char or input.get_char()
345343
if not cache.delete.char then
346344
return
347345
end
348346

349-
M.delete_surround({
350-
del_char = cache.delete.char,
351-
curpos = curpos,
352-
})
347+
for _ = 1, cache.delete.count do
348+
M.delete_surround({
349+
del_char = cache.delete.char,
350+
curpos = buffer.get_curpos(),
351+
})
352+
end
353353
end
354354

355355
M.change_callback = function()

tests/basics_spec.lua

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -866,4 +866,23 @@ describe("nvim-surround", function()
866866
"a sli<<<ghtly longer l>>>ine",
867867
})
868868
end)
869+
870+
it("can handle number prefixing for deleting surrounds", function()
871+
set_lines({ "some {{{{more placeholder}}}} text" })
872+
set_curpos({ 1, 6 })
873+
vim.cmd("normal 2dsB")
874+
check_lines({ "some {{more placeholder}} text" })
875+
vim.cmd("normal .")
876+
check_lines({ "some more placeholder text" })
877+
878+
set_lines({ "((foo) bar (baz))" })
879+
set_curpos({ 1, 9 })
880+
vim.cmd("normal 2dsb")
881+
check_lines({ "foo bar (baz)" })
882+
883+
set_lines({ "some ((more placeholder)) text" })
884+
set_curpos({ 1, 6 })
885+
vim.cmd("normal 3dsb")
886+
check_lines({ "some more placeholder text" })
887+
end)
869888
end)

tests/configuration_spec.lua

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,4 +592,27 @@ describe("configuration", function()
592592
assert.are.same(get_curpos(), { 1, 1 })
593593
check_lines({ "print('foo')" })
594594
end)
595+
596+
it("will handle number prefixing as if the user used dot-repeat", function()
597+
require("nvim-surround").setup({ move_cursor = "sticky" })
598+
set_lines({ "foo bar baz" })
599+
set_curpos({ 1, 5 })
600+
vim.cmd("normal 3ysiwb")
601+
check_lines({ "foo (((bar))) baz" })
602+
check_curpos({ 1, 8 })
603+
vim.cmd("normal 2ySSa")
604+
check_lines({
605+
"<",
606+
"<",
607+
"foo (((bar))) baz",
608+
">",
609+
">",
610+
})
611+
612+
set_lines({ "((foo) bar (baz))" })
613+
set_curpos({ 1, 9 })
614+
vim.cmd("normal 2dsb")
615+
check_lines({ "(foo) bar baz" })
616+
check_curpos({ 1, 8 })
617+
end)
595618
end)

0 commit comments

Comments
 (0)