Skip to content

Commit

Permalink
Merge pull request #43 from shiradofu/feat/add-config-for-commentary-…
Browse files Browse the repository at this point in the history
…maps
  • Loading branch information
JoosepAlviste authored Aug 4, 2022
2 parents c3d6b00 + 31ac30f commit 7d0b001
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 12 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -237,13 +237,18 @@ If `vim-commentary` is detected, then this plugin automatically sets up
`vim-commentary` mappings to first update the `commentstring`, and then trigger
`vim-commentary`.

If you don't use default mappings, you can disable them:
You can override default mappings, or disable them by specifying `false`.

```lua
require'nvim-treesitter.configs'.setup {
context_commentstring = {
enable = true,
disable_commentary_mappings = true,
commentary_integration = {
-- change default mapping
Commentary = 'g/',
-- disable default mapping
CommentaryLine = false,
},
}
}
```
Expand Down
53 changes: 46 additions & 7 deletions lua/ts_context_commentstring/integrations/vim_commentary.lua
Original file line number Diff line number Diff line change
@@ -1,15 +1,54 @@
local map = vim.api.nvim_buf_set_keymap
local gmap = vim.api.nvim_set_keymap
local bmap = vim.api.nvim_buf_set_keymap

for _, mode in ipairs { 'n', 'x', 'o' } do
gmap(
mode,
'<Plug>ContextCommentary',
[[v:lua.context_commentstring.update_commentstring_and_run('Commentary')]],
{ expr = true }
)
end
gmap(
'n',
'<Plug>ContextCommentaryLine',
[[v:lua.context_commentstring.update_commentstring_and_run('CommentaryLine')]],
{ expr = true }
)
gmap(
'n',
'<Plug>ContextChangeCommentary',
[[v:lua.context_commentstring.update_commentstring_and_run('ChangeCommentary')]],
{ expr = true }
)

local M = {}

---Set up vim-commentary mappings to first update the commentstring, and then
---run vim-commentary
function M.set_up_maps()
map(0, 'n', 'gc', [[v:lua.context_commentstring.update_commentstring_and_run('Commentary')]], { expr = true })
map(0, 'x', 'gc', [[v:lua.context_commentstring.update_commentstring_and_run('Commentary')]], { expr = true })
map(0, 'o', 'gc', [[v:lua.context_commentstring.update_commentstring_and_run('Commentary')]], { expr = true })
map(0, 'n', 'gcc', [[v:lua.context_commentstring.update_commentstring_and_run('CommentaryLine')]], { expr = true })
map(0, 'n', 'cgc', [[v:lua.context_commentstring.update_commentstring_and_run('ChangeCommentary')]], { expr = true })
function M.set_up_maps(maps)
maps = (maps and type(maps) == 'table') and maps or {}
maps = vim.tbl_extend('force', {
Commentary = 'gc',
CommentaryLine = 'gcc',
ChangeCommentary = 'cgc',
CommentaryUndo = 'gcu',
}, maps)

if maps.Commentary then
for _, mode in ipairs { 'n', 'x', 'o' } do
bmap(0, mode, maps.Commentary, '<Plug>ContextCommentary', {})
end
end
if maps.CommentaryLine then
bmap(0, 'n', maps.CommentaryLine, '<Plug>ContextCommentaryLine', {})
end
if maps.ChangeCommentary then
bmap(0, 'n', maps.ChangeCommentary, '<Plug>ContextChangeCommentary', {})
end
if maps.CommentaryUndo then
bmap(0, 'n', maps.CommentaryUndo, '<Plug>ContextCommentary<Plug>Commentary', {})
end
end

return M
4 changes: 1 addition & 3 deletions lua/ts_context_commentstring/internal.lua
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,7 @@ function M.setup_buffer()
-- If vim-commentary is installed, set up mappings for it
if vim.g.loaded_commentary == 1 then
enable_autocmd = false
if not plugin_config.disable_commentary_mappings then
require('ts_context_commentstring.integrations.vim_commentary').set_up_maps()
end
require('ts_context_commentstring.integrations.vim_commentary').set_up_maps(plugin_config.commentary_integration)
end

if enable_autocmd then
Expand Down

0 comments on commit 7d0b001

Please sign in to comment.