⚡ปลั๊กอินความคิดเห็นที่ชาญฉลาดและทรงพลังสำหรับ NeoVim ⚡

commentstring อ่าน :h comment.commentstring// ) และ block ( /* */ ) ความคิดเห็น. ) สนับสนุนซ้ำสำหรับ 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 ตั้งค่าการทำแผนที่พื้นฐานบางอย่างซึ่งสามารถใช้ในโหมดปกติและโหมดภาพเพื่อให้คุณเริ่มต้นด้วยความสุขในการแสดงความคิดเห็น
การแมปเหล่านี้เปิดใช้งานโดยค่าเริ่มต้น (config: 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 การแมปเหล่านี้เปิดใช้งานโดยค่าเริ่มต้น (config: 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) ปลั๊กอินนี้มีการรองรับ ต้นไม้ พื้นเมืองสำหรับการคำนวณ commentstring ที่ใช้งานได้หลายภาษา (ฉีด/ฝัง) หลายภาษาเช่น Vue หรือ Markdown แต่เนื่องจากลักษณะของต้นไม้ที่แยกวิเคราะห์การใช้งานนี้จึงมีข้อ จำกัด ที่ทราบกันดี
jsx/tsx การดำเนินการของมันค่อนข้างซับซ้อน สำหรับกรณีการใช้งานล่วงหน้าให้ใช้ nvim-ts-context-commentsstring ดูส่วน pre_hook สำหรับการรวม
หมายเหตุ -ปลั๊กอินนี้ไม่ได้ขึ้นอยู่กับ NVIM-Treesitter แต่ขอแนะนำเพื่อติดตั้งตัวแยกวิเคราะห์ผู้ใช้ต้นไม้
มีสองวิธีเบ็ดเช่น pre_hook และ post_hook ซึ่งเรียกว่าก่อนความคิดเห็นและหลังจากความคิดเห็นตามลำดับ ควรให้ทั้งสองอย่างระหว่าง setup()
pre_hook - เรียกด้วยอาร์กิวเมนต์ ctx (อ่าน :h comment.utils.CommentCtx ) ก่อน (UN) ความคิดเห็น สามารถเลือกส่งคืน commentstring ที่จะใช้สำหรับการแสดงความคิดเห็น (UN) {
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 ,
} นอกจากนี้คุณยังสามารถรวม NVIM-TS-context-commentstring โดยใช้ pre_hook เพื่อแสดงความคิดเห็นไฟล์ tsx/jsx ได้อย่างง่ายดาย
หมายเหตุ
Comment.nvimรองรับtreesitterนอกกรอบสำหรับทุกภาษายกเว้นtsx/jsx
{
pre_hook = require ( ' ts_context_commentstring.integrations.comment_nvim ' ). create_pre_hook (),
}post_hook - วิธีนี้เรียกว่าหลังจาก (UN) การแสดงความคิดเห็น มันได้รับ ctx เดียวกัน (อ่าน :h comment.utils.CommentCtx ) เป็น pre_hook {
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 ดูตัวอย่างที่นี่pre_hook ) และย้ายเคอร์เซอร์ไปยังบล็อกถัดไป (ใช้ post_hook ) ดูสิ่งนี้หมายเหตุ: เมื่อกด
gc,gbและเพื่อน,cmode(โหมดความคิดเห็น) ภายในpre_hookจะสลับเสมอเพราะเมื่อเรียกว่า pre-hook ในช่วงเวลานั้นเราไม่รู้ว่าgcหรือgbจะแสดงความคิดเห็นหรือไม่ทำให้รู้สึกถึงบรรทัด แต่โชคดีที่เรารู้เรื่องนี้ก่อนpost_hookและสิ่งนี้จะได้รับความคิดเห็นหรือสถานะไม่เกี่ยวกับการไม่ลงรอยกันเสมอ
คุณสามารถใช้ ignore ต่อการละเว้นบางบรรทัดระหว่างความคิดเห็น/ไม่สม่ำเสมอ สามารถใช้สตริง Lua Regex หรือฟังก์ชั่นที่ส่งคืนสตริง 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 ,
} ภาษา/ภาษาส่วนใหญ่มีการสนับสนุนแบบดั้งเดิมสำหรับความคิดเห็นผ่านทาง commentstring แต่อาจมีไฟล์ที่ไม่ได้รับการสนับสนุน มีสองวิธีในการเปิดใช้งานการแสดงความคิดเห็นสำหรับ FileTypes ที่ไม่ได้รับการสนับสนุน:
commentstring สำหรับ fileType นั้นได้เช่นต่อไปนี้ อ่าน :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 ' )สามารถเพิ่มความคิดเห็นเพิ่มเติมภายในปลั๊กอินได้
มีหลายวิธีในการมีส่วนร่วมในการรายงาน/แก้ไขข้อบกพร่องการร้องขอคุณสมบัติ นอกจากนี้คุณยังสามารถส่ง CommentsTring ไปยังปลั๊กอินนี้ได้โดยการอัปเดต Ft.lua และส่ง PR
/**%s*/ (js), ///%s (สนิม) ---- ------------------
-- This is a header --
---- ------------------