267
267
---- -------- OLD END ------------
268
268
269
269
local core = {}
270
- local extra = {}
271
270
272
271
function core .__index (that , ctype )
273
272
local idxd = {}
@@ -300,20 +299,6 @@ function core.__index(that, ctype)
300
299
})
301
300
end
302
301
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
-
317
302
--- API to toggle comments using line or block comment string
318
303
---
319
304
--- Following are the API functions that are available:
@@ -368,27 +353,37 @@ api.uncomment = setmetatable({ cmode = U.cmode.comment }, core)
368
353
--- require('Comment.api').insert.blockwise.below({cfg?})
369
354
--- require('Comment.api').insert.blockwise.eol({cfg?})
370
355
--- @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
+ })
372
371
373
372
--- 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 )
375
374
--- @usage `require('Comment.api').locked('toggle.linewise.current')()`
376
375
api .locked = setmetatable ({}, {
377
- __index = function (_ , cb )
376
+ __index = function (this , cb )
378
377
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 )
386
379
end ,
387
380
-- TODO: After removal of the old api functions, make `api.locked` a simple function call
388
381
__call = function (_ , cb )
389
- return function (opmode )
382
+ --- Actual function which will be attached to operatorfunc
383
+ --- @param motion OpMotion
384
+ return function (motion )
390
385
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 ))
392
387
)
393
388
end
394
389
end ,
@@ -399,11 +394,16 @@ api.locked = setmetatable({}, {
399
394
--- 2. Preserves jumps and marks
400
395
--- 3. Stores last cursor position
401
396
--- @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
407
407
end
408
408
409
409
--- @private
@@ -422,18 +422,14 @@ function api.setup(config)
422
422
K (' n' , cfg .opleader .line , ' <Plug>(comment_toggle_linewise)' , { desc = ' Comment toggle linewise' })
423
423
K (' n' , cfg .opleader .block , ' <Plug>(comment_toggle_blockwise)' , { desc = ' Comment toggle blockwise' })
424
424
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' })
437
433
438
434
-- VISUAL mode mappings
439
435
K (
@@ -452,65 +448,30 @@ function api.setup(config)
452
448
453
449
-- Extra Mappings
454
450
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' })
473
454
end
474
455
475
456
-- Extended Mappings
476
457
if cfg .mappings .extended then
477
458
-- 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' })
496
462
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' })
503
464
K (
504
465
' n' ,
505
466
' 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' }
508
469
)
509
470
K (
510
471
' n' ,
511
472
' 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' }
514
475
)
515
476
516
477
-- VISUAL mode extended
0 commit comments