Skip to content

Commit 0932d0c

Browse files
authored
fix!: i_<C-o>gcc inserts g@$ (#204)
Now `g@` is returned from the RHS callback of every operator-mode keybindings i.e., `gc[motion]`, `gcc` etc.. Instead of using a command we are now using a callback as the RHS and returning `g@` from that callback to used as an expression, which will be executed by neovim using `{ expr = true }` option. `<Plug>` mappings are also covered in this fix. - Before ```lua vim.keymap.set('n', 'gc', '<CMD>lua require("Comment.api").call("toggle.linewise")<CR>g@') ``` - After ```lua vim.keymap.set('n', 'gc', require("Comment.api").call("toggle.linewise", "g@"), { expr = true }) ``` > This PR also changes the `.call()` method signature, now it returns a function which is used to create keymap RHS --- Fixes #143
1 parent 4818a4c commit 0932d0c

File tree

2 files changed

+64
-102
lines changed

2 files changed

+64
-102
lines changed

lua/Comment/api.lua

Lines changed: 51 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,6 @@ end
267267
------------ OLD END ------------
268268

269269
local core = {}
270-
local extra = {}
271270

272271
function core.__index(that, ctype)
273272
local idxd = {}
@@ -300,20 +299,6 @@ function core.__index(that, ctype)
300299
})
301300
end
302301

303-
function extra.__index(_, ctype)
304-
return {
305-
above = function(cfg)
306-
Ex.insert_above(U.ctype[ctype], cfg or Config:get())
307-
end,
308-
below = function(cfg)
309-
Ex.insert_below(U.ctype[ctype], cfg or Config:get())
310-
end,
311-
eol = function(cfg)
312-
Ex.insert_eol(U.ctype[ctype], cfg or Config:get())
313-
end,
314-
}
315-
end
316-
317302
---API to toggle comments using line or block comment string
318303
---
319304
---Following are the API functions that are available:
@@ -368,27 +353,37 @@ api.uncomment = setmetatable({ cmode = U.cmode.comment }, core)
368353
---require('Comment.api').insert.blockwise.below({cfg?})
369354
---require('Comment.api').insert.blockwise.eol({cfg?})
370355
---@type table A metatable containing API functions
371-
api.insert = setmetatable({}, extra)
356+
api.insert = setmetatable({}, {
357+
__index = function(_, ctype)
358+
return {
359+
above = function(cfg)
360+
Ex.insert_above(U.ctype[ctype], cfg or Config:get())
361+
end,
362+
below = function(cfg)
363+
Ex.insert_below(U.ctype[ctype], cfg or Config:get())
364+
end,
365+
eol = function(cfg)
366+
Ex.insert_eol(U.ctype[ctype], cfg or Config:get())
367+
end,
368+
}
369+
end,
370+
})
372371

