Skip to content

Commit 1b56191

Browse files
committed
feat: Very jank way of supporting numeric prefixes for normal_surround.
1 parent ae29810 commit 1b56191

File tree

3 files changed

+26
-7
lines changed

3 files changed

+26
-7
lines changed

lua/nvim-surround/cache.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ local M = {}
22

33
-- These variables hold cache values for dot-repeating the three actions
44

5-
---@type { delimiters: string[][]|nil, line_mode: boolean }
5+
---@type { delimiters: string[][]|nil, line_mode: boolean, count: integer }
66
M.normal = {}
77
---@type { char: string }
88
M.delete = {}

lua/nvim-surround/init.lua

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,23 +50,26 @@ M.normal_surround = function(args)
5050
local config = require("nvim-surround.config")
5151
local buffer = require("nvim-surround.buffer")
5252
local cache = require("nvim-surround.cache")
53+
local utils = require("nvim-surround.utils")
5354
-- Call the operatorfunc if it has not been called yet
5455
if not args.selection then
55-
-- Clear the normal cache (since it was user-called)
56-
cache.normal = { line_mode = args.line_mode }
56+
-- Clear the normal cache's delimiters (since it was user-called)
57+
cache.normal = { line_mode = args.line_mode, count = vim.v.count1 }
5758
M.normal_curpos = buffer.get_curpos()
5859
M.pending_surround = true
5960

6061
vim.go.operatorfunc = "v:lua.require'nvim-surround'.normal_callback"
61-
return "g@"
62+
-- TODO: Very hacky way of supporting just 1 digit, need to find better way to set v:count to 1
63+
return "<Del>g@"
6264
end
6365

6466
local first_pos = args.selection.first_pos
6567
local last_pos = { args.selection.last_pos[1], args.selection.last_pos[2] + 1 }
6668

69+
local delimiters = utils.repeat_delimiters(args.delimiters, cache.normal.count)
6770
local sticky_pos = buffer.with_extmark(M.normal_curpos, function()
68-
buffer.insert_text(last_pos, args.delimiters[2])
69-
buffer.insert_text(first_pos, args.delimiters[1])
71+
buffer.insert_text(last_pos, delimiters[2])
72+
buffer.insert_text(first_pos, delimiters[1])
7073
end)
7174
buffer.restore_curpos({
7275
first_pos = first_pos,
@@ -75,7 +78,7 @@ M.normal_surround = function(args)
7578
})
7679

7780
if args.line_mode then
78-
config.get_opts().indent_lines(first_pos[1], last_pos[1] + #args.delimiters[1] + #args.delimiters[2] - 2)
81+
config.get_opts().indent_lines(first_pos[1], last_pos[1] + #delimiters[1] + #delimiters[2] - 2)
7982
end
8083
M.pending_surround = false
8184
end

lua/nvim-surround/utils.lua

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,22 @@ local M = {}
77
-- Do nothing.
88
M.NOOP = function() end
99

10+
-- Repeats a delimiter pair n times.
11+
---@param delimiters delimiter_pair The delimiters to be repeated.
12+
---@param n integer The number of times to repeat the delimiters.
13+
---@return delimiter_pair @The repeated delimiters.
14+
---@nodiscard
15+
M.repeat_delimiters = function(delimiters, n)
16+
local acc = { { "" }, { "" } }
17+
for _ = 1, n do
18+
acc[1][#acc[1]] = acc[1][#acc[1]] .. delimiters[1][1]
19+
vim.list_extend(acc[1], delimiters[1], 2)
20+
acc[2][#acc[2]] = acc[2][#acc[2]] .. delimiters[2][1]
21+
vim.list_extend(acc[2], delimiters[2], 2)
22+
end
23+
return acc
24+
end
25+
1026
-- Gets the nearest two selections for the left and right surrounding pair.
1127
---@param char string|nil A character representing what kind of surrounding pair is to be selected.
1228
---@param action "delete"|"change" A string representing what action is being performed.

0 commit comments

Comments
 (0)