preservim / nerdcommenter

Vim plugin for intensely nerdy commenting powers
Creative Commons Zero v1.0 Universal
4.97k stars 447 forks source link

[Feature Request] Un/Folding comments #499

Open tkapias opened 1 year ago

tkapias commented 1 year ago

Apparently, there is no plugin that supports comments folding for major filetypes. Those that I found are deprecated, or really limited. Most of the plugins dedicated to syntax and filetypes don't offer it either.

I currently use custom fold expressions for Unix-like comments, like the one found here:

set fdm=expr
set fde=getline(v:lnum)=~'^\\s#'?1:getline(prevnonblank(v:lnum))=~'^\\s#'?1:getline(nextnonblank(v:lnum))=~'^\\s*#'?1:0

Would nerdcommenter be a good place to add such a functionality?

tkapias commented 1 year ago

For now I use this setup:

autocmd FileType sh,toml,yaml,python noremap <buffer><silent> <Leader>fc :set foldmethod=expr<CR>
    \:set foldexpr=getline(v:lnum)=~'^\\s#'?1
    \:getline(prevnonblank(v:lnum))=~'^\\s#'?1
    \:getline(nextnonblank(v:lnum))=~'^\\s*#'?1
    \:0<CR>
autocmd FileType vim noremap <buffer><silent> <Leader>fc :set foldmethod=expr<CR>
    \:set foldexpr=getline(v:lnum)=~'^\\s\"'?1
    \:getline(prevnonblank(v:lnum))=~'^\\s\"'?1
    \:getline(nextnonblank(v:lnum))=~'^\\s*\"'?1
    \:0<CR>
alerque commented 1 year ago

I would help facilitate a PR if somebody wanted to contribute this as a new feature. I would suggest in needs to be behind a default-off feature flag so as to not mess up other fold expression enabled plugins, and we need some way to derive the expression to use for all block/multiline comments. Preferably we wouldn't be matching inline or even single line comments.