0

我正在测试 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";

一切都将被包装在同一个缩进处(我检查了选项Never join already Wrapped lines

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

我觉得可读性较差。

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

4

1 回答 1

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-05-18T08:09:04.863 回答