@@ -18,13 +18,17 @@ M.insert_surround = function(args)
18
18
local config = require (" nvim-surround.config" )
19
19
local buffer = require (" nvim-surround.buffer" )
20
20
local input = require (" nvim-surround.input" )
21
- local char = input .get_char ()
22
- local curpos = buffer .get_curpos ()
23
- local delimiters = config .get_delimiters (char , args .line_mode )
21
+ local user_input = input .get_char ()
22
+ if not user_input then
23
+ return
24
+ end
25
+ -- TODO: Handle repeating!
26
+ local delimiters = config .get_delimiters (user_input .char , args .line_mode )
24
27
if not delimiters then
25
28
return
26
29
end
27
30
31
+ local curpos = buffer .get_curpos ()
28
32
buffer .insert_text (curpos , delimiters [2 ])
29
33
buffer .insert_text (curpos , delimiters [1 ])
30
34
buffer .set_curpos ({ curpos [1 ] + # delimiters [1 ] - 1 , curpos [2 ] + # delimiters [1 ][# delimiters [1 ]] })
@@ -86,12 +90,15 @@ M.visual_surround = function(args)
86
90
local config = require (" nvim-surround.config" )
87
91
local buffer = require (" nvim-surround.buffer" )
88
92
local input = require (" nvim-surround.input" )
89
- local ins_char = input .get_char ()
93
+ local user_input = input .get_char ()
94
+ if user_input == nil then
95
+ return
96
+ end
90
97
91
98
if vim .fn .visualmode () == " V" then
92
99
args .line_mode = true
93
100
end
94
- local delimiters = config .get_delimiters (ins_char , args .line_mode )
101
+ local delimiters = config .get_delimiters (user_input . char , args .line_mode )
95
102
local first_pos , last_pos = buffer .get_mark (" <" ), buffer .get_mark (" >" )
96
103
if not delimiters or not first_pos or not last_pos then
97
104
return
@@ -163,7 +170,7 @@ M.delete_surround = function(args)
163
170
-- Call the operatorfunc if it has not been called yet
164
171
if not args then
165
172
-- Clear the delete cache (since it was user-called)
166
- cache .delete = {}
173
+ cache .delete = nil
167
174
168
175
vim .go .operatorfunc = " v:lua.require'nvim-surround'.delete_callback"
169
176
return " g@l"
@@ -301,9 +308,14 @@ M.normal_callback = function(mode)
301
308
end
302
309
-- Get a character input and the delimiters (if not cached)
303
310
if not cache .normal .delimiters then
304
- local char = input .get_char ()
311
+ local user_input = input .get_char ()
312
+ if user_input == nil then
313
+ M .pending_surround = false
314
+ buffer .clear_highlights ()
315
+ return
316
+ end
305
317
-- Get the delimiter pair based on the input character
306
- cache .normal .delimiters = config .get_delimiters (char , cache .normal .line_mode )
318
+ cache .normal .delimiters = config .get_delimiters (user_input . char , cache .normal .line_mode )
307
319
if not cache .normal .delimiters then
308
320
M .pending_surround = false
309
321
buffer .clear_highlights ()
@@ -328,9 +340,11 @@ M.delete_callback = function()
328
340
-- Save the current position of the cursor
329
341
local curpos = buffer .get_curpos ()
330
342
-- Get a character input if not cached
331
- cache .delete .char = cache .delete .char or input .get_char ()
332
- if not cache .delete .char then
333
- return
343
+ if cache .delete == nil then
344
+ cache .delete = input .get_char ()
345
+ if cache .delete == nil then
346
+ return
347
+ end
334
348
end
335
349
336
350
M .delete_surround ({
@@ -348,7 +362,12 @@ M.change_callback = function()
348
362
-- Save the current position of the cursor
349
363
local curpos = buffer .get_curpos ()
350
364
if not cache .change .del_char or not cache .change .add_delimiters then
351
- local del_char = config .get_alias (input .get_char ())
365
+ local user_input = input .get_char ()
366
+ if user_input == nil then
367
+ return
368
+ end
369
+
370
+ local del_char = config .get_alias (user_input .char )
352
371
local change = config .get_change (del_char )
353
372
local selections = utils .get_nearest_selections (del_char , " change" )
354
373
if not (del_char and change and selections ) then
@@ -366,12 +385,15 @@ M.change_callback = function()
366
385
end
367
386
368
387
-- Get the new surrounding pair, querying the user for more input if no replacement is provided
369
- local ins_char , delimiters
388
+ local delimiters
370
389
if change and change .replacement then
371
390
delimiters = change .replacement ()
372
391
else
373
- ins_char = input .get_char ()
374
- delimiters = config .get_delimiters (ins_char , cache .change .line_mode )
392
+ local user_input = input .get_char ()
393
+ if user_input == nil then
394
+ return
395
+ end
396
+ delimiters = config .get_delimiters (user_input .char , cache .change .line_mode )
375
397
end
376
398
377
399
-- Clear the highlights after getting the replacement surround
0 commit comments