X

Geertjan's Blog

  • August 25, 2013

Eclipse Formatter for NetBeans IDE (Part 1)

Geertjan Wielenga
Product Manager

Several times I've been told that the only major blocker to considering NetBeans IDE as a viable alternative is that Java formatting rules have been defined in Eclipse and that those formatting rules cannot be imported into NetBeans IDE.

Well, Willie Scholtz from South Africa has solved this problem:

http://epochcoder.blogspot.nl/2013/08/import-eclipse-formatter-into-netbeans.html

And I followed the instructions in his blog and I now have a plugin that provides the possibility to integrate an Eclipse XML formatting file into NetBeans IDE:

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

Here's how it works. Install the plugin into NetBeans IDE 7.3.1 (should also work in later versions but haven't tried that) and then go to the Options window and register your Eclipse formatting XML file:

That's it, you're done. Now, whenever you save a Java source file, the formatting rules defined in the XML file you registered above are applied. At the moment, no checks are done to see whether the XML file is a valid Eclipse formatting file. The assumption is that you've configured Java formatting rules in Eclipse, that you exported those rules, and that you therefore have an XML file containing all those rules. That XML file is the one that you should have registered above. No restarts or anything like that is needed. Immediately after you click OK above, the formatting rules in the XML file will be applied whenever you make a change in a file and click Save.

Regardless of whether this plugin is useful to you, do you want to help by trying it out? Take the following steps, in this order:

  1. Start up NetBeans IDE 7.3.1 with a fresh user directory. And then install the plugin into NetBeans IDE 7.3.1.

  2. In the New Project wizard, go to Java | Java Application and complete the wizard. Now you have a new Java SE project with a Java source file.

  3. Go to the Options window and register an XML file on disk, named "eclipse-demo.xml", with this content:

    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <profiles version="1">
    <profile kind="CodeFormatterProfile" name="eclipse-demo" version="1">
    <setting
    id="org.eclipse.jdt.core.formatter.alignment_for_enum_constants"
    value="49"/>
    </profile>
    </profiles>

    When you click OK to exit the Options window, this message in the status bar (i.e., left corner of the IDE) tells you that you have set the Eclipse formatting file, if you don't see this, you have not succeeded in setting the file:

  4. Declare the following in the Java source file:
    public enum X { A,B,C,D,E,F}

    Save the file and now you should see this:

    public enum X {

    A,

    B,

    C,

    D,

    E,

    F
    }

Any feedback is very welcome.

Additional references:

Update 6 September 2013: See part2 here.

Update 31 October 2013: See part 3 here.

Join the discussion

