8

可以配置(或扩展)eclipse 格式化程序和代码清理以在以下示例中添加我期望的缩进:

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

我已经使用了所有我能找到的设置。“从不加入行”选项可防止它完全破坏代码,但即便如此,缩进也被全部剥离,代码如下所示:

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

我发现可以关闭这样的块周围的格式化:

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

这是一个体面的工作,除了我的代码最终到处都是它们,并且代码清理的“正确缩进”部分忽略了指令并且无论如何都会弄乱缩进。

编辑:我找到了“换行”->“换行的默认缩进”和“数组初始化的默认缩进”的设置,并将它们设置为“1”而不是“0”。这对于数组初始化器来说更好,但仍然不会缩进右括号以按照我想要的方式匹配左括号:

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

1 回答 1

1

您是否检查了换行选项卡。如果您为表达式/数组初始值设定项和函数调用/合格对象分配参数选择“将所有元素包装在新行上”,我认为您会得到类似的东西

于 2012-02-08T01:55:10.587 回答