Wink Saville’s Blog

January 5, 2008

Eclipse - line wrapping and indentation when formatting

Filed under: Eclipse, java — wink @ 11:45 am

Eclipse is a reasonable IDE and it has a reasonable pretty printer (Source/Format or Shift+Ctrl+F) except for one thing; when a function call exceeds 80 characters the formatting is terrible in my opinion:

/**
* Arguments
*/
class Example {
  void foo() {
    Other
             .bar(
                     100, 200, 300,
                     400, 500, 600,
                     700, 800, 900);
    }
}

Anyway, this can be changed by going to menu:

Window/Preferences/Java/Code Style/Formatter

Here you should make a “New” profile from an existing profile that is close to what you want, I used “Eclipse [built-in]” for my base, it was the default. And then select “Edit”. In the dialog box “Profile ‘….’” select “Line Wrapping” tab and then I changed “Maximum line width:” to 100 from 80 and then selected “Function Calls/Arguments” in the window below and then in the “Settings for agruments/Indentation policy:” selected “indent by one” this cured the formatting problem and now the above is:

/**
* Arguments
*/
class Example {
  void foo() {
    Other.bar(100, 200, 300, 400,
              500, 600, 700, 800, 900);
    }
}

Much better!

1 Comment »

  1. Great Tip! I hate 80 characters limit and switching the readable code to un-readable code
    StringBuffer a = new StringBuffer()
    a.append(”Abc….”);

    to

    StringBuffer a = new StringBuffer()
    a.
    append(”Abc….”);

    Comment by Javed — January 22, 2008 @ 1:32 pm

RSS feed for comments on this post. TrackBack URL

Leave a comment

Powered by WordPress