Comments ( 17 )
  • markiewb Sunday, August 25, 2013

    Finally Geertjan! I already saw several inhouse-solutions which did the same. Are you getting the plugin verified?

    > Probably problems will occur if you try this on Matisse GUI Builder files, i.e., with guarded blocks.

    You could ignore the formatting when the document contains such guarded blocks.

    Like this (pseudo-code):

    GuardedSectionManager guards = GuardedSectionManager.getInstance(doc);

    Iterable<GuardedSection> guardedSections=guards.getGuardedSections();

    containsGuardedSections=(null!=guardedSections && guardedSections.hasNext());

    if (!containsGuardedSections){

    //format(...)

    }else{

    //warn the user via popup, notification or statusbar

    }

    Best regards, Benno


  • Geertjan Sunday, August 25, 2013

    Hi Benno, many thanks! Uploaded version 1.2 of the plugin, which does a check to see whether the document has guarded blocks. If guarded blocks are found, the standard NetBeans formatter is used, otherwise the Eclipse formatter is used, if it exists. Would appreciate it if you'd take the steps in the blog above, exactly in that order, to verify that it works.


  • markiewb Sunday, August 25, 2013

    @Geertjan: Yes it worked. I sent you more feedback by email


  • Jens Hofschr&ouml;er Sunday, August 25, 2013

    Hi Geertjan.

    It would be nice to see this in the plugin center. I did some testings a few years ago (same starting point from the "How to use the eclipse code formatter from your code" blog entry.) but didn't get finished.

    Jens


  • Vidhyadharan Wednesday, August 28, 2013

    Hi Geertjian,

    Good feature! i really need it . since i am the only guy using NB in my team.

    Regards,

    vidhya


  • Franz van Betteraey Wednesday, August 28, 2013

    Hi Geertjan,

    unfortunately I did not manage to successfully register the "Eclipse [built-in]" formatter profile (used "Export All..." to export the profile from eclipse Kepler) in NetBeans (no NetBeans status bar information).

    Would be nice if this would work.

    Kind regards

    Franz


  • Geertjan Wednesday, August 28, 2013

    Did you follow the steps, i.e., start with a fresh userdir? Then install the plugin? Then create a new Java application? Then, and only then, register your Eclipse formatter file? All in that order?

    Once the above works, let's look at your own process, but, the only way I can reproduce the problem is if you actually follow the steps in the order given.


  • guest Wednesday, August 28, 2013

    Hi Geertjan,

    I indeed forget to create a project. Now I get the confirmation that the file was registered.

    But what I see now is, that while working inside the code, the NetBeans formatter is responsible for code formatting (e.g. replaces tabs with spaces or when using Source -> Format) and only when saving the file the eclipse formatter reformats the whole file.

    While this might be the only way to guarantee that the code gets formatted the eclipse way I would have (also) expected that the NetBeans format options would be adjusted to the eclipse options best possible so that the rules also apply while editing files or reformatting (parts of) a file on demand.

    Also it should be possible to disable the formatting "on save".

    So the current plugin is a good first attempt to handle the problem but not yet something I would like to use day by day.

    Best regards

    Franz


  • Geertjan Wednesday, August 28, 2013

    Yes, currently, the way the Eclipse formatter in NetBeans works right now, only when you save the file is the Eclipse formatter used. It's really a proof of concept and it's good to hear it works for you for what it currently does. As we work more on it, features such as those you're looking for will definitely be added.


  • Franz van Betteraey Wednesday, August 28, 2013

    Hi Geertjan,

    I can confirm that it works for me like (you) expected ;-)!

    Nice to hear that you take care of the plugin and that more features should be added.

    Kind regards,

    Franz


  • guest Thursday, August 29, 2013

    Hi Geertjan,

    Just tried this on my 7.3.1 install (Ubuntu 13.04), and after some initial hiccup it worked quite well. First tried with the demo file you provided, followed by our specific eclipse formatter XML export. After that loaded one of our own maven oriented projects. Thanks a lot, for this fine addition to Netbeans!

    Cheers,

    Jan.


  • Thomas Friday, August 30, 2013

    Thanks for this plugin. I would like to suggest to not format on save, but on action (keyboard/menu) and then either the selected part or the whole file (if nothing is selected).


  • guest Wednesday, September 4, 2013

    Great Tool, exactly what I was looking for!

    Only bug so far is that it seems to loose the XML file reference when you exit netbeans (it still shows up in the settings dialog, but it isn't applied).


  • guest Thursday, September 19, 2013

    As i understand, this plugin works only with JDK 1.7+ and it would be very nice to have it for JDK 1.6 also.

    Are there any plans for this?


  • Geertjan Thursday, September 19, 2013

    Pretty sure it works with any JDK. If not, please tell me how to reproduce the problem. Steps, provide steps that need to be followed, otherwise I'm not going to spend time on this.


  • guest Tuesday, October 22, 2013

    I get the same message about the plugin requiring jdk 1.7 when running netbeans with 64bit jdk 1.6.45 on windows 7 also 64 bit. Maybe just a piece of metadata in the plugin somewhere?

    To reproduce I have the jdk 1.6 set as my main java version and try to install the nbm downloaded from plugins.netbeans.org from the downloaded plugins section.

    screenshot

    http://postimg.org/image/lg7izknrt/


  • markiewb Tuesday, October 22, 2013

    @Guest (author of "I get the same message about the plugin requiring jdk 1.7..."):

    Please use the fixed version at http://plugins.netbeans.org/plugin/50877/eclipse-code-formatter-for-java


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