Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can the Eclipse formatter be configured to indent multiple lines between parenthesis properly?

Can eclipse formatter and code cleanup be configured (or extended) to add the indentation I expect in the following examples:

public static void main(String[] args) {
    String[] numbers = new String[] {
        "one",
        "two",
        "three",
        "four",
    };

    new MessageFormat("{0} {1} {2} {3}").format(
        "this is string one",
        "this is string two",
        "this is string three"
    );

    System.out.println(
        new MessageFormat("{0} {1} {2} {3}").format(
            new String[]{
                "this is string zero",
                "this is string one",
                "this is string two",
                "this is string three"
            }
        )
    );
}

I've played around with all the settings that I can find. The "never join lines" option keeps it from completely butchering the code, but even then the indentation is all stripped and the code comes out like this:

    String[] numbers = new String[] {
    "one",
    "two",
    "three",
    "four",
    };

    new MessageFormat("{0} {1} {2} {3}").format(
    "this is string one",
    "this is string two",
    "this is string three"
    );

    System.out.println(
    new MessageFormat("{0} {1} {2} {3}").format(
    new String[] {
    "this is string zero",
    "this is string one",
    "this is string two",
    "this is string three"
    }
    )
    );

I discovered the ability to turn off formatting around such blocks like this:

    // @formatter:off
    String[] numbers = new String[] {
        "one",
        "two",
        "three",
        "four",
    };
    // @formatter:on

Which is a decent work around, except that my code ends up littered with them, and the "correct indentation" part of code cleanup ignores the directive and messes up the indentation anyway.

Edit: I found the settings for "Line Wrapping" -> "Default indentation for wrapped lines" and "Default indentation for array initializes" and have set them to "1" instead of "0". That is better for the array initializers, but still doesn't indent closing parethesis to match the opening parenthesis the way that I want it to:

public static void main(String[] args) {
    String[] numbers = new String[] {
        "one",
        "two",
        "three",
        "four",
    };

    new MessageFormat("{0} {1} {2} {3}").format(
        "this is string one",
        "this is string two",
        "this is string three"
        );

    System.out.println(
        new MessageFormat("{0} {1} {2} {3}").format(
            new String[] {
                "this is string zero",
                "this is string one",
                "this is string two",
                "this is string three"
            }
            )
        );
}
like image 208
Stephen Ostermiller Avatar asked Feb 08 '12 01:02

Stephen Ostermiller


Video Answer


1 Answers

Did you check line wrapping tab. if you select "Wrap all elements, every element on a new line" for Expressions/Array Initializers and Function Calls/Qalified object allocation arguments I think you will get something similar

like image 188
HRgiger Avatar answered Oct 17 '22 16:10

HRgiger