X

Geertjan's Blog

  • September 6, 2013

Eclipse Formatter for NetBeans IDE (Part 2)

Geertjan Wielenga
Product Manager

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.

Join the discussion

Comments ( 13 )
  • guest Friday, September 6, 2013

    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.


  • Geertjan Friday, September 6, 2013

    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".


  • Geertjan Friday, September 6, 2013

    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.


  • Jan Friday, September 6, 2013

    Great! Thanks.


  • Rangi Keen Monday, September 9, 2013

    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.


  • Martin Friday, September 20, 2013

    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?


  • Geertjan Friday, September 20, 2013

    "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.


  • guest Monday, October 14, 2013

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

    so we can use the new version 1.3?


  • Geertjan Monday, October 14, 2013

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


  • guest Wednesday, October 16, 2013

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


  • guest Wednesday, October 16, 2013

    How to remove existing rules?


  • markiewb Sunday, October 20, 2013

    > 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.


  • Sebastian Tuesday, December 9, 2014

    Hi,

    how can I configure plugin to format only modified lines? Is it possible? This is possible with netbeans formatter by Options->Editor->On Save Reformat: Modified lines only. But this option always use netbeans formatter.

    Thansk in advance.


Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.Captcha