Saturday, June 13, 2015

Eclipse Mars - how to switch back to previous Java formatter?

Update: the Luna formatter plugin now lives on GitHub, thanks to Asier Lostalé!

Java code formatter in Eclipse 4.5 has been completely rewritten. There's a lot less of bugs, the behavior is more consistent, and line wrapping is a bit smarter. It also opens way to easier implementation of new improvements in the future.
While most users will probably be happy with the new formatter, for some the changes may be unwelcome. Probably the most controversial change is a more restrictive approach to the "Never join already wrapped lines" option - a lot of line breaks that used to be tolerated by the old formatter, will be now removed if they don't fit the line wrapping settings. Also, some teams just don't want to force everyone to immediately switch to the newest Eclipse, so during the transition it would be problematic if part of the team used different formatter.
If you also find that problems related to changed formatter behavior outweigh the benefits of bug fixes and improvements, you'll be glad to hear that Eclipse Mars has a new extension point for Java formatter. So it's easy to take the code of the old formatter and wrap it in a plugin. I did just that for your convenience, the plugin can be downloaded here. Although I tested it a bit, the plugin is provided "as is" and I take no responsibility for anything that happens because of it. Just unzip the provided jar into the "plugins" directory of your Eclipse installation, restart, and in the Preferences -> Code Style -> Formatter page select Formatter implementation: Old formatter. Happy formatting!

69 comments:

DerEineDa said...

Thank you, this works nicely! I almost had to revert to Eclipse Luna.

Anonymous said...

Hi,
seems it doesn't work on properties, ex :

public interface MyInterface
{
String PROP1 = "prop1";
String PROP2 = "prop2";
}
It doesn't keep old formatting.
+

Anonymous said...

I've placed the attached jar into my plugins directory but don't see the indicated option to switch to the old format... where should I be looking?

Anonymous said...

Disregard the previous comment. I had an old folder from a previous install... silly me.

Seems to work with the exception that I also had to manually change the "tab size" setting from 4 to 2 to make it behave as before.

Anonymous said...

Echoing DerEinDa. Thanks so much. I wanted to use Mars at work and I was going to have to switch back to Luna until a coworker found this post.

lpalli said...

Thanks for the workaround (plugin), my coworkers don't have all switched to Eclipse Mars and this change was a big problem to allow Eclipse Mars and Eclipse Luna coexistence.

Anonymous said...

How do i do that with Eclipse 4.3 Kepler?

Simon Stratmann said...

Oh my god, you are a godsend. It took me forever to find out what exactly was going on and then to find your plugin. It works flawlessly. No more complaints from my colleagues that my code style is whacky.

Anonymous said...

Genius! Thanx a lot!

User Anonymous said...

I've extracted the content of jar file into {eclipse-home}/plugins directory and restarted my eclipse. But still the same issue.

On my previous version (Luna) I've created my formatter XML which I'm using in Mars but it is behaving differently.

Anonymous said...

it is not working.

Oliver Milke said...

Awesome!

Works like charm.

It just seems there are still minor differences, which I cannot explain. Anyway, this will enable me to use Mars.

Thank you :)

TG said...

Not working in OS X 10.10.5 :(
I can't see the Formatter implementation select option.

Unknown said...

One important step if you're using the "Eclipse [built-in]" profile, is that the profile itself also differs from Luna. So the plugin isn't quite enough in that case. I just opened my Luna and created a "Eclipse [built-in] Luna" profile which I cloned from the built-in profile. Then I just exported it to Mars. If you already have a custom profile, you're probably exporting anyway, but if you're just using the default, keep this in mind.

My Mars profile is now working exactly like my coworkers' Luna profiles. Thanks for the plugin!

Anonymous said...

Your suggestion is not working for me.
first of all the code style is under preferences--> Java-->code Style-->Formatter

within that, I have created new profile using Eclipse built in and checked "Never join already wrapped lines" and select Old formatter for "Formatter implementation". applied the change and restarted Eclipse. Still same problem persisting.

Unknown said...
This comment has been removed by a blog administrator.
Anonymous said...
This comment has been removed by a blog administrator.
Anonymous said...
This comment has been removed by a blog administrator.
Anonymous said...
This comment has been removed by a blog administrator.
Anonymous said...
This comment has been removed by a blog administrator.
Anonymous said...
This comment has been removed by a blog administrator.
Anonymous said...
This comment has been removed by a blog administrator.
Anonymous said...
This comment has been removed by a blog administrator.
Anonymous said...
This comment has been removed by a blog administrator.
Anonymous said...
This comment has been removed by a blog administrator.
Anonymous said...
This comment has been removed by a blog administrator.
Anonymous said...

Does not work in Eclipse Neon (4.6)

Anonymous said...
This comment has been removed by a blog administrator.
Unknown said...
This comment has been removed by a blog administrator.
hira khan said...
This comment has been removed by a blog administrator.
docstrange said...

Hello,

thx for your plugin! Works like a charm.

Is it possible for you,to create a update site out of the plugin? We distribute eclipse through a tool called SDC and this needs a updatesite, so that we can add the plugin.

regards

DMM Jhon said...
This comment has been removed by a blog administrator.
Anonymous said...

Wow this is really great - saved my week! ("saved my day" would be not honour enough for your great solution!) :-)

