Skip to content

Commit

Permalink
change: use g@ with expr = true
Browse files Browse the repository at this point in the history
  • Loading branch information
numToStr committed Aug 13, 2022
1 parent 4818a4c commit 312b367
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 64 deletions.
73 changes: 21 additions & 52 deletions lua/Comment/api.lua
Expand Up @@ -394,16 +394,20 @@ api.locked = setmetatable({}, {
end,
})

---@private
---Callback function which does the following
--- 1. Sets operatorfunc for dot-repeat
--- 2. Preserves jumps and marks
--- 3. Stores last cursor position
---@param cb string Name of the API function to call
---@usage `require('Comment.api').call('toggle.linewise')`
function api.call(cb)
A.nvim_set_option('operatorfunc', ("v:lua.require'Comment.api'.locked'%s'"):format(cb))
Config.position = Config:get().sticky and A.nvim_win_get_cursor(0) or nil
Config.count = vim.v.count
function api.call(cb, op)
return function()
A.nvim_set_option('operatorfunc', ("v:lua.require'Comment.api'.locked'%s'"):format(cb))
Config.position = Config:get().sticky and A.nvim_win_get_cursor(0) or nil
Config.count = vim.v.count
return op
end
end

---@private
Expand All @@ -426,13 +430,13 @@ function api.setup(config)
'n',
cfg.toggler.line,
"v:count == 0 ? '<Plug>(comment_toggle_linewise_current)' : '<Plug>(comment_toggle_linewise_count)'",
{ expr = true, remap = true, replace_keycodes = false, desc = 'Comment toggle current line' }
{ expr = true, replace_keycodes = false, desc = 'Comment toggle current line' }
)
K(
'n',
cfg.toggler.block,
"v:count == 0 ? '<Plug>(comment_toggle_blockwise_current)' : '<Plug>(comment_toggle_blockwise_count)'",
{ expr = true, remap = true, replace_keycodes = false, desc = 'Comment toggle current block' }
{ expr = true, replace_keycodes = false, desc = 'Comment toggle current block' }
)

-- VISUAL mode mappings
Expand All @@ -452,65 +456,30 @@ function api.setup(config)

-- Extra Mappings
if cfg.mappings.extra then
K(
'n',
cfg.extra.below,
'<CMD>lua require("Comment.api").locked("insert.linewise.below")()<CR>',
{ desc = 'Comment insert below' }
)
K(
'n',
cfg.extra.above,
'<CMD>lua require("Comment.api").locked("insert.linewise.above")()<CR>',
{ desc = 'Comment insert above' }
)
K(
'n',
cfg.extra.eol,
'<CMD>lua require("Comment.api").locked("insert.linewise.eol")()<CR>',
{ desc = 'Comment insert end of line' }
)
K('n', cfg.extra.below, api.locked('insert.linewise.below'), { desc = 'Comment insert below' })
K('n', cfg.extra.above, api.locked('insert.linewise.above'), { desc = 'Comment insert above' })
K('n', cfg.extra.eol, api.locked('insert.linewise.eol'), { desc = 'Comment insert end of line' })
end

-- Extended Mappings
if cfg.mappings.extended then
-- NORMAL mode extended
K(
'n',
'g>',
'<CMD>lua require("Comment.api").call("comment.linewise")<CR>g@',
{ desc = 'Comment region linewise' }
)
K(
'n',
'g>c',
'<CMD>lua require("Comment.api").call("comment.linewise.current")<CR>g@$',
{ desc = 'Comment current line' }
)
K(
'n',
'g>b',
'<CMD>lua require("Comment.api").call("comment.blockwise.current")<CR>g@$',
{ desc = 'Comment current block' }
)
K('n', 'g>', api.call('comment.linewise', 'g@'), { expr = true, desc = 'Comment region linewise' })
K('n', 'g>c', api.call('comment.linewise.current', 'g@$'), { expr = true, desc = 'Comment current line' })
K('n', 'g>b', api.call('comment.blockwise.current', 'g@$'), { expr = true, desc = 'Comment current block' })

K(
'n',
'g<',
'<CMD>lua require("Comment.api").call("uncomment.linewise")<CR>g@',
{ desc = 'Uncomment region linewise' }
)
K('n', 'g<', api.call('uncomment.linewise', 'g@'), { expr = true, desc = 'Uncomment region linewise' })
K(
'n',
'g<c',
'<CMD>lua require("Comment.api").call("uncomment.linewise.current")<CR>g@$',
{ desc = 'Uncomment current line' }
api.call('uncomment.linewise.current', 'g@$'),
{ expr = true, desc = 'Uncomment current line' }
)
K(
'n',
'g<b',
'<CMD>lua require("Comment.api").call("uncomment.blockwise.current")<CR>g@$',
{ desc = 'Uncomment current block' }
api.call('uncomment.blockwise.current', 'g@$'),
{ expr = true, desc = 'Uncomment current block' }
)

-- VISUAL mode extended
Expand Down
25 changes: 13 additions & 12 deletions plugin/Comment.lua
@@ -1,45 +1,46 @@
local K = vim.keymap.set
local call = require('Comment.api').call

-- Operator-Pending mappings
K(
'n',
'<Plug>(comment_toggle_linewise)',
'<CMD>lua require("Comment.api").call("toggle.linewise")<CR>g@',
{ desc = 'Comment toggle linewise' }
call('toggle.linewise', 'g@'),
{ expr = true, desc = 'Comment toggle linewise' }
)
K(
'n',
'<Plug>(comment_toggle_blockwise)',
'<CMD>lua require("Comment.api").call("toggle.blockwise")<CR>g@',
{ desc = 'Comment toggle blockwise' }
call('toggle.blockwise', 'g@'),
{ expr = true, desc = 'Comment toggle blockwise' }
)

-- Toggle mappings
K(
'n',
'<Plug>(comment_toggle_linewise_current)',
'<CMD>lua require("Comment.api").call("toggle.linewise.current")<CR>g@$',
{ desc = 'Comment toggle current line' }
call('toggle.linewise.current', 'g@$'),
{ expr = true, desc = 'Comment toggle current line' }
)
K(
'n',
'<Plug>(comment_toggle_blockwise_current)',
'<CMD>lua require("Comment.api").call("toggle.blockwise.current")<CR>g@$',
{ desc = 'Comment toggle current block' }
call('toggle.blockwise.current', 'g@$'),
{ expr = true, desc = 'Comment toggle current block' }
)

-- Count mappings
K(
'n',
'<Plug>(comment_toggle_linewise_count)',
'<CMD>lua require("Comment.api").call("toggle.linewise.count_repeat")<CR>g@$',
{ desc = 'Comment toggle linewise with count' }
call('toggle.linewise.count_repeat', 'g@$'),
{ expr = true, desc = 'Comment toggle linewise with count' }
)
K(
'n',
'<Plug>(comment_toggle_blockwise_count)',
'<CMD>lua require("Comment.api").call("toggle.blockwise.count_repeat")<CR>g@$',
{ desc = 'Comment toggle blockwise with count' }
call('toggle.blockwise.count_repeat', 'g@$'),
{ expr = true, desc = 'Comment toggle blockwise with count' }
)

-- Visual-Mode mappings
Expand Down

0 comments on commit 312b367

Please sign in to comment.