Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 0 additions & 89 deletions lua/Comment/api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -246,93 +246,4 @@ function api.call(cb, op)
end
end

---@private
---Configures the plugin
---@param config? CommentConfig
---@return CommentConfig
function api.setup(config)
local cfg = Config:set(config):get()

if cfg.mappings then
local K = vim.keymap.set

-- Basic Mappings
if cfg.mappings.basic then
-- NORMAL mode mappings
K('n', cfg.opleader.line, '<Plug>(comment_toggle_linewise)', { desc = 'Comment toggle linewise' })
K('n', cfg.opleader.block, '<Plug>(comment_toggle_blockwise)', { desc = 'Comment toggle blockwise' })

K('n', cfg.toggler.line, function()
return A.nvim_get_vvar('count') == 0 and '<Plug>(comment_toggle_linewise_current)'
or '<Plug>(comment_toggle_linewise_count)'
end, { expr = true, desc = 'Comment toggle current line' })
K('n', cfg.toggler.block, function()
return A.nvim_get_vvar('count') == 0 and '<Plug>(comment_toggle_blockwise_current)'
or '<Plug>(comment_toggle_blockwise_count)'
end, { expr = true, desc = 'Comment toggle current block' })

-- VISUAL mode mappings
K(
'x',
cfg.opleader.line,
'<Plug>(comment_toggle_linewise_visual)',
{ desc = 'Comment toggle linewise (visual)' }
)
K(
'x',
cfg.opleader.block,
'<Plug>(comment_toggle_blockwise_visual)',
{ desc = 'Comment toggle blockwise (visual)' }
)
end

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

if cfg.mappings.extended then
vim.notify_once(
[=[[Comment] `extended` mappings are deprecated and will be removed on 07 Nov 2022. Please refer to https://github.com/numToStr/Comment.nvim/wiki/Extended-Keybindings on how to define them manually.]=],
vim.log.levels.WARN
)

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<', api.call('uncomment.linewise', 'g@'), { expr = true, desc = 'Uncomment region linewise' })
K(
'n',
'g<c',
api.call('uncomment.linewise.current', 'g@$'),
{ expr = true, desc = 'Uncomment current line' }
)
K(
'n',
'g<b',
api.call('uncomment.blockwise.current', 'g@$'),
{ expr = true, desc = 'Uncomment current block' }
)

K(
'x',
'g>',
'<ESC><CMD>lua require("Comment.api").locked("comment.linewise")(vim.fn.visualmode())<CR>',
{ desc = 'Comment region linewise (visual)' }
)
K(
'x',
'g<',
'<ESC><CMD>lua require("Comment.api").locked("uncomment.linewise")(vim.fn.visualmode())<CR>',
{ desc = 'Uncomment region linewise (visual)' }
)
end
end

return cfg
end

return api
50 changes: 48 additions & 2 deletions lua/Comment/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ local C = {}

---Configures the plugin
---@param config? CommentConfig User configuration
---@return CommentConfig #Returns the mutated config
---@return CommentConfig #Returns the modified config
---@see comment.config
---@usage [[
----- Use default configuration
Expand All @@ -87,7 +87,53 @@ local C = {}
---})
---@usage ]]
function C.setup(config)
return require('Comment.api').setup(config)
---@diagnostic disable-next-line: invisible
local cfg = require('Comment.config'):set(config):get()

if cfg.mappings then
local api = require('Comment.api')
local vvar = vim.api.nvim_get_vvar
local K = vim.keymap.set

-- Basic Mappings
if cfg.mappings.basic then
-- NORMAL mode mappings
K('n', cfg.opleader.line, '<Plug>(comment_toggle_linewise)', { desc = 'Comment toggle linewise' })
K('n', cfg.opleader.block, '<Plug>(comment_toggle_blockwise)', { desc = 'Comment toggle blockwise' })

K('n', cfg.toggler.line, function()
return vvar('count') == 0 and '<Plug>(comment_toggle_linewise_current)'
or '<Plug>(comment_toggle_linewise_count)'
end, { expr = true, desc = 'Comment toggle current line' })
K('n', cfg.toggler.block, function()
return vvar('count') == 0 and '<Plug>(comment_toggle_blockwise_current)'
or '<Plug>(comment_toggle_blockwise_count)'
end, { expr = true, desc = 'Comment toggle current block' })

-- VISUAL mode mappings
K(
'x',
cfg.opleader.line,
'<Plug>(comment_toggle_linewise_visual)',
{ desc = 'Comment toggle linewise (visual)' }
)
K(
'x',
cfg.opleader.block,
'<Plug>(comment_toggle_blockwise_visual)',
{ desc = 'Comment toggle blockwise (visual)' }
)
end

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

return cfg
end

return C