Anonymous said...
This comment has been removed by a blog administrator.
Unknown said...
This comment has been removed by a blog administrator.
Unknown said...
This comment has been removed by a blog administrator.
Unknown said...
This comment has been removed by a blog administrator.
Anonymous said...

Unfortunately this doesn't work with Neo anymore :-(

Anonymous said...

First of all, thanks a lot for providing the old formatter!
Unfortunately though, it doesn't seem to work with Eclipse 4.6 (Neon).
Any chance you provide a plugin working with neon? Or some pointers how to wrap the old formatter as a 'neon' plugin?

Thanks a lot!

Mateusz Matela said...

Hey, I've just updated the bundle to make it work with Eclipse Neon - download it again and you should see the jar with version 1.1.0.
Don't forget to remove the old jar if you have it in plugins folder.
If there's any problem, try running Eclipse in clean mode.

Anonymous said...

The new version 1.1.0 works like a charm with Eclipse Neon. Thanks a lot for providing this!

Unknown said...
This comment has been removed by the author.
Anonymous said...

Hey, I am using Neon, but can't seem to find the tick-box to select the old formatter...

Anonymous said...

Hey, I have the same issue. Can't find where to select the old formatter.
I am using Eclipse Neon 64-bit. Will the old formatter word for both 32 and 64 bit?
I am using OldJavaFormatter_1.1.0.201606242022.jar. Thanks for your effort. Much appreciated..

Anonymous said...

Same issue here. Selection to old formatter not available.... Also using Eclipse Neon 64bit.
Using a modified profile cloned from K&R. Would love to use this, if I can only get it selected!!

Anonymous said...

Mateusz,
Is it maybe possible to look why the selection for the old formatter does not disply?
I am using a cloned & modified formatter profile with Eclipse Neon.

Omit said...

I am using

Version: Neon Release (4.6.0)
Build id: 20160613-1800
OldJavaFormatter_1.1.0.201606242022.jar

and can select it under
Preferences -> Java -> Code Style -> Formatter
There is a dropdown in the middle ...

Anonymous said...

Hi Omit,
I have the same Eclipse Build as you.

I have copied OldJavaFormatter_1.1.0.201606242022.jar to c:\eclipse\plugins

When I navigate to Window->preferences, I see the following options:
General, C/C++, Changelog, Help, Install/Update, Library Hover, Mylyn, Oomph, Remote Development, Remote Systems, RPM, Run/Debug, Team, Terminal, Tracing

There is no Java selection...

Code Style is under Window->preferences->C/C++->Code Style->Formatter but there is nothing to select...

I am using Java 8 Update 31. I have the same issue on 2 different PC's....
I am not sure is it's an installation issue????

Any help would be greatly appreciated.

Anonymous said...

Try placing the jar file in the 'dropins' directory instead of the 'plugins' directory. This is what I had to do.

Eclise Neon with OldJavaFormatter 1.1.0

Anonymous said...

Hi,
I tried that - did not seem to make any difference. I am also not sure if I am looking at the right place for the selection to the old formatter... Can anyone please confirm?

Anonymous said...

