IntelliJ IDEA 13: Importing Code Formatter Settings from Eclipse

Many development teams are using more than one IDE because it’s a very personal matter, and people always seek what best suits their individual needs. And then there’s collaboration, and it sometimes is harder when different sorts of IDEs are involved. One of the most frequently encountered problems is code style, that has to be consistent in the entire project.

You may have heard about Eclipse Code Formatter, a quite popular IntelliJ IDEA plugin that lets you, what else, — use Eclipse’s code formatter with IntelliJ IDEA. In most cases the plugin is fine, but because it’s calling Eclipse API directly from IntelliJ IDEA, there can be problems with processing such actions as refactoring, code generation, etc., so it’s not always that helpful.

Now things are going to be a bit easier, because IntelliJ IDEA 13 is capable of importing code formatter settings from Eclipse without the use of any plugins. All you need is to export settings from Eclipse (go to Eclipse’s PreferencesJavaCode StyleFormatter and export the settings to an XML file via the Export All button.), and then open IntelliJ IDEA SettingsCode StyleJava, click Manage, and import that XML file by simply clicking Import.

Currently, IntelliJ IDEA supports the import of the following settings:

  • General
    • Right margin, Formatter on/off tags
    • Indentation
    • Indent size
    • Usage of ‘Tab’ character
    • Usage of Tab only for leading indentation (Smart Tabs)
    • Indent ‘case’ branches from ‘switch’
    • Indent class members
    • Keep comment at first column
  • Spaces (Java)
    • Before/after comma (as set for Eclipse method declaration parameters)
    • After comma in type arguments
    • Within array initializer braces
    • Within brackets (in array reference)
    • Within parentheses of: annotation, ‘for’, ‘if’, ‘catch’ ’while’, ’switch’, method, empty method, parenthesized expression, method call, type cast, ‘synchronized’
    • Before parentheses of: ‘try’, ‘for’, ’while’, ‘switch’, method, ’if’, ‘catch’, method, method call, ‘synchronized’.
    • After type cast
    • Around unary, assignment operators (if it’s set for ‘before’ and ‘after’ in Eclipse).
    • Before opening brace of: array initializer, ‘switch’
    • Before ‘?’ in conditional expression
    • Space before/after ‘:’ in conditional expression
    • Space around binary operators (a single Eclipse setting is mapped to multiple IntelliJ IDEA’s settings)
  • Blank lines
    • Around fields and methods
    • Before/after package
    • Before/after imports
    • Before method body
    • Keep blank lines in code (number of empty lines to preserve)
  • Wrapping
    • New line before: closing brace in array initializer, ‘else’ in ‘if’ statement, ‘finally’ and ‘catch’ in ‘try’ statement, binary operator (if wrapped)
    • New line after:  opening brace in array initializer
    • Special ‘else if’ treatment (compact ‘else if’)
    • Keep simple blocks in one line
    • Keep control statements in one line
  • Alignment of: array initializer expressions, arguments in method declarations and calls, field declarations, extends list, assignments, binary expressions, ‘throws’ clause, resources in ‘try’.
  • Brace style for: code blocks, methods and classes
  • JavaDoc
    • Enable JavaDoc formatting
    • Blank lines in JavaDoc

Keep in mind that code style settings in IntelliJ IDEA and Eclipse are fundamentally different and can’t be mapped one to another with complete accuracy, e.g. you can’t tell IntelliJ IDEA to put space after ‘(‘ or not to put it before ‘)’. However, we’re constantly working on improving this interoperability, and your feedback with problems and use cases is very appreciated.

What’s planned for the nearest future:

  • More settings (based on your feedback, tell us what’s important for you.)
  • More languages.
  • Working directly with .settings/org.eclipse.jdt.core.prefs, without the need to use the XML export/import.
  • Importing of Conventions (from “Java Code Style”), e.g. field prefixes.
  • Importing of Code templates and Organize imports settings.

Feel free to share your feedback here, or in our discussion forum, or issue tracker.

Develop with Pleasure!

About Andrey Cheptsov

Andrey Cheptsov is IntelliJ IDEA product marketing manager at JetBrains. He's passionate about productivity, programming languages and tools.
This entry was posted in New Features and tagged , . Bookmark the permalink.

