Eclipse Formatter for NetBeans IDE (Part 1)

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.

Comments:

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

Posted by markiewb on August 25, 2013 at 06:08 AM PDT #

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.

Posted by Geertjan on August 25, 2013 at 06:35 AM PDT #

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

Posted by markiewb on August 25, 2013 at 09:56 AM PDT #

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

Posted by Jens Hofschröer on August 25, 2013 at 11:24 AM PDT #

Hi Geertjian,

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

Regards,
vidhya

Posted by Vidhyadharan on August 27, 2013 at 11:34 PM PDT #

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

Posted by Franz van Betteraey on August 28, 2013 at 02:49 AM PDT #

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.

Posted by Geertjan on August 28, 2013 at 03:20 AM PDT #

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

Posted by guest on August 28, 2013 at 04:31 AM PDT #

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.

Posted by Geertjan on August 28, 2013 at 04:35 AM PDT #

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

Posted by Franz van Betteraey on August 28, 2013 at 04:40 AM PDT #

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.

Posted by guest on August 29, 2013 at 02:44 AM PDT #

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

Posted by Thomas on August 30, 2013 at 02:42 AM PDT #

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

Posted by guest on September 04, 2013 at 06:26 AM PDT #

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?

Posted by guest on September 19, 2013 at 03:52 AM PDT #

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.

Posted by Geertjan on September 19, 2013 at 01:20 PM PDT #

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/

Posted by guest on October 22, 2013 at 02:11 AM PDT #

@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

Posted by markiewb on October 22, 2013 at 02:11 PM 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
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today