This worked fine in eclipse Mars, but it fails with Neon :(

Here is the error I get:

The save participant 'org.eclipse.jdt.ui.postsavelistener.cleanup' caused an exception: java.lang.NoSuchMethodError: org.eclipse.jdt.internal.compiler.impl.CompilerOptions.versionToJdkLevel(Ljava/lang/Object;)J

java.lang.NoSuchMethodError: org.eclipse.jdt.internal.compiler.impl.CompilerOptions.versionToJdkLevel(Ljava/lang/Object;)J
at org.eclipse.jdt.old.formatter.CodeFormatterVisitor.(CodeFormatterVisitor.java:206)
at org.eclipse.jdt.old.formatter.DefaultCodeFormatter.formatCompilationUnit(DefaultCodeFormatter.java:243)
at org.eclipse.jdt.old.formatter.DefaultCodeFormatter.format(DefaultCodeFormatter.java:167)
at org.eclipse.jdt.old.formatter.DefaultCodeFormatter.format(DefaultCodeFormatter.java:150)
at org.eclipse.jdt.internal.corext.util.CodeFormatterUtil.reformat(CodeFormatterUtil.java:288)
at org.eclipse.jdt.internal.corext.util.CodeFormatterUtil.reformat(CodeFormatterUtil.java:351)
at org.eclipse.jdt.internal.corext.fix.CodeFormatFix.createCleanUp(CodeFormatFix.java:69)
at org.eclipse.jdt.internal.ui.fix.CodeFormatCleanUp.createFix(CodeFormatCleanUp.java:61)
at org.eclipse.jdt.internal.corext.fix.CleanUpRefactoring.calculateChange(CleanUpRefactoring.java:789)
at org.eclipse.jdt.internal.corext.fix.CleanUpPostSaveListener.saved(CleanUpPostSaveListener.java:387)
at org.eclipse.jdt.internal.ui.javaeditor.CompilationUnitDocumentProvider$5.run(CompilationUnitDocumentProvider.java:1636)
at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)

Anonymous said...

Hi,

I've managed to workaround that error in Neon with this code https://github.com/alostale/luna-java-formatter/commit/bffac295bc52f9c5d1c9d80fdf42571525085f27

But now the problem is that the format is completely messy :(

Any help/comment is more than welcome.

BTW, I created a github repo for this: https://github.com/alostale/luna-java-formatter

Mateusz Matela said...

@Anonymous from July 2: looks like you're using Eclipse to work with C/C++ code. This blog post is about Java code formatting and it has nothing to do with C code formatting (completely different tool).

@Asier Lostalé: I've fixed this compilation error in version 1.1.0 - have you tried it? See my comment from June 24.

Anonymous said...

Thanks @Mateusz Matela it works great also in Neon!

Sorry I didn't see the latest comment.

Would it be possible for you to share the source code of the plugin?

Anonymous said...

Thank you very much! You rock ;)

(My team has been using your plugin with eclipse 4.5 and we're preparing to upgrade to 4.6 - really happy about your update!)

Unknown said...
This comment has been removed by a blog administrator.
d3b5s3r said...

Looks like there's still a bug in Neon. We have our indentation set to 3 spaces and the OldJavaFormatter always uses 4 no matter what you have the indentation set as.

Mateusz Matela said...

@d3b5s3r I can change Old Formatter's indention in Neon without problems, you must be doing something wrong or have a general configuration problem. Try in a fresh workspace, check if changing other settings works for you and if the new formatter can see changes in profile, maybe you'll get some clues from these tests.

d3b5s3r said...

It's definitely using the old formatter. I think I've found the problem I had the indentation set to 3 spaces, tab size to 4 and the tab policy is spaces only. It's using the tab size and not the indentation size for the indentation. I can workaround it by setting the tab size to 3 as well.
It looks correct in the Profile edit screen but not the preview screen in the section where you select your profile.

Marco said...

Anyone know how use the old formatter on eclipse Neon? I tried to copy the jar file but on preferences I don't find the selector of the formatter

Benjamin said...

Under Eclipse Neon.2 (4.6.2), I needed to place the plugin in the "dropins" folder instead of the plugins folder to get it to work.

norhausen said...

Would be nice to know what you modified to upgrade the version so we can do it for the next eclipse release (Oxygen).
Thank you for your nice work

Anonymous said...

Did anyone make it work with latest Eclipse Oxygen?

Anonymous said...

Hi, sorry for the noise: it simply works also in Oxygen.

I just didn't install it correctly...

Bjarni said...

It worked for Neon, but not for Oxygen. I placed the exact same OldJavaFormatter_1.1.0.201606242022.jar file in the dropins folder with any success. Any ideas?

Anonymous said...

It simply does not work with Eclipse Oxygen. I put the .jar in the dropins folder and it sees the formatter alright, but id does not perform the actual formatting. Ctrl+Shift+F does nothing! Somebody please help!

Anonymous said...

It does not work because there is a compilation error in https://github.com/alostale/luna-java-formatter/blob/master/src/org/eclipse/jdt/luna/formatter/CodeFormatterVisitor.java#L1865

I just fixed it locally and might do a pull request later unless Asier fixes it sooner.

Anonymous said...

Please, do the pull request. I just created the repo in github because there was not any and I wanted to do a contrib, but I'm not the author neither I do maintain it.

Anonymous said...

I've created a new jar including the new pull request:

https://github.com/alostale/luna-java-formatter/releases/tag/1.1.4

For me previous version was working up to Oxygen.1 but stopped working from Oxygen.1a.