18 Responses to IntelliJ IDEA 13: Importing Code Formatter Settings from Eclipse

  1. Henning says:

    That is a great feature for teams using both IDEs (and I hadn’t noticed it so far, so thanks for this blogpost)!

    What I’m currently missing most is an option to mirror the Eclipse formatter’s behaviour regarding imports: Grouping imports by their top level package, inserting a blank line between groups, but sorting other packages between the groups (effectively, giving more than one “other” group).

    It’s not that I particularly like that scheme, I just can’t force all Eclipse users to alter their default settings…

  2. Meo says:

    @Henning
    Eclipse Code Formatter plugin to the rescue! :)

    I found that it works nicely with the plugin together, because Eclipse API is actually not used for actions as refactoring, code generation, etc, so it helps if the code style in IntelliJ is similar to the Eclipse one.

  3. mooli says:

    imports support needed here too.
    (specifically order e.g java;javax;org;com;)

    This feature will become relevant for us at ovirt [1] when/if it is supported:

    Until then it seems we must use eclipse code formatter plugin [2].

    [1] http://www.ovirt.org/Home
    [2] http://www.ovirt.org/Building_oVirt_Engine/IDE#IntelliJ

  4. Bert says:

    Must be doing something false, but I do not have the import button. And this was one of the main reasons to upgrade :-(

    Any ideas what I might be doing wron?

  5. Antoine Sabot-Durand says:

    Great feature. Yet I have still formatting issue in Javadoc.
    When you format javadoc empty line under eclipse, it adds a space after * while IntelliJ puts carriage return directly after *.

    Quite stupid issue but enough to add false changes to push to VCS. If you have a trick to fix that it would be awesome.

  6. Bert says:

    No need to check, I know that I have not enabled the Eclipse plugin. If this is needed, than please correct the post. In the moment, it reads:

    “Now things are going to be a bit easier, because IntelliJ IDEA 13 is capable of importing code formatter settings from Eclipse without the use of any plugins. ”

    Thank you for your input

  7. Patrick Dezenzio says:

    Great tool but you need to be careful when upgrading. I upgraded from 12 to 13 and it’s taken all morning trying to figure out why the formatter doesn’t work. It keeps displaying a message that the settings file doesn’t exist and the location points to Intellij12, not Intellij13. I even wen tin a set the default to my file located in the .intellij13 folder. Doing a search for a file that contains the old setting and set it there. I uninstalled 12 so that’s why the folder doesn’t exist.

  8. Patrick Dezenzio says:

    Ok, the culprit is the misc.xml file under your project folder – .idea. I can see the line that points to the formatter file but the path name shows .intellij12. Changing this to .intellij13 works until you start up Intellij and something is changing the value back. Where in the world is Intellij getting .Intellij12? It doesn’t exist. I might have to dump intellij for MyEclipse until I figure this out because the normal formatting is screwing up the team.

  9. Patrick Dezenzio says:

    I found it!! Ignore this tutorial. There are 2 places you can set the formatting and the place you want to change it is under Eclipse Code Formatter, not Code Style. You can see it under Settings.

  10. Shehan Simen says:

    This is really great. I was looking for this for years. Thanks a lot to IDEA

  11. Luca Garulli says:

    Eclipse Code Formatter doesn’t work with last IntelliJ 13.1, help!

  12. Mohamed Mounirou says:

    You need to install the “Eclipse Integration Plugin” first to have the “import” button

  13. Ted says:

    Currently working on a very Eclipse heavy team with a formatting set that doesn’t seem to map completely to IntelliJ.

    I’d be happy to help with listing the issues I have (of which I seem to have quite a few) if it’d help.

    As it is now I have to keep Eclipse open for the formatting only, which is a pain. The import rules I’ve sorted manually, so that’s not an issue (but would be cool if they also could be imported automagically)

    I’ll give the Eclipse Code Formatter plugin a try – but the optimal solution would obviously be to use IntelliJ functionality alone without having to use the Eclipse monstrosity behind the scenes.

    /T

  14. tang says:

    How to use two different formatter files

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">