A Neovim plugin for folding comments using treesitter detection, while preserving LSP folding functionality.
- Fold all multi-line comments in the current buffer with a single command
- High performance with optimized treesitter queries and binary search
- Preserves LSP folding for code structures
- Hybrid foldexpr approach combines comment folds with LSP folds
- Filters out inline comments (code + comment on same line)
- Configurable merging of consecutive comment blocks
- Configurable fallback foldexpr for emergency reset
- Automatic cleanup when saving sessions (works with persistence.nvim)
- Supports any language with treesitter parser
- Neovim >= 0.9.0
- Treesitter parser installed for your target language(s)
Using lazy.nvim
{
"Raaghav-Arya/no-comments-please.nvim",
opts = {},
keys = {
{ "zh", "<cmd>CommentFoldToggle<cr>", desc = "Toggle comment folding" },
},
}Using packer.nvim
use {
"Raaghav-Arya/no-comments-please.nvim",
config = function()
require("no-comments-please").setup()
end
}Using vim-plug
Plug 'Raaghav-Arya/no-comments-please.nvim'Then in your init.lua:
require("no-comments-please").setup():CommentFold- Fold all multi-line comments in the current buffer:CommentUnfold- Unfold all comments and restore original foldexpr:CommentFoldToggle- Toggle comment folding
If using the lazy.nvim config above:
- Press
zhto toggle comment folding - Press
<leader>zlto restore the default foldexpr (fallback reset)
The plugin can be configured via the setup() function:
require("no-comments-please").setup({
-- Merge consecutive comment blocks into a single fold (default: true)
merge_consecutive = true,
-- Include blank lines between comments when merging (default: false)
-- When true, comments separated only by blank lines will be merged
include_blank_after = false,
-- Default foldexpr to use for fallback reset (default: LSP foldexpr)
default_foldexpr = "v:lua.vim.lsp.foldexpr()",
-- Keybindings configuration
keybinds = {
-- Keybind to restore default foldexpr (default: "<leader>zl")
-- Set to nil or false to disable
restore_foldexpr = "<leader>zl",
},
}){
"Raaghav-Arya/no-comments-please.nvim",
opts = {
merge_consecutive = true,
include_blank_after = false,
default_foldexpr = "v:lua.vim.lsp.foldexpr()",
keybinds = {
restore_foldexpr = "<leader>zl",
},
},
}{
"Raaghav-Arya/no-comments-please.nvim",
opts = {
merge_consecutive = true,
include_blank_after = true, -- Merge comments separated by blank lines
},
}{
"Raaghav-Arya/no-comments-please.nvim",
opts = {
merge_consecutive = false, -- Each comment block gets its own fold
},
}{
"Raaghav-Arya/no-comments-please.nvim",
opts = {
-- Use treesitter foldexpr as fallback instead of LSP
default_foldexpr = "v:lua.vim.treesitter.foldexpr()",
},
}{
"Raaghav-Arya/no-comments-please.nvim",
opts = {
keybinds = {
restore_foldexpr = "<leader>fu", -- Use custom keybind
-- Or set to nil/false to disable: restore_foldexpr = nil
},
},
}- Treesitter Detection: Uses treesitter to identify all comments in the buffer
- Filtering: Removes single-line comments and inline comments
- Hybrid Foldexpr: Creates a custom foldexpr that:
- Routes comment lines to custom fold logic
- Routes code lines to the original LSP foldexpr
- Session Cleanup: Automatically restores original foldexpr when saving sessions (persistence.nvim)
- Works with any LSP-based folding setup
- Compatible with persistence.nvim for session management
- Supports all languages with treesitter parsers
MIT License - see LICENSE file for details