It's not possible to have different foldmethod types in same buffer. How is Vim to know that there are some comments at same indent level as other text that you want to treat as being of a different (higher numbered) level?
I'm sure you can achieve what you want by setting foldmethod to 'expr'. This is most flexible way of doing folds in Vim but can get complicated (and/or slow) depending on what you want. I think it would work fairly easily for your use case, though.
First, somewhere in your vimrc or vimscripts you need to make sure that foldexpr is getting defined for the filetype in question.
set foldexpr=MyFoldLevel(v:lnum)
set foldmethod=expr
" and for last code example
let b:previous_level = 0
and you then have to flesh out your foldexpr function so that it assigns levels in a way that results in behavior you want. Something like the code below might come close to working in cases where each comment line has prefix symbol (i.e., not in your case), but I expect it needs some tweaks. h: fold-expr
would be a good place to look for help:
function! MyFoldLevel(linenum)
" assign levels based on spaces indented and tabstop of 4
let level = indent(a:linenum) / 4
if getline(a:linenum) =~ [put line-based comment prefix pattern here]
let level = 20
endif
endfunction
Would need to be modified to assign higher level for lines between comment start and end markers the way you want:
function! MyFoldLevel(linenum)
let linetext = getline(a:linenum)
if linetext =~ [put line-based comment prefix pattern here]
let level = 20
elseif linetext =~ '^\s*/\*'
let level = 20
elseif linetext =~ '^\s*\*/'
let level = 21
else
if b:previous_level == 20
let level = 20
else
"assuming code is space-indented with tabstop of 4
let level = indent(a:linenum) / 4
endif
endif
let b:previous_level = level
return level
endfunction
I don't expect the foldmethod functions I've written would work exactly as written. But they do point the way to something that would work.
Note that use of level of '20' for comments is just arbitrary level that allows them to be folded while all (presumably lower-leveled) indented code could be visible. '21' for last line of comment section is just to differentiate it from the previous lines of comments that have level of 20, to know that next line should be treated as regular line of code.
Also, key ops like 'zc' and 'zo' will not work quite right on comments when they're set to level much higher than surrounding code. Would want to use direct command like :set foldlevel=21
to show all comment lines.
Not pretty, and I expect it could be simplified a little, but something like this is what I think is required for what you want.
Actually, thinking through this a little more, I think you would want the first line of any comment blocks to be at same level as if it were a non-comment line, only subsequent comment lines in same block would need to be of higher level to have them "fold" into the starting comment line. In the code I gave, if it works or comes close to working at all, I think vim would fold all the comment lines behind the preceding non-comment line, which isn't what you want, but I unfortunately don't have more time to devote to this little puzzle. . . I've done this sort of custom-folding quite a few times and generally always have a little bit of trial and error in getting exactly what I want.