Contents

The fold-method marker can be used to set folds in Java source. Define a marker and place it inside HTML comments <!-- xx -->. This way, it does not affect the Javadocs generated without the necessity of a seprate comment line.

By default, the officially distributed Java syntax file (as of Vim 7.3.462) provides folding for all {...} blocks. However, this may not be sufficient for some users, who may wish to fold comments and javadoc, or who may wish to see the function name or enumeration folded with the block. This tip provides a syntax file for Java to fold methods, constructors, enums, and comments in Java code in a more appealing way. Note, however, that it depends on good indentation style to work. If your file is poorly indented, the folding will not be correct.

The following will fold all methods in a Java program. The enum syn region is not perfect, but it's workable. Annotations are included in the fold, which cleans up the fold display. It also folds C-style comment blocks, including javadoc. Single line functions, like public int getFoo() { return 4; } are not folded.

Place the following script in file ~/.vim/after/syntax/java.vim on Unix-based systems, or in file $HOME/vimfiles/after/syntax/java.vim on Windows systems.

This is great, but I had a lot of problems with it (brace-folds kept folding to the end of the class etc). It seems that my settings for java highlighting (e.g. let java_highlight_functions="style") have a lot to do with it. So I did some changes.

I cannot really explain exactly why the following works, but it does for me:

This works (for braces) because the "keepend" makes sure that any contained items end whenever the braces region ends, whether or not the contained item already ended. The "extend" allows THIS GROUP to override the keepend setting for a container group, so that you can nest this group. For this reason, I doubt very much that you really want to add "extend" to the foldJavadoc group. If you do, javadoc comments become nestable for folding!

I also added folding for import statements. This could probably be a lot cleaner; still, here goes:

Basically, until they update the syntax/java.vim, putting the above in your after/syntax/java.vim should fix things. (You'll have to set fdm=syntax in your ftplugin, of course.)

Using 7.2.79 here, and this doesn't appear to be necessary anymore

However, if you're not using Vim 7 yet, my original solution with expression should yield the same results.

Works like a charm!

This is a nice tip for cleaning up Java source. Thanks for posting it! I haven't checked the 7.0 behavior, and I'm betting it's handled cleanly, but I had to make one change for 6.x. We'll all encounter code like this at some point:

try {
//stuff
} catch (Exception e) {
//stuff
}

To fix it, I tweaked the fold-excluding regular expression to check for close-stuff-open braces on the same line. The behavior actually makes sense, too, since you should only see that brace style in compound control structures. The fold applies to the outside pair (the if, try, etc.) and it turns out pretty clean. Here's the new line I'm using:

if ( thisLine =~ '\%(\%(/\*\*\).*\%(\*/\)\)\|\%({.*}\)\|\%(}.*{\)' )

I'm using Ubuntu 9.04 here with Vim 7.2.079. This folding problem still exists, but the solution of editing the syntax file works fine. Just put the below code in at the end of your syntax/java.vim. (You'll have to set fdm=syntax in your ftplugin, of course.)

This is another fold function which enables folding in any C/java/actionscript/php file or XML/HTML.
Also when its mixed (for example an php file containing javascript and php).
I made it because the syntax & marker foldingmethods did not fit my needs.
For example, if I have a big function with a big function comment, I want to fold both with one action.
This script makes it possible, because its a bit more bruteforce.