【问题标题】:How to keep custom indentation for binary operation in Eclipse formatter如何在 Eclipse 格式化程序中为二进制操作保留自定义缩进
【发布时间】:2021-08-07 10:17:05
【问题描述】:

我正在测试 eclipse 格式化程序,以尝试使用与我当前的 Java 编码格式相匹配的东西。但是,我找不到保留当前缩进以进行字符串连接(二进制操作)的选项。比如我要写这个字符串(SQL查询):

// Current code, I want to keep this format
String query = "select "
            + "a, "
            + "b, "
            + "c, "
        + "from table "
        + "where "
            + "a = 1 "
            + "and b = 2 "
        + "order by c";

所有内容都将被包裹在同一个缩进中(我选中了选项从不加入已经包裹的行)

// Formatted code
String query = "select "
        + "a, "
        + "b, "
        + "c, "
        + "from table "
        + "where "
        + "a = 1 "
        + "and b = 2 "
        + "order by c";

我觉得不太可读。

我看到有一个选项可以关闭部分代码的格式化程序,但我想知道是否有适合我需要的内置选项。

【问题讨论】:

  • 注意:Java 15“文本块”会更易读
  • @greg-449 我刚刚检查了 Java 15 文本块。我会接受这个作为答案,但不幸的是我不使用 Java 15。

标签: eclipse code-formatting eclipse-formatter


【解决方案1】:

从 Java 15 开始,“文本块”的可读性最高:

String query = """
    select
      a,
      b,
      c,
     from table
     where
      a = 1
      and b = 2
     order by c
     """.replace("\n", "");

产生:

select  a,  b,  c, from table where  a = 1  and b = 2 order by c

其中有一些额外的不重要的空白。

【讨论】:

    猜你喜欢
    • 2021-01-19
    • 2015-09-01
    • 2020-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-04
    • 1970-01-01
    相关资源
    最近更新 更多