diff --git a/lua/Comment/api.lua b/lua/Comment/api.lua index a99b5615..b60c30c5 100644 --- a/lua/Comment/api.lua +++ b/lua/Comment/api.lua @@ -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, '(comment_toggle_linewise)', { desc = 'Comment toggle linewise' }) - K('n', cfg.opleader.block, '(comment_toggle_blockwise)', { desc = 'Comment toggle blockwise' }) - - K('n', cfg.toggler.line, function() - return A.nvim_get_vvar('count') == 0 and '(comment_toggle_linewise_current)' - or '(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 '(comment_toggle_blockwise_current)' - or '(comment_toggle_blockwise_count)' - end, { expr = true, desc = 'Comment toggle current block' }) - - -- VISUAL mode mappings - K( - 'x', - cfg.opleader.line, - '(comment_toggle_linewise_visual)', - { desc = 'Comment toggle linewise (visual)' } - ) - K( - 'x', - cfg.opleader.block, - '(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', - 'lua require("Comment.api").locked("comment.linewise")(vim.fn.visualmode())', - { desc = 'Comment region linewise (visual)' } - ) - K( - 'x', - 'g<', - 'lua require("Comment.api").locked("uncomment.linewise")(vim.fn.visualmode())', - { desc = 'Uncomment region linewise (visual)' } - ) - end - end - - return cfg -end - return api diff --git a/lua/Comment/init.lua b/lua/Comment/init.lua index 084bb835..42b7c54d 100644 --- a/lua/Comment/init.lua +++ b/lua/Comment/init.lua @@ -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 @@ -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, '(comment_toggle_linewise)', { desc = 'Comment toggle linewise' }) + K('n', cfg.opleader.block, '(comment_toggle_blockwise)', { desc = 'Comment toggle blockwise' }) + + K('n', cfg.toggler.line, function() + return vvar('count') == 0 and '(comment_toggle_linewise_current)' + or '(comment_toggle_linewise_count)' + end, { expr = true, desc = 'Comment toggle current line' }) + K('n', cfg.toggler.block, function() + return vvar('count') == 0 and '(comment_toggle_blockwise_current)' + or '(comment_toggle_blockwise_count)' + end, { expr = true, desc = 'Comment toggle current block' }) + + -- VISUAL mode mappings + K( + 'x', + cfg.opleader.line, + '(comment_toggle_linewise_visual)', + { desc = 'Comment toggle linewise (visual)' } + ) + K( + 'x', + cfg.opleader.block, + '(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