Eclipse Formatter for NetBeans IDE (Part 2)

Judging by the many comments received, after I announced the work being done on the Eclipse Formatter for NetBeans IDE, in my blog and via e-mail, as well as via Twitter, there's really quite a lot of interest in this plugin. NetBeans users working in scenarios where some of their colleagues use Eclipse will find this plugin particularly useful since now organization-wide formatting rules can be shared for the first time ever.

While vacationing in France (near Joncy, which is a tiny place more or less near Buxy, which is itself a tiny place too, the past week, as well as the coming week), I've managed to make a bit of progress. Based on the comments I've received so far, users want this combination of support for Eclipse formatting in NetBeans IDE:

  • Global Eclipse Formatter. In the Options window, the user should be able to define (i.e., by selecting on disk) an Eclipse formatter XML file which, when formatting is done (via Source | Format and the keybinding Alt-Shift-F, as well as via related actions, such as Refactor | Inspect & Transform, and on saving), the Eclipse formatter should be used instead of the NetBeans formatter.

  • Project-Level Eclipse Formatter. In the Project Properties dialog of each project, the user should be able to define an Eclipse formatter to be used for the project, i.e., overriding the global Eclipse formatter.

  • NetBeans Formatter. The user should be able to (1) globally, i.e., via the Options winow, switch off the global Eclipse formatter, so that the NetBeans formatter is used instead and (2) for each project, the user should be able to switch off either the global or the project-level Eclipse formatter, so that the NetBeans formatter is used when formatting is done for a particular project.

In short, the user should be able to switch on/off and override the Eclipse formatter, globally and on project-level.

These, as I understand them, are the requirements. And, once configured, the formatter (whatever it happens to be for the current context) should behave absolutely seamlessly in exactly the same way, without complicated manoeuvres and additional steps.

Global Support

So, using these requirements, here's the Options window showing how/where to define the global Eclipse formatter:

Note that one can disable the global Eclipse formatter, via "Define global Eclipse formatter". Once that checkbox is unchecked, the NetBeans formatter is used globally, instead of the Eclipse formatter defined above. However, even though it is disabled, the global Eclipse formatter is still available, it simply needs to be reenabled to become active again.

There's also the "Show Debug Information" checkbox which, when checked, shows a balloon message (and note that all balloon messages are gathered into the new Notifications window in NetBeans IDE 7.4) whenever a formatting action is performed, showing which formatter was used to do the formatting. Handy to debug things when you're not getting the formatting results you'd like, to see which of your various formatters has been most recently used.

Finally, note that the text area above, as well as in the screenshot below, is not editable. It only serves to show the content of the selected Eclipse formatter XML file.

Project-Level Support

For each project, the Project Properties dialog lets you specify (1) whether a different Eclipse formatter should be used for this particular project or (2) whether for this particular project the NetBeans formatter should be used instead of the global Eclipse formatter:

Note that the two checkboxes in the screenshot above cannot be checked at the same time. I.e., if one is checked when you check the other one, the first is automatically unchecked. Makes sense because either you're overriding the global Eclipse formatter with your local one or you're wanting to use the NetBeans formatter, not both at the same time.

Conclusion

All of the above actually works right now. Just my bandwidth is too low (remember, I'm in Joncy, somewhere in the middle of France, with cows, wine, and medieval churches, i.e., bandwith is not a primary concern in these parts of the world) to upload it to the Plugin Portal. Hence, you're welcome to try the Eclipse formatter in NetBeans IDE, but note that none of the screenshots above are reflected in the plugin you'll find here, currently:

http://plugins.netbeans.org/plugin/50212/?show=true

The above link points, at the time of writing, to the 1.2 version of the plugin, which is used exactly as defined in the original blog entry about this plugin.

Nonetheless, at random times of the day I can get on-line, so looking forward to any comments anyone might have on my progress.

Update 31 October 2013: See part 3 here.

Comments:

Hi,

What I'd like to see is that every code change, that might effect the contents of files - e.g. organize imports, apply refactoring hint - to adhere to the formattting rules specified in the eclipse formatter.

Thanks,
Jan.

Posted by guest on September 06, 2013 at 05:37 AM PDT #

Yes, that's exactly what's supported right now, and is as described above, i.e., that's what I mean by "once configured, the formatter (whatever it happens to be for the current context) should behave absolutely seamlessly in exactly the same way, without complicated manoeuvres and additional steps".

Posted by Geertjan on September 06, 2013 at 05:39 AM PDT #

To be even more precise, though the 1.1 and 1.2 version of the plugin do not have the "seamless support", the 1.3 version (here on my laptop), has this support. I.e., configure the Eclipse formatter and then use it for absolutely everything everywhere where formatting is used in any way at all. Sources are here:

https://java.net/projects/nb-api-samples/sources/api-samples/show/versions/7.3/misc/EclipseFormatter

And will publish this version of the plugin once I am somewhere with better bandwidth.

Posted by Geertjan on September 06, 2013 at 05:42 AM PDT #

Great! Thanks.

Posted by Jan on September 06, 2013 at 07:38 AM PDT #

It would be nice if the plugin honored the reformat preference set in Options > Editor > On Save > Reformat. Sometimes you are working in a legacy file and don't want the code to be reformatted on save.

Posted by Rangi Keen on September 09, 2013 at 05:56 AM PDT #

The plugin refuses to install for me since our project is still using Java 1.6 (it wants 1.7)

Is this a fixed dependency or is there a way I could use it today with Java 6?

Posted by Martin on September 20, 2013 at 04:25 AM PDT #

"Refuses to install"? What does that mean. Provide steps and results, i.e., how can you tell that 1.6 is needed? No, I am not going to install the plugin and try for myself and on and on. I am going to wait for you to provide a full description of what you did (steps) and what the results are (what message did you get, and where, and what does it say, and what happens then)? The more specific you are, the better I am able to help you.

Posted by Geertjan on September 20, 2013 at 04:45 AM PDT #

any chance to update http://plugins.netbeans.org/plugin/50212/?show=true

so we can use the new version 1.3?

Posted by guest on October 14, 2013 at 07:14 AM PDT #

Not sure what the new version is you're referring to.

Posted by Geertjan on October 14, 2013 at 08:57 AM PDT #

Why does it reformat on save? Can it be turned off? I did uncheck the options in the IDO "On save" window

Posted by guest on October 16, 2013 at 04:53 AM PDT #

How to remove existing rules?

Posted by guest on October 16, 2013 at 04:56 AM PDT #

> Why does it reformat on save? Can it be turned off? I did uncheck the options in the IDO "On save" window

> Is this a fixed dependency or is there a way I could use it today with Java 6?

Fixed in http://plugins.netbeans.org/plugin/50877/eclipse-code-formatter-for-java

> How to remove existing rules?
Not supported.

Posted by markiewb on October 20, 2013 at 08:09 AM PDT #

Post a Comment:
  • HTML Syntax: NOT allowed
About

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today