@@ -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
408420end
409421
410422return M
0 commit comments