373372
---Wraps a given function with `lockmarks` to preserve marks/jumps when commenting
374-
---@type fun(cb:string):fun(opmotion:OpMotion)
373+
---@type fun(cb:string):fun(motion:OpMotion)
375374
---@usage `require('Comment.api').locked('toggle.linewise.current')()`
376375
api.locked = setmetatable({}, {
377-
__index = function(_, cb)
376+
__index = function(this, cb)
378377
D(string.format('locker.%s(args...)', cb), string.format('locked(%q)(args...)', cb))
379-
---Actual function which will be attached to operatorfunc
380-
---@param opmode OpMotion
381-
return function(opmode)
382-
return A.nvim_command(
383-
('lockmarks lua require("Comment.api").%s(%s)'):format(cb, opmode and ('%q'):format(opmode))
384-
)
385-
end
378+
return this(cb)
386379
end,
387380
-- TODO: After removal of the old api functions, make `api.locked` a simple function call
388381
__call = function(_, cb)
389-
return function(opmode)
382+
---Actual function which will be attached to operatorfunc
383+
---@param motion OpMotion
384+
return function(motion)
390385
return A.nvim_command(
391-
('lockmarks lua require("Comment.api").%s(%s)'):format(cb, opmode and ('%q'):format(opmode))
386+
('lockmarks lua require("Comment.api").%s(%s)'):format(cb, motion and ('%q'):format(motion))
392387
)
393388
end
394389
end,
@@ -399,11 +394,16 @@ api.locked = setmetatable({}, {
399394
--- 2. Preserves jumps and marks
400395
--- 3. Stores last cursor position
401396
---@param cb string Name of the API function to call
402-
---@usage `require('Comment.api').call('toggle.linewise')`
403-
function api.call(cb)
404-
A.nvim_set_option('operatorfunc', ("v:lua.require'Comment.api'.locked'%s'"):format(cb))
405-
Config.position = Config:get().sticky and A.nvim_win_get_cursor(0) or nil
406-
Config.count = vim.v.count
397+
---@param op 'g@'|'g@$' Operator string to execute
398+
---@return fun():string _ Keymap RHS callback
399+
---@usage `vim.keymap.set('n', 'gc', api.call('toggle.linewise', 'g@'), { expr = true })`
400+
function api.call(cb, op)
401+
return function()
402+
A.nvim_set_option('operatorfunc', ("v:lua.require'Comment.api'.locked'%s'"):format(cb))
403+
Config.position = Config:get().sticky and A.nvim_win_get_cursor(0) or nil
404+
Config.count = vim.v.count
405+
return op
406+
end
407407
end
408408

409409
---@private
@@ -422,18 +422,14 @@ function api.setup(config)
422422
K('n', cfg.opleader.line, '<Plug>(comment_toggle_linewise)', { desc = 'Comment toggle linewise' })
423423
K('n', cfg.opleader.block, '<Plug>(comment_toggle_blockwise)', { desc = 'Comment toggle blockwise' })
424424

425-
K(
426-
'n',
427-
cfg.toggler.line,
428-
"v:count == 0 ? '<Plug>(comment_toggle_linewise_current)' : '<Plug>(comment_toggle_linewise_count)'",
429-
{ expr = true, remap = true, replace_keycodes = false, desc = 'Comment toggle current line' }
430-
)
431-
K(
432-
'n',
433-
cfg.toggler.block,
434-
"v:count == 0 ? '<Plug>(comment_toggle_blockwise_current)' : '<Plug>(comment_toggle_blockwise_count)'",
435-
{ expr = true, remap = true, replace_keycodes = false, desc = 'Comment toggle current block' }
436-
)
425+
K('n', cfg.toggler.line, function()
426+
return vim.v.count == 0 and '<Plug>(comment_toggle_linewise_current)'
427+
or '<Plug>(comment_toggle_linewise_count)'
428+
end, { expr = true, desc = 'Comment toggle current line' })
429+
K('n', cfg.toggler.block, function()
430+
return vim.v.count == 0 and '<Plug>(comment_toggle_blockwise_current)'
431+
or '<Plug>(comment_toggle_blockwise_count)'
432+
end, { expr = true, desc = 'Comment toggle current block' })
437433

438434
-- VISUAL mode mappings
439435
K(
@@ -452,65 +448,30 @@ function api.setup(config)
452448

453449
-- Extra Mappings
454450
if cfg.mappings.extra then
455-
K(
456-
'n',
457-
cfg.extra.below,
458-
'<CMD>lua require("Comment.api").locked("insert.linewise.below")()<CR>',
459-
{ desc = 'Comment insert below' }
460-
)
461-
K(
462-
'n',
463-
cfg.extra.above,
464-
'<CMD>lua require("Comment.api").locked("insert.linewise.above")()<CR>',
465-
{ desc = 'Comment insert above' }
466-
)
467-
K(
468-
'n',
469-
cfg.extra.eol,
470-
'<CMD>lua require("Comment.api").locked("insert.linewise.eol")()<CR>',
471-
{ desc = 'Comment insert end of line' }
472-
)
451+
K('n', cfg.extra.below, api.locked('insert.linewise.below'), { desc = 'Comment insert below' })
452+
K('n', cfg.extra.above, api.locked('insert.linewise.above'), { desc = 'Comment insert above' })
453+
K('n', cfg.extra.eol, api.locked('insert.linewise.eol'), { desc = 'Comment insert end of line' })
473454
end
474455

475456
-- Extended Mappings
476457
if cfg.mappings.extended then
477458
-- NORMAL mode extended
478-
K(
479-
'n',
480-
'g>',
481-
'<CMD>lua require("Comment.api").call("comment.linewise")<CR>g@',
482-
{ desc = 'Comment region linewise' }
483-
)
484-
K(
485-
'n',
486-
'g>c',
487-
'<CMD>lua require("Comment.api").call("comment.linewise.current")<CR>g@$',
488-
{ desc = 'Comment current line' }
489-
)
490-
K(
491-
'n',
492-
'g>b',
493-
'<CMD>lua require("Comment.api").call("comment.blockwise.current")<CR>g@$',
494-
{ desc = 'Comment current block' }
495-
)
459+
K('n', 'g>', api.call('comment.linewise', 'g@'), { expr = true, desc = 'Comment region linewise' })
460+
K('n', 'g>c', api.call('comment.linewise.current', 'g@$'), { expr = true, desc = 'Comment current line' })
461+
K('n', 'g>b', api.call('comment.blockwise.current', 'g@$'), { expr = true, desc = 'Comment current block' })
496462

497-
K(
498-
'n',
499-
'g<',
500-
'<CMD>lua require("Comment.api").call("uncomment.linewise")<CR>g@',
501-
{ desc = 'Uncomment region linewise' }
502-
)
463+
K('n', 'g<', api.call('uncomment.linewise', 'g@'), { expr = true, desc = 'Uncomment region linewise' })
503464
K(
504465
'n',
505466
'g<c',
506-
'<CMD>lua require("Comment.api").call("uncomment.linewise.current")<CR>g@$',
507-
{ desc = 'Uncomment current line' }
467+
api.call('uncomment.linewise.current', 'g@$'),
468+
{ expr = true, desc = 'Uncomment current line' }
508469
)
509470
K(
510471
'n',
511472
'g<b',
512-
'<CMD>lua require("Comment.api").call("uncomment.blockwise.current")<CR>g@$',
513-
{ desc = 'Uncomment current block' }
473+
api.call('uncomment.blockwise.current', 'g@$'),
474+
{ expr = true, desc = 'Uncomment current block' }
514475
)
515476

516477
-- VISUAL mode extended

plugin/Comment.lua

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,46 @@
11
local K = vim.keymap.set
2+
local call = require('Comment.api').call
23

34
-- Operator-Pending mappings
45
K(
56
'n',
67
'<Plug>(comment_toggle_linewise)',
7-
'<CMD>lua require("Comment.api").call("toggle.linewise")<CR>g@',
8-
{ desc = 'Comment toggle linewise' }
8+
call('toggle.linewise', 'g@'),
9+
{ expr = true, desc = 'Comment toggle linewise' }
910
)
1011
K(
1112
'n',
1213
'<Plug>(comment_toggle_blockwise)',
13-
'<CMD>lua require("Comment.api").call("toggle.blockwise")<CR>g@',
14-
{ desc = 'Comment toggle blockwise' }
14+
call('toggle.blockwise', 'g@'),
15+
{ expr = true, desc = 'Comment toggle blockwise' }
1516
)
1617

1718
-- Toggle mappings
1819
K(
1920
'n',
2021
'<Plug>(comment_toggle_linewise_current)',
21-
'<CMD>lua require("Comment.api").call("toggle.linewise.current")<CR>g@$',
22-
{ desc = 'Comment toggle current line' }
22+
call('toggle.linewise.current', 'g@$'),
23+
{ expr = true, desc = 'Comment toggle current line' }
2324
)
2425
K(
2526
'n',
2627
'<Plug>(comment_toggle_blockwise_current)',
27-
'<CMD>lua require("Comment.api").call("toggle.blockwise.current")<CR>g@$',
28-
{ desc = 'Comment toggle current block' }
28+
call('toggle.blockwise.current', 'g@$'),
29+
{ expr = true, desc = 'Comment toggle current block' }
2930
)
3031

3132
-- Count mappings
3233
K(
3334
'n',
3435
'<Plug>(comment_toggle_linewise_count)',
35-
'<CMD>lua require("Comment.api").call("toggle.linewise.count_repeat")<CR>g@$',
36-
{ desc = 'Comment toggle linewise with count' }
36+
call('toggle.linewise.count_repeat', 'g@$'),
37+
{ expr = true, desc = 'Comment toggle linewise with count' }
3738
)
3839
K(
3940
'n',
4041
'<Plug>(comment_toggle_blockwise_count)',
41-
'<CMD>lua require("Comment.api").call("toggle.blockwise.count_repeat")<CR>g@$',
42-
{ desc = 'Comment toggle blockwise with count' }
42+
call('toggle.blockwise.count_repeat', 'g@$'),
43+
{ expr = true, desc = 'Comment toggle blockwise with count' }
4344
)
4445

4546
-- Visual-Mode mappings

0 commit comments

Comments
 (0)