Reputation: 16148
I have a lot of unittests in my code base, it would be nice if I could auto fold them.
The structure of unittests are
unittest{
//...
}
I have never written in vimscript before but I managed to get something that roughly works
function! UnittestFold()
let thisline = getline(v:lnum)
if match(thisline, '^unittest') >= 0
return ">1"
endif
if match(thisline, '^}') >= 0
return "0"
endif
return "="
endfunction
setlocal foldmethod=expr
setlocal foldexpr=UnittestFold()
There are a few issues with this code, the closing bracket }
of the unittest block is not inside the fold.
I also can not use zA
to open all folds and I have no idea why. No folds found
I think the problem is that I set the foldlevel to 0 for every closing bracket. Also I probably can not use {
or }
inside the unittest block.
How would I indicate that the fold should end after the last }
of unittest block?
Upvotes: 1
Views: 54
Reputation: 303
There are a few issues with this code, the closing bracket } of the unittest block is not inside the fold.
I can not test it right now, but changing the return "0"
statement to return "s1"
could help.
I also can not use zA to open all folds and I have no idea why.
When you enter :h zA
in Vim you see that zA
does not open all folds. To open all folds, press zR
(to close all zM
).
Also I probably can not use { or } inside the unittest block.
You should be able to use those brackets inside the unittest block. match(thisline, '^}') >= 0
is only true when a closing bracket appears at the very first column of a line - which is not the case if you use spaces. The hat (^
) in this statement is a regular expression meaning the beginning of a line of code.
The only thing that will not work with this code is if you have something other than unittest { ...}
at the same level. If you only use unittest for your file, you should be fine.
Upvotes: 1