Neovim的智能和强大的评论插件⚡

commentstring 。阅读:h comment.commentstring// )和块( /* */ )注释. )重复支持gcc , gbc和朋友[count]gcc和[count]gbcgcw gc$ )和上下( gc2j gc4k )运动gci{ gbat ) -- add this to your lua/plugins.lua, lua/plugins/init.lua, or the file you keep your other plugins:
{
' numToStr/Comment.nvim ' ,
opts = {
-- add any options here
}
}
use {
' numToStr/Comment.nvim ' ,
config = function ()
require ( ' Comment ' ). setup ()
end
}Plug ' numToStr/Comment.nvim '
" Somewhere after plug#end()
lua require ( ' Comment ' ). setup ()Comment.nvim提供可以通过运行访问的帮助文档:help comment-nvim
首先,您需要调用setup()方法来创建默认映射。
注意- 如果您面对钥匙凸的映射,但它们没有解决问题,请尝试一下
require ( ' Comment ' ). setup () lua << EOF
require ( ' Comment ' ). setup ()
EOF以下是setup()的默认配置。如果要覆盖,只需修改所需的选项,然后将其与默认配置合并。阅读:h comment.config以获取更多信息。
{
--- Add a space b/w comment and the line
padding = true ,
--- Whether the cursor should stay at its position
sticky = true ,
--- Lines to be ignored while (un)comment
ignore = nil ,
--- LHS of toggle mappings in NORMAL mode
toggler = {
--- Line-comment toggle keymap
line = ' gcc ' ,
--- Block-comment toggle keymap
block = ' gbc ' ,
},
--- LHS of operator-pending mappings in NORMAL and VISUAL mode
opleader = {
--- Line-comment keymap
line = ' gc ' ,
--- Block-comment keymap
block = ' gb ' ,
},
--- LHS of extra mappings
extra = {
--- Add comment on the line above
above = ' gcO ' ,
--- Add comment on the line below
below = ' gco ' ,
--- Add comment at the end of line
eol = ' gcA ' ,
},
--- Enable keybindings
--- NOTE: If given `false` then the plugin won't create any mappings
mappings = {
--- Operator-pending mapping; `gcc` `gbc` `gc[count]{motion}` `gb[count]{motion}`
basic = true ,
--- Extra mapping; `gco`, `gcO`, `gcA`
extra = true ,
},
--- Function to call before (un)comment
pre_hook = nil ,
--- Function to call after (un)comment
post_hook = nil ,
}当您调用setup()方法时, Comment.nvim设置了一些基本映射,这些映射可在正常和视觉模式下使用,以使您开始发表评论内容的乐趣。
默认情况下启用了这些映射。 (配置: mappings.basic )
`gcc` - Toggles the current line using linewise comment
`gbc` - Toggles the current line using blockwise comment
`[ count ]gcc` - Toggles the number of line given as a prefix-count using linewise
`[ count ]gbc` - Toggles the number of line given as a prefix-count using blockwise
`gc[ count ]{motion}` - (Op-pending) Toggles the region using linewise comment
`gb[ count ]{motion}` - (Op-pending) Toggles the region using blockwise comment `gc` - Toggles the region using linewise comment
`gb` - Toggles the region using blockwise comment默认情况下启用了这些映射。 (配置: mappings.extra )
`gco` - Insert comment to the next line and enters INSERT mode
`gcO` - Insert comment to the previous line and enters INSERT mode
`gcA` - Insert comment to end of the current line and enters INSERT mode# Linewise
`gcw` - Toggle from the current cursor position to the next word
`gc $ ` - Toggle from the current cursor position to the end of line
`gc}` - Toggle until the next blank line
`gc5j` - Toggle 5 lines after the current cursor position
`gc8k` - Toggle 8 lines before the current cursor position
`gcip` - Toggle inside of paragraph
`gca}` - Toggle around curly brackets
# Blockwise
`gb2}` - Toggle until the 2 next blank line
`gbaf` - Toggle comment around a function (w/ LSP/treesitter support)
`gbac` - Toggle comment around a class (w/ LSP/treesitter support)该插件具有本机treeTitter支持计算评论的本机,该commentstring正在用于多种(注射/嵌入式)语言(如Vue或Markdown)。但是由于解析树的性质,该实现具有一些已知的局限性。
jsx/tsx支持。它的实施非常复杂。对于提前用例,请使用nvim-ts-context-commentstring。有关集成,请参见pre_hook部分。
注意- 此插件不取决于nvim-treesitter,但是建议您轻松安装树木 - sitter解析器。
有两种挂钩方法,即pre_hook和post_hook ,分别在评论和评论之后被调用。两者都应在setup()期间提供。
pre_hook在(un)注释之前,请使用ctx参数(读:h comment.utils.CommentCtx )。可以选择返回用于(联合)评论的commentstring 。 {
pre_hook = function ( ctx )
if ctx . range . srow == ctx . range . erow then
-- do something with the current line
else
-- do something with lines range
end
end ,
}您还可以使用pre_hook集成NVIM-TS-context-commentstring,以轻松评论tsx/jsx文件。
注意-
Comment.nvim已经支持除tsx/jsx以外的所有语言的treesitter即用。
{
pre_hook = require ( ' ts_context_commentstring.integrations.comment_nvim ' ). create_pre_hook (),
}post_hook此方法在(联合)评论后调用。它接收到与pre_hook相同的ctx (读:h comment.utils.CommentCtx )。 {
post_hook = function ( ctx )
if ctx . range . srow == ctx . range . erow then
-- do something with the current line
else
-- do something with lines range
end
end ,
}可以实施post_hook以涵盖一些利基用例,如以下内容:
#if 0在C中评论代码。在这里查看一个示例。pre_hook )并将光标移至下一个块(使用post_hook )。看到这个。注意:按下
gc,gb和Friends时,pre_hook内部的cmode(注释模式)将始终切换,因为当调用Pre-Hook时,在那一刻,我们不知道gc还是gb是否会评论或不调查行。但幸运的是,我们确实在post_hook之前就知道这一点,这将始终会收到评论或不可食用状态
您可以ignore在评论/不注释期间忽略某些行。它可以占用LUA REGEX字符串或返回正则弦字符串的函数,应在setup()期间提供。
注意:仅在linewise评论时忽略工作。这是设计。因为忽略块注释中的行是没有多大意义的。
string -- ignores empty lines
ignore = ' ^$ '
-- ignores line that starts with `local` (excluding any leading whitespace)
ignore = ' ^(%s*)local '
-- ignores any lines similar to arrow function
ignore = ' ^const(.*)=(%s?)%((.*)%)(%s?)=> 'function {
ignore = function ()
-- Only ignore empty lines for lua files
if vim . bo . filetype == ' lua ' then
return ' ^$ '
end
end ,
}大多数语言/filetypes都通过commentstring对注释的本地支持,但可能不支持FILETYPE。有两种方法可以评论不受支持的Filetypes:
commentstring 。阅读:h commentstring以获取更多信息。 vim . bo . commentstring = ' //%s '
-- or
vim . api . nvim_command ( ' set commentstring=//%s ' )commentstring的更强大版本。阅读:h comment.ft以获取更多信息。 local ft = require ( ' Comment.ft ' )
-- 1. Using set function
ft
-- Set only line comment
. set ( ' yaml ' , ' #%s ' )
-- Or set both line and block commentstring
. set ( ' javascript ' , { ' //%s ' , ' /*%s*/ ' })
-- 2. Metatable magic
ft . javascript = { ' //%s ' , ' /*%s*/ ' }
ft . yaml = ' #%s '
-- Multiple filetypes
ft ({ ' go ' , ' rust ' }, ft . get ( ' c ' ))
ft ({ ' toml ' , ' graphql ' }, ' #%s ' )欢迎PR(S)在插件中添加更多评论
有多种方法可以贡献报告/修复错误,功能请求。您还可以通过更新ft.lua和发送PR提交该插件的评论。
/**%s*/ (js), ///%s (rust) ---- ------------------
-- This is a header --
---- ------------------