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

1

8

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"
            }
            )
        );
}
Bleier answered 8/2, 2012 at 1:40 Comment(1)
Yes; change the indents for each of the constructs. There are 3-4 options for each. You have a pretty broken setup if it's flattening anything, by default I think I get the formatting you want.Greenheart
I
1

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

Identical answered 8/2, 2012 at 1:55 Comment(1)
I just want it to fix the indentation, but never to split lines for me. I still want to be able to do one line arrays and method calls: new int[]{1,2,3}; call.method("one","two"); In the case of array initializers, even when I have it split lines for me, it doesn't put the closing brace on a new line and make the indentation so that it lines up with the opening brace, it puts it after the last array element.Bleier

© 2022 - 2024 — McMap. All rights reserved.