java - 能否将 Eclipse 格式化程序配置为在括号之间正确缩进多行?

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

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

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

我发现可以关闭像这样的 block 周围的格式设置:

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

最佳答案

您是否检查了换行选项卡。如果您为表达式/数组初始值设定项和函数调用/限定对象分配参数选择“换行所有元素,每个元素都换行”,我想您会得到类似的结果

关于java - 能否将 Eclipse 格式化程序配置为在括号之间正确缩进多行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9186714/

相关文章:

java - 使用 Mysql 的 Java Web 应用程序中的增补字符支持

java - DateTime.plusMinutes(..) 返回分钟 -1

Java分割问题

Android 复选框 state_enabled

java - 将格式化字符串保存到 LocalDate

java - 通过用户输入指定精确的小数位数,Java

java - 方法在其生命周期中应该只调用一次

java - 从命令行使用 eclipse 组织 java 文件的导入

eclipse - 在 Eclipse 构建路径中使用用户库时出现 ClassNotFoundException

python - 格式化程序 %r 和 %s - 理论上的差异,但这个脚本另有说明