可以将Eclipse格式化程序配置为在括号之间正确缩进多行吗?

Ste*_*ler 8 java eclipse formatter

可以配置(或扩展)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"
            }
        )
    );
}
Run Code Online (Sandbox Code Playgroud)

我玩过我能找到的所有设置."never join lines"选项使它不会完全屠宰代码,但即使这样,缩进也会被剥离,代码就像这样:

    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"
    }
    )
    );
Run Code Online (Sandbox Code Playgroud)

我发现能够像这样关闭这些块的格式:

    // @formatter:off
    String[] numbers = new String[] {
        "one",
        "two",
        "three",
        "four",
    };
    // @formatter:on
Run Code Online (Sandbox Code Playgroud)

这是一个体面的工作,除了我的代码最终散落在它们之外,代码清理的"正确的缩进"部分忽略了指令并且无论如何都会弄乱缩进.

编辑:我找到了"Line Wrapping" - >"包装行的默认缩进"和"数组初始化的默认缩进"的设置,并将它们设置为"1"而不是"0".这对于数组初始化器来说更好,但仍然不会缩减关闭的parethesis以匹配我想要它的方式的左括号:

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"
            }
            )
        );
}
Run Code Online (Sandbox Code Playgroud)

HRg*_*ger 1

您检查换行选项卡了吗?如果您为表达式/数组初始值设定项和函数调用/限定对象分配参数选择“将所有元素,每个元素放在新行上”,我想您会得到类似的东西

  • 我只是想让它修复缩进,但永远不要为我分割线。我仍然希望能够执行一行数组和方法调用: new int[]{1,2,3}; call.method("一","二"); 在数组初始值设定项的情况下,即使我让它为我分割行,它也不会将右大括号放在新行上并进行缩进,以便它与左大括号对齐,而是将其放在最后一个数组之后元素。 (5认同)