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

☆樱花仙子☆ 提交于 2019-12-18 22:32:44

问题


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"
            }
            )
        );
}

回答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



来源:https://stackoverflow.com/questions/9186714/can-the-eclipse-formatter-be-configured-to-indent-multiple-lines-between-parenth

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!