-
Notifications
You must be signed in to change notification settings - Fork 37
Integrations
For the native commenting feature and some commenting plugins, it's possible to trigger the commentstring calculation only when it is actually needed, i.e, only when commenting. It is recommended to use an integration as triggering the commentstring calculation on the CursorHold autocommand is not very efficient. Also, setting options inside the CursorHold autocommand can have odd side effects.
Integration with some plugins requires more configuration than others.
Let me know if you'd like to see more integrations for other commenting plugins. A PR is always appreciated :)
Neovim now has built-in support for commenting lines. First, disable the CursorHold autocommand of this plugin:
require('ts_context_commentstring').setup {
enable_autocmd = false,
}Then, anywhere in your config, override the Neovim internal get_option function which is called whenever the commentstring is requested:
local get_option = vim.filetype.get_option
vim.filetype.get_option = function(filetype, option)
return option == "commentstring"
and require("ts_context_commentstring.internal").calculate_commentstring()
or get_option(filetype, option)
endThese plugins allow configuring a hook function that is executed before commenting. For each of them, disabling the default autocmd is required:
require('ts_context_commentstring').setup {
enable_autocmd = false,
}After disabling the default autocmd, configure kommentary to trigger the commentstring updating logic with its hook_function configuration:
require('kommentary.config').configure_language('default', {
single_line_comment_string = 'auto',
multi_line_comment_strings = 'auto',
hook_function = function()
require('ts_context_commentstring').update_commentstring()
end,
})After disabling the default autocmd, configure nvim_comment to trigger the commentstring updating logic with its hook configuration:
require('nvim_comment').setup {
hook = function()
require('ts_context_commentstring').update_commentstring()
end,
}After disabling the default autocmd, configure Comment.nvim to trigger the commentstring updating logic
with its pre_hook configuration:
require('Comment').setup {
pre_hook = require('ts_context_commentstring.integrations.comment_nvim').create_pre_hook(),
}After disabling the default autocmd, configure mini.comment to trigger the commentstring updating logic by supplying custom_commentstring function:
require('mini.comment').setup {
options = {
custom_commentstring = function()
return require('ts_context_commentstring').calculate_commentstring() or vim.bo.commentstring
end,
},
}For these plugins, it is not necessary to set enable_autocmd = false as it is done automatically if the plugin is detected.
There is an existing integration with vim-commentary in this plugin.
If vim-commentary is detected, then this plugin automatically sets up
vim-commentary mappings to first update the commentstring, and then trigger
vim-commentary.
You can override default mappings, or disable them by specifying false.
require('nvim-treesitter.configs').setup {
context_commentstring = {
enable = true,
commentary_integration = {
-- change default mapping
Commentary = 'g/',
-- disable default mapping
CommentaryLine = false,
},
},
}Alternatively, update your mappings to include the Context prefix
vim.keymap.set(
'n',
'g/',
'<Plug>ContextCommentaryLine', -- Previously '<Plug>CommentaryLine'
{ silent = true, desc = 'Comment